官术网_书友最值得收藏!

How to do it...

  1. First, we install CNTK with pip as follows:
pip install https://cntk.ai/PythonWheel/GPU/cntk-2.2-cp35-cp35m-linux_x86_64.whl

Adjust the wheel file if necessary (see https://docs.microsoft.com/en-us/cognitive-toolkit/Setup-Linux-Python?tabs=cntkpy22). 

  1. After installing CNTK, we can import it into our Python environment:
import cntk
  1. Let's create some simple dummy data that we can use for training:
import numpy as np
x_input = np.array([[1,2,3,4,5]], np.float32)
y_input = np.array([[10]], np.float32)
  1. Next, we need to define the placeholders for the input data:
X = cntk.input_variable(5, np.float32)
y = cntk.input_variable(1, np.float32)
  1. With CNTK, it's straightforward to stack multiple layers. We stack a dense layer with 32 inputs on top of an output layer with 1 output:
from cntk.layers import Dense, Sequential
model = Sequential([Dense(32),
Dense(1)])(X)
  1. Next, we define the loss function:
loss = cntk.squared_error(model, y)
  1. Now, we are ready to finalize our model with an optimizer:
learning_rate = 0.001
trainer = cntk.Trainer(model, (loss), cntk.adagrad(model.parameters, learning_rate))
  1. Finally, we can train our model as follows:
for epoch in range(10):
trainer.train_minibatch({X: x_input, y: y_input})
As we have demonstrated in this introduction, it is straightforward to build models in CNTK with the appropriate high-level wrappers. However, just like TensorFlow and PyTorch, you can choose to implement your model on a more granular level, which gives you a lot of freedom.
主站蜘蛛池模板: 阳谷县| 蕉岭县| 顺义区| 塔河县| 太保市| 青龙| 永定县| 绥江县| 陇川县| 左权县| 淮安市| 常州市| 固阳县| 旅游| 宣化县| 平泉县| 江华| 宁武县| 静海县| 习水县| 莲花县| 侯马市| 隆化县| 勃利县| 西平县| 宁阳县| 庆阳市| 长岭县| 天长市| 瑞金市| 同心县| 达孜县| 辛集市| 潮州市| 吴川市| 陇西县| 嘉鱼县| 漳平市| 汪清县| 思茅市| 敖汉旗|