PyTorch – 使用Convents的序列处理

PyTorch – 使用Convents的序列处理

在这一章中,我们提出了一种替代方法,即依靠一个单一的二维卷积神经网络来处理两个序列。我们的网络的每一层都是在迄今为止产生的输出序列的基础上对源标记进行重新编码。因此,类似注意力的特性在整个网络中是普遍存在的。

在这里,我们将着重于 从数据集所包含的值中创建具有特定集合的序列网络。 这个过程也最好应用于 “图像识别模块”。

PyTorch - 使用溶剂的序列处理

以下步骤用于使用PyTorch与convents创建序列处理模型 −

第1步

导入必要的模块,以便使用 convents 进行序列处理。

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

第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)

第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])

产生的输出结果如下 −

PyTorch - 使用溶剂的序列处理

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程