2018 年 6 月

# 測試回合-使用 CNTK 的類神經迴歸

James McCaffrey |2018 年 6 月

## 了解的資料

``````-2.3 0.568 4.78 3.99 3.17 0.125 0.11
-2.3 0.568 4.78 3.99 3.17 0.150 0.27
...
-5.0 0.530 4.78 3.75 3.15 0.125 0.09
...
-2.3 0.600 4.34 4.23 2.73 0.450 46.66
``````

``````|predictors  0.540000  0.542857 . . |resistance  0.001602
|predictors  0.540000  0.542857 . . |resistance  0.004166
...
``````

## 示範程式

``````# hydro_reg.py
# CNTK 2.4 with Anaconda 4.1.1 (Python 3.5, NumPy 1.11.1)
# Predict yacht hull resistance based on six predictors

import numpy as np
import cntk as C

sweeps):
x_strm = C.io.StreamDef(field='predictors',
shape=input_dim, is_sparse=False)
y_strm = C.io.StreamDef(field='resistance',
shape=output_dim, is_sparse=False)
streams = C.io.StreamDefs(x_src=x_strm, y_src=y_strm)
deserial = C.io.CTFDeserializer(path, streams)
mb_src = C.io.MinibatchSource(deserial,
randomize=rnd_order, max_sweeps=sweeps)
return mb_src

# ========================================================

def main():
print("\nBegin yacht hull regression \n")
print("Using CNTK version = " + \
str(C.__version__) + "\n")
input_dim = 6  # center of buoyancy, etc.
hidden_dim = 5
output_dim = 1  # residuary resistance
train_file = ".\\Data\\hydro_data_cntk.txt"
# data resembles:
# |predictors 0.540  0.542 . . |resistance  0.001
# |predictors 0.540  0.542 . . |resistance  0.004
# 1. create neural network model
X = C.ops.input_variable(input_dim, np.float32)
Y = C.ops.input_variable(output_dim)
print("Creating a 6-(5-5)-1 tanh regression NN for \
yacht hull dataset ")
with C.layers.default_options():
hLayer1 = C.layers.Dense(hidden_dim,
activation=C.ops.tanh, name='hidLayer1')(X)
hLayer2 = C.layers.Dense(hidden_dim,
activation=C.ops.tanh, name='hidLayer2')(hLayer1)
oLayer = C.layers.Dense(output_dim,
activation=None, name='outLayer')(hLayer2)
model = C.ops.alias(oLayer)  # alias
# 2. create learner and trainer
print("Creating a squared error batch=11 Adam \
fixed LR=0.005 Trainer \n")
tr_loss = C.squared_error(model, Y)
max_iter = 50000
batch_size = 11
learn_rate = 0.005
trainer = C.Trainer(model, (tr_loss), [learner])
# 3. create reader for train data
rnd_order=True, sweeps=C.io.INFINITELY_REPEAT)
hydro_input_map = {
X : rdr.streams.x_src,
Y : rdr.streams.y_src
}
# 4. train
print("Starting training \n")
for i in range(0, max_iter):
curr_batch = rdr.next_minibatch(batch_size,
input_map=hydro_input_map)
trainer.train_minibatch(curr_batch)
if i % int(max_iter/10) == 0:
mcee = trainer.previous_minibatch_loss_average
print("batch %6d: mean squared error = %8.4f" % \
(i, mcee))
print("\nTraining complete")
# (could save model to disk here)
# 5. use trained model to make some predictions
np.set_printoptions(precision=2, suppress=True)
inpts = np.array(
[[0.520000, 0.785714, 0.550000, 0.405512, \
0.648352, 0.000000],
[1.000000, 1.000000, 0.550000, 0.562992, \
0.461538, 1.000000]],
dtype=np.float32)
actuals = np.array([0.003044, 0.825028],
dtype=np.float32)
for i in range(len(inpts)):
print("\nInput: ", inpts[i])
pred = model.eval(inpts[i])
print("predicted resistance: %0.4f" % pred[0][0])
print("actual resistance:    %0.4f" % actuals[i])
print("\nEnd yacht hull regression ")

# ========================================================

if __name__ == "__main__":
main()
``````

``````>pip install https://cntk.ai/PythonWheel/CPU-Only/cntk-2.4-cp35-cp35m-win_amd64.whl
``````

``````def main():
print("Begin yacht hull regression \n")
print("Using CNTK version = " + \
str(C.__version__) + "\n")
input_dim = 6  # center of buoyancy, etc.
hidden_dim = 5
output_dim = 1  # residuary resistance
train_file = ".\\Data\\hydro_data_cntk.txt"
...
``````

## 建立類神經網路模型

``````X = C.ops.input_variable(input_dim, np.float32)
Y = C.ops.input_variable(output_dim)
``````

``````print("Creating a 6-(5-5)-1 NN")
with C.layers.default_options():
hLayer1 = C.layers.Dense(hidden_dim,
activation=C.ops.tanh, name='hidLayer1')(X)
hLayer2 = C.layers.Dense(hidden_dim,
activation=C.ops.tanh, name='hidLayer2')(hLayer1)
oLayer = C.layers.Dense(output_dim,
activation=None, name='outLayer')(hLayer2)
model = C.ops.alias(oLayer)  # alias
``````

## 定型模型

CNTK 功能的核心是定型類神經網路模型的能力。定型會由這些陳述式：

``````tr_loss = C.squared_error(model, Y)
max_iter = 50000
batch_size = 11
learn_rate = 0.005
trainer = C.Trainer(model, (tr_loss), [learner])
``````

``````rdr = create_reader(train_file, input_dim, output_dim,
rnd_order=True, sweeps=C.io.INFINITELY_REPEAT)
hydro_input_map = {
X : rdr.streams.x_src,
Y : rdr.streams.y_src
}
``````

Rnd_order 參數，可確保將會以不同方式處理項目，在每個階段，請務必防止訓練出一的資料。INFINITELY_REPEAT 引數會允許透過 308 項目資料集的多個傳遞的訓練。

``````for i in range(0, max_iter):
curr_batch = rdr.next_minibatch(batch_size,
input_map=hydro_input_map)
trainer.train_minibatch(curr_batch)
if i % int(max_iter/10) == 0:
mcee = trainer.previous_minibatch_loss_average
print("batch %6d: mean squared error = %8.4f" % \
(i, mcee))
``````

Next_minibatch 函式會提取 11 資料中的項目。定型函式會使用 Adam 演算法，來更新加權和徵才根據計算的輪廓抵抗值與實際的防禦功能值之間的平方誤差的偏差。目前的 11 個項目的批次的平方的錯誤會顯示每個 50000 / 10 = 5,000 個批次，所以您以視覺化方式可以監視訓練進度：您想要查看通常減少的遺失/錯誤值。

## 使用模型

``````inpts = np.array(
[[0.520000, 0.785714, 0.550000, 0.405512,
0.648352, 0.000000],
[1.000000, 1.000000, 0.550000, 0.562992,
0.461538, 1.000000]],
dtype=np.float32)
``````

``````actuals = np.array([0.003044, 0.825028], dtype=np.float32)
``````

``````for i in range(len(inpts)):
print("\nInput: ", inpts[i])
pred = model.eval(inpts[i])
print("predicted resistance: %0.4f" % pred[0][0])
print("actual resistance:    %0.4f" % actuals[i])
print("End yacht hull regression ")
``````

## 總結

Dr。James McCaffrey 適用於 Microsoft Research Redmond，Wash.他已投入許多 Microsoft 產品，包括 Internet Explorer 和 Bing。Dr。在可到達 McCaffrey jamccaff@microsoft.com