CNTK入门01:初次使用和运行第一个例子
来源:互联网 发布:华润网络福利待遇 编辑:程序博客网 时间:2024/06/05 00:44
Python API for CNTK
参看官网:https://www.cntk.ai/pythondocs/gettingstarted.html
1.输出cntk的版本号:
>>> import cntk>>> cntk.__version__'2.0rc2'
2.两个数组相减求值 ; evaluation评估,求值
>>> cntk.minus([1, 2, 3], [4, 5, 6]).eval()array([-3., -3., -3.], dtype=float32)
3.计算 (2-4)**2 + (1-6)**2
>>> import numpy as np>>> x = cntk.input(2)>>> y = cntk.input(2)>>> x0 = np.asarray([[2., 1.]], dtype=np.float32)>>> y0 = np.asarray([[4., 6.]], dtype=np.float32)>>> cntk.squared_error(x, y).eval({x:x0, y:y0})array([ 29.], dtype=float32)
4.asarray() 方法返回NumPy接口:
>>> import cntk as C>>> c = C.constant(3, shape=(2,3))>>> c.asarray()array([[ 3., 3., 3.],[ 3., 3., 3.]], dtype=float32)>>> np.ones_like(c.asarray())array([[ 1., 1., 1.],[ 1., 1., 1.]], dtype=float32)
5.综述和第一次运行
CNTK2是之前版本的大修改,使得它现在有强大的数据控制能力,如何读入数据,训练,测试和minibatch构建;
Python相关的提供直接构建网络图和数据读入前的操作不仅仅是为了更强大和更复杂的网络,也是为了当一个模型被创建和被调试时候的Python交互;
CNTK2包含一个准备继续扩展的例子库和层库(a layers libary)。允许简单的通过组合各个块,构建一个强大的深度网络。
例如:CNN,RNN(LSTMs),FCN.
第一次基本的使用例子:一个标准的FCN (a standardfully connected deep network)
在CNTK上训练或者运行一个网络,第一步需要决定运行在哪一个设备上,如果你有可用的GPU,训练时间将会极大的被改进。为了明确使用设备,设置使用设备如下:
from cntk.device import set_default_device, gpuset_default_device(gpu(0))
利用全连接层FCN训练一个分类器的网络例子
用函数:Sequential()和Dense()
网络结构:
a 2-layer fully connected deep neural network with 50 hidden dimensions per layer。
ce
is the cross entropy which defined our model’s loss function
pe
is the classification error.
代码如下;
#encoding=utf-8from __future__ import print_functionimport numpy as npimport cntk as Cfrom cntk.learners import sgd, learning_rate_schedule, UnitTypefrom cntk.logging import ProgressPrinterfrom cntk.layers import Dense, Sequentialdef generate_random_data(sample_size, feature_dim, num_classes): # Create synthetic data using NumPy. Y = np.random.randint(size=(sample_size, 1), low=0, high=num_classes) # Make sure that the data is separable X = (np.random.randn(sample_size, feature_dim) + 3) * (Y + 1) X = X.astype(np.float32) # converting class 0 into the vector "1 0 0", # class 1 into vector "0 1 0", ... class_ind = [Y == class_number for class_number in range(num_classes)] Y = np.asarray(np.hstack(class_ind), dtype=np.float32) return X, Ydef ffnet(): inputs = 2 outputs = 2 layers = 2 hidden_dimension = 50#设置模型 # input variables denoting the features and label data features = C.input((inputs), np.float32) label = C.input((outputs), np.float32) # Instantiate the feedforward classification model 前馈分类器模型 my_model = Sequential ([ Dense(hidden_dimension, activation=C.sigmoid), Dense(outputs)]) z = my_model(features) ce = C.cross_entropy_with_softmax(z, label) pe = C.classification_error(z, label)#初始化训练器 # Instantiate the trainer object to drive the model training lr_per_minibatch = learning_rate_schedule(0.125, UnitType.minibatch) progress_printer = ProgressPrinter(0) trainer = C.Trainer(z, (ce, pe), [sgd(z.parameters, lr=lr_per_minibatch)], [progress_printer]) # Get minibatches of training data and perform model training minibatch_size = 25 num_minibatches_to_train = 1024 aggregate_loss = 0.0 for i in range(num_minibatches_to_train): train_features, labels = generate_random_data(minibatch_size, inputs, outputs) #训练# Specify the mapping of input variables in the model to actual minibatch data to be trained with trainer.train_minibatch({features : train_features, label : labels}) sample_count = trainer.previous_minibatch_sample_count aggregate_loss += trainer.previous_minibatch_loss_average * sample_count last_avg_error = aggregate_loss / trainer.total_number_of_samples_seen test_features, test_labels = generate_random_data(minibatch_size, inputs, outputs) avg_error = trainer.test_minibatch({features : test_features, label : test_labels}) print(' error rate on an unseen minibatch: {}'.format(avg_error)) return last_avg_error, avg_errorif __name__=='__main__':np.random.seed(98052)ffnet()命令行下运行:Python simplenet.py
- CNTK入门01:初次使用和运行第一个例子
- Activity工作流(2)-入门安装运行第一个例子
- Struts2入门--第一个例子
- Velocity 第一个例子. 正常运行.
- 成功运行RMI的第一个例子
- hibernate入门的第一个例子
- 初学webservice,使用axis2第一个例子
- Lgame引擎搭建和第一个例子
- 《C和指针》的第一个例子
- springmvc的介绍和第一个例子
- Hadoop 环境配置和第一个例子
- OpenLayers介绍和第一个例子
- 初识jemeter和第一个例子
- 5、第一个例子
- opengl 第一个例子
- hibernate第一个例子
- ajax 第一个例子
- JavaBeans第一个例子
- git push: error 401 Unauthorized while accessing
- 认识XXXServlet之 DispatcherServlet
- JavaScript运动框架(一):解决速度正负取整问题
- Grunt打包前端代码教程
- 在Android上使用OpenCL调用GPU加速
- CNTK入门01:初次使用和运行第一个例子
- 喷水装置1
- 前端开发浏览器兼容测试工具Lunascape
- Linux安装Nginx
- 程序员的修炼之道
- kvm中重命名虚拟机
- Ajax获取form表单提交数据
- fg/bg/jobs
- phpcms 后台登陆及版权修改问题