CNTK-训练神经网络

本文详细介绍了在CNTK中训练神经网络的过程,包括选择学习者、配置训练,加载数据集,将数据输入训练师,以及衡量模型性能和进行预测的步骤。文章通过具体的示例展示了如何使用Stochastic Gradient Descent (SGD)学习器进行训练,并讨论了防止过拟合的方法。
摘要由CSDN通过智能技术生成

CNTK-训练神经网络 (CNTK - Training the Neural Network)

Here, we will understand about training the Neural Network in CNTK.

在这里,我们将了解有关在CNTK中训练神经网络的信息。

在CNTK中训练模型 (Training a model in CNTK)

In the previous section, we have defined all the components for the deep learning model. Now it is time to train it. As we discussed earlier, we can train a NN model in CNTK using the combination of learner and trainer.

在上一节中,我们定义了深度学习模型的所有组件。 现在该训练它了。 如前所述,我们可以使用学习者训练者的组合训练 CNTK中的NN模型。

选择一个学习者并进行培训 (Choosing a learner and setting up training)

In this section, we will be defining the learner. CNTK provides several learners to choose from. For our model, defined in previous sections, we will be using Stochastic Gradient Descent (SGD) learner.

在本节中,我们将定义学习者 。 CNTK提供了一些学习者供您选择。 对于前面几节中定义的模型,我们将使用随机梯度下降(SGD)学习器

In order to train the neural network, let us configure the learner and trainer with the help of following steps −

为了训练神经网络,让我们在以下步骤的帮助下配置学习者训练者 -

Step 1 − First, we need to import sgd function from cntk.lerners package.

步骤1-首先,我们需要从cntk.lerners包中导入sgd函数。


from cntk.learners import sgd

Step 2 − Next, we need to import Trainer function from cntk.train.trainer package.

步骤2-接下来,我们需要从cntk.train .trainer包中导入Trainer函数。


from cntk.train.trainer import Trainer

Step 3 − Now, we need to create a learner. It can be created by invoking sgd function along with providing model’s parameters and a value for the learning rate.

步骤3-现在,我们需要创建一个学习者 。 可以通过调用sgd函数以及提供模型的参数和学习率值来创建它。


learner = sgd(z.parametrs, 0.01)

Step 4 − At last, we need to initialize the trainer. It must be provided the network, the combination of the loss and metric along with the learner.

步骤4-最后,我们需要初始化Trainer 。 它必须与学习者一起提供网络, 损失度量的组合。


trainer = Trainer(z, (loss, error_rate), [learner])

The learning rate which controls the speed of optimisation should be small number between 0.1 to 0.001.

控制优化速度的学习率应在0.1到0.001之间。

选择学习者并设置培训-完整的示例 (Choosing a learner and setting up the training - Complete example)


from cntk.learners import sgd
from cntk.train.trainer import Trainer
learner = sgd(z.parametrs, 0.01)
trainer = Trainer(z, (loss, error_rate), [learner])

将数据输入培训师 (Feeding data into the trainer)

Once we chose and configured the trainer, it is time to load the dataset. We have saved the iris dataset as a .CSV file and we will be using data wrangling package named pandas to load the dataset.

一旦我们选择并配置了培训者,就该加载数据集了。 我们将虹膜数据集另存为。 CSV文件,我们将使用名为pandas的数据整理包来加载数据集。

从.CSV文件加载数据集的步骤 (Steps to load the dataset from .CSV file)

Step 1 − First, we need to import the pandas package.

步骤1-首先,我们需要导入pandas包。


from import pandas as pd

Step 2 − Now, we need to invoke the function named read_csv function to load the .csv file from the disk.

步骤2-现在,我们需要调用名为read_csv函数的函数以从磁盘加载.csv文件。


df_source = pd.read_csv(‘iris.csv’, names = [‘sepal_length’, ‘sepal_width’, 
‘petal_length’, ‘petal_width’, index_col=False)

Once we load the dataset, we need to split it into a set of features and a label.

加载数据集后,我们需要将其拆分为一组要素和一个标签。

将数据集分为特征和标签的步骤 (Steps to split the dataset into features and label)

Step 1 − First, we need to select all rows and first four columns from the dataset. It can be done by using iloc function.

步骤1-首先,我们需要从数据集中选择所有行和前四列。 可以通过使用iloc函数来完成。


x = df_source.iloc[:, :4].values

Step 2 − Next we need to select the species column from iris dataset. We will be using the values property to access the underlying numpy array.

步骤2-接下来,我们需要从虹膜数据集中选择物种列。 我们将使用values属性访问基础的numpy数组。


x = df_source[‘species’].values

将种类列编码为数字矢量表示的步骤 (Steps to encode the species column to a numeric vector representation)

As we discussed earlier, our model is based on classification, it requires numeric input values. Hence, here we need to encode the species column to a numeric vector representation. Let’s see the steps to do it −

如前所述,我们的模型基于分类,它需要数字输入值。 因此,这里我们需要将种类列编码为数字矢量表示。 让我们看看执行此操作的步骤-

Step 1 − First, we need to create a list expression to iterate over all elements in the array. Then perform a look up in the label_mapping dictionary for each value.

步骤1-首先,我们需要创建一个列表表达式来遍历数组中的所有元素。 然后在label_mapping字典中对每个值进行查找。


label_mapping = {‘Iris-Setosa’ : 0, ‘Iris-Versicolor’ : 1, ‘Iris-Virginica’ : 2}

Step 2 − Next, convert this converted numeric value to a one-hot encoded vector. We will be using one_hot function as follows −

步骤2-接下来,将此转换后的数值转换为单次编码的矢量。 我们将使用one_hot函数,如下所示:


def one_hot(index, length):
result = np.zeros(length)
result[index] = 1
return result

Step 3 − At last, we need to turn this converted list into a numpy array.

步骤3-最后,我们需要将转换后的列表转换为一个numpy数组。


y = np.array([one_hot(label_mapping[v], 3) for v in y])

检测过度拟合的步骤 (Steps to detect overfitting)

The situation, when your model remembers samples but can’t deduce rules from the training samples, is overfitting. With the help of following steps, we can detect overfitting on our model −

当您的模型记住样本但无法从训练样本中得出规则时,情况就变得过拟合了。 借助以下步骤,我们可以检测模型的过度拟合-

Step 1 − First, from sklearn package, import the train_test_split function from the model_selection module.

步骤1-首先,从sklearn包中,从model_selection模块导入train_test_split函数。


from sklearn.model_selection import train_test_split

Step 2 − Next, we need to invoke the train_test_split function with features x and labels y as follows −

步骤2-接下来,我们需要调用带有特征x和标签y的train_test_split函数,如下所示:


x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0-2, 
stratify=y)

We specified a test_size of 0.2 to set aside 20% of total data.

我们将test_size指定为0.2以预留总数据的20%。


label_mapping = {‘Iris-Setosa’ : 0, ‘Iris-Versicolor’ : 1, ‘Iris-Virginica’ : 2}

向模型提供训练集和验证集的步骤 (Steps to feed training set and validation set to our model)

Step 1 − In order to train our model, first, we will be invoking the train_minibatch method. Then give it a dictionary that maps the input data to the input variable that we have used to define the NN and its associated loss function.

步骤1-为了训练我们的模型,首先,我们将调用train_minibatch方法。 然后给它一个字典,将输入数据映射到我们用来定义NN及其相关损失函数的输入变量。


trainer.train_minibatch({ features: X_train, label: y_train})

Step 2 − Next, call train_minibatch by using the following for loop −

步骤2-接下来,使用以下for循环调用train_minibatch-


for _epoch in range(10):
trainer.train_minbatch ({ feature: X_train, label: y_train})
print(‘Loss: {}, Acc: {}’.format(
trainer.previous_minibatch_loss_average,
trainer.previous_minibatch_evaluation_average))

将数据输入培训师-完整示例 (Feeding data into the trainer - Complete example)


from import pandas as pd
df_source = pd.read_csv(‘iris.csv’, names = [‘sepal_length’, ‘sepal_width’, ‘petal_length’, ‘petal_width’, index_col=False)
x = df_source.iloc[:, :4].values
x = df_source[‘species’].values
label_mapping = {‘Iris-Setosa’ : 0, ‘Iris-Versicolor’ : 1, ‘Iris-Virginica’ : 2}
def one_hot(index, length):
result = np.zeros(length)
result[index] = 1
return result
y = np.array([one_hot(label_mapping[v], 3) for v in y])
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0-2, stratify=y)
label_mapping = {‘Iris-Setosa’ : 0, ‘Iris-Versicolor’ : 1, ‘Iris-Virginica’ : 2}
trainer.train_minibatch({ features: X_train, label: y_train})
for _epoch in range(10):
trainer.train_minbatch ({ feature: X_train, label: y_train})
print(‘Loss: {}, Acc: {}’.format(
trainer.previous_minibatch_loss_average,
trainer.previous_minibatch_evaluation_average))

衡量NN的性能 (Measuring the performance of NN)

In order to optimise our NN model, whenever we pass data through the trainer, it measures the performance of the model through the metric that we configured for trainer. Such measurement of performance of NN model during training is on training data. But on the other hand, for a full analysis of the model performance we need to use test data as well.

为了优化我们的NN模型,每当我们通过培训员传递数据时,它都会通过为培训员配置的指标来衡量模型的性能。 训练期间神经网络模型性能的这种测量是在训练数据上进行的。 但另一方面,要对模型性能进行全面分析,我们还需要使用测试数据。

So, to measure the performance of the model using the test data, we can invoke the test_minibatch method on the trainer as follows −

因此,要使用测试数据衡量模型的性能,我们可以在训练器上调用test_minibatch方法,如下所示:


trainer.test_minibatch({ features: X_test, label: y_test})

用NN进行预测 (Making prediction with NN)

Once you trained a deep learning model, the most important thing is to make predictions using that. In order to make prediction from the above trained NN, we can follow the given steps−

训练了深度学习模型后,最重要的是使用该模型进行预测。 为了从上面训练有素的NN进行预测,我们可以按照给定的步骤进行操作-

Step 1 − First, we need to pick a random item from the test set using the following function −

步骤1-首先,我们需要使用以下函数从测试集中选择一个随机项目-


np.random.choice

Step 2 − Next, we need to select the sample data from the test set by using sample_index.

步骤2-接下来,我们需要使用sample_index从测试集中选择样本数据。

Step 3 − Now, in order to convert the numeric output to the NN to an actual label, create an inverted mapping.

步骤3-现在,为了将数字输出转换为NN并转换为实际标签,请创建一个反向映射。

Step 4 − Now, use the selected sample data. Make a prediction by invoking the NN z as a function.

步骤4-现在,使用选定的样本数据。 通过调用NN z进行预测。

Step 5 − Now, once you got the predicted output, take the index of the neuron that has the highest value as the predicted value. It can be done by using the np.argmax function from the numpy package.

步骤5-现在,一旦获得了预测输出,就将具有最高值的神经元的索引作为预测值。 可以使用numpy包中的np.argmax函数来完成。

Step 6 − At last, convert the index value into the real label by using inverted_mapping.

第6步 -最后,通过使用reverse_mapping将索引值转换为实数标签。

用NN进行预测-完整示例 (Making prediction with NN - Complete example)


sample_index = np.random.choice(X_test.shape[0])
sample = X_test[sample_index]
inverted_mapping = {
   1:’Iris-setosa’,
   2:’Iris-versicolor’,
   3:’Iris-virginica’
}
prediction = z(sample)
predicted_label = inverted_mapping[np.argmax(prediction)]
print(predicted_label)

输出量 (Output)

After training the above deep learning model and running it, you will get the following output −

在训练了上面的深度学习模型并运行它之后,您将获得以下输出:


Iris-versicolor

翻译自: https://www.tutorialspoint.com/microsoft_cognitive_toolkit/microsoft_cognitive_toolkit_training_the_neural_network.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值