Pytorch 简明教程

PyTorch - Sequence Processing with Convents

在本章中,我们提出了一种替代方法,该方法依赖于跨两个序列的单个 2D 卷积神经网络。我们网络的每一层都根据到目前为止生成的输出序列重新编码源标记。因此,类似注意力的特性在整个网络中都是普遍存在的。

在这里,我们将重点关注 creating the sequential network with specific pooling from the values included in dataset 。此过程也最适用于“图像识别模块”。

sequential network

创建具有卷积的序列处理模型,按照以下步骤使用 PyTorch -

Step 1

导入执行序列处理所需的卷积模块。

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
import numpy as np

Step 2

按照以下代码执行必要的操作以创建相应序列中的模式 -

batch_size = 128
num_classes = 10
epochs = 12
# input image dimensions
img_rows, img_cols = 28, 28
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000,28,28,1)
x_test = x_test.reshape(10000,28,28,1)
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

Step 3

编译模型,并将模式拟合到如下所示的传统神经网络模型中 -

model.compile(loss =
keras.losses.categorical_crossentropy,
optimizer = keras.optimizers.Adadelta(), metrics =
['accuracy'])
model.fit(x_train, y_train,
batch_size = batch_size, epochs = epochs,
verbose = 1, validation_data = (x_test, y_test))
score = model.evaluate(x_test, y_test, verbose = 0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

输出如下 −

neural network model