📜  CNTK-创建第一个神经网络(1)

📅  最后修改于: 2023-12-03 14:40:07.515000             🧑  作者: Mango

CNTK-创建第一个神经网络

Microsoft Cognitive Toolkit(CNTK)是微软发布的一款深度学习框架,支持多GPU和分布式计算,具有高效、灵活、易用的特点。本文将介绍如何使用CNTK创建第一个神经网络。

环境准备

在开始之前,请确保您已经安装了以下软件:

  • Python 3.x
  • CNTK

您可以在CNTK的官方网站(https://cntk.ai/pythondocs/installation.html)上获得CNTK的安装教程。

数据集准备

我们将使用MNIST手写数字数据集来训练我们的神经网络。您可以通过以下代码从CNTK的数据库中下载该数据集:

from cntk.datasets import MNIST
import numpy as np

# Define the data dimensions
input_dim = 784
num_output_classes = 10

# Ensure the training and test data is only downloaded once
# Note: we pass arguments explicitly to force download to the data folder under working directory
train_data = MNIST('train-images-idx3-ubyte.gz', 'train-labels-idx1-ubyte.gz', 
                   os.path.join(os.getcwd(), "data", "MNIST"))
test_data = MNIST('t10k-images-idx3-ubyte.gz', 't10k-labels-idx1-ubyte.gz', 
                   os.path.join(os.getcwd(), "data", "MNIST"))

# Extract features and labels
# Convert to float32 for GPU compatibility
train_features = np.array(train_data.train_images, dtype=np.float32)
test_features = np.array(test_data.test_images, dtype=np.float32)
train_labels = np.array(train_data.train_labels, dtype=np.float32)
test_labels = np.array(test_data.test_labels, dtype=np.float32)

# Normalize features for better training
def normalize_features(features):
    mean = features.mean()
    std = features.std()
    return (features - mean) / std

train_features = normalize_features(train_features)
test_features = normalize_features(test_features)
创建模型

我们将使用一个全连接层的模型来训练我们的数据。以下代码创建了一个包含784个输入节点和10个输出节点的全连接层:

import cntk as C

# Define the input and output variables
input_var = C.input_variable(input_dim)
label_var = C.input_variable(num_output_classes)

# Define the model
z = C.layers.Dense(num_output_classes, activation=None)(input_var)
定义损失函数和优化器

我们将使用交叉熵作为我们的损失函数,并使用随机梯度下降作为我们的优化器。以下代码定义了我们的损失函数和优化器:

# Define the loss function and evaluation metric
loss = C.cross_entropy_with_softmax(z, label_var)
eval_error = C.classification_error(z, label_var)

# Define the optimizer
learning_rate = 0.001
lr_schedule = C.learning_rate_schedule(learning_rate, C.UnitType.minibatch)
learner = C.sgd(z.parameters, lr_schedule)
trainer = C.Trainer(z, (loss, eval_error), [learner])
训练模型

我们将使用小批量梯度下降来训练我们的模型。以下代码定义了我们的训练过程:

# Define the minibatch size and number of epochs
minibatch_size = 64
num_epochs = 10
num_minibatches = len(train_labels) // minibatch_size

# Train the model
for epoch in range(num_epochs):
    epoch_loss = 0
    epoch_metric = 0
    for i in range(num_minibatches):
        train_features_mb = train_features[i * minibatch_size:(i + 1) * minibatch_size]
        train_labels_mb = train_labels[i * minibatch_size:(i + 1) * minibatch_size]
        minibatch_loss, minibatch_metric, _ = trainer.train_minibatch({input_var: train_features_mb, label_var: train_labels_mb})
        epoch_loss += minibatch_loss
        epoch_metric += minibatch_metric
    print("Epoch: {}, Loss: {:.2f}, Error: {:.2f}%".format(epoch+1, epoch_loss/num_minibatches, epoch_metric/num_minibatches*100))
测试模型

我们将使用测试集来评估我们的模型。以下代码定义了我们的测试过程:

# Test the model
num_test_minibatches = len(test_labels) // minibatch_size
test_metric = 0
for i in range(num_test_minibatches):
    test_features_mb = test_features[i * minibatch_size:(i + 1) * minibatch_size]
    test_labels_mb = test_labels[i * minibatch_size:(i + 1) * minibatch_size]
    minibatch_metric = trainer.test_minibatch({input_var: test_features_mb, label_var: test_labels_mb})
    test_metric += minibatch_metric
print("Test Error: {:.2f}%".format(test_metric/num_test_minibatches*100))

到此为止,我们就成功地使用CNTK创建了我们的第一个神经网络。祝贺您!