Pytorch教程

PyTorch Convent进行序列处理

PyTorch Convent进行序列处理详细操作教程
在本章中,提出了一种替代方法,它依赖于跨两个序列的单个2D卷积神经网络。网络的每一层都根据到目前为止产生的输出序列重新编码源令牌。因此,类似注意的属性在整个网络中普遍存在。
在这里,将专注于使用数据集中包含的值创建具有特定池的顺序网络。此过程也最适用于“图像识别模块”。
Convent进行序列处理
以下步骤用于使用PyTorch创建带有Convent的序列处理模型 -
第1步
使用convent导入必要的模块以执行序列处理。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
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步
使用以下代码执行必要的操作以按相应的顺序创建模式 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
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步编译模型并在所提到的传统神经网络模型中拟合模式,如下所示 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
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])
产生的输出如下 -
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4