环境为windows10+python3+tensorflow,如无法运行,可能是tf版本更新发生变化。
训练文件train.py:
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) #导入数据
lr=0.001#学习效率
training_iters=500000 #训练次数
batch_size=128 #每次抓取的数据量
n_inputs=28 #28列
n_steps=28 #28行
n_hidden_unis=128 #隐藏层单元个数
n_classes=10 #0-9
#进行网络结构的建立
with tf.name_scope('inputs'): #input
x = tf.placeholder(tf.float32,[None,n_steps,n_inputs],name='x_inputs') #得到传递进来的训练样本 图片
y = tf.placeholder(tf.float32,[None,n_classes],name='y_inputs') #标签
weights=\
{
'in':tf.Variable(tf.random_normal([n_inputs,n_hidden_unis]),name='weights'), #从样本进行输入
'out':tf.Variable(tf.random_normal([n_hidden_unis,n_classes]),name='weights1') #输出至隐藏层
}
biases=\
{ #偏置
'in':tf.Variable(tf.constant(0.1,shape=[n_hidden_unis,]),name='biases'), #(128,)
'out':tf.Variable(tf.constant(0.1,shape=[n_classes,]),name='biases1') #(10,)
}
def RNN(X,weights,biases):#X(128batch,28steps,28inputs)
with tf.name_scope('layer'):
X = tf.reshape(X,[-1,n_inputs]) #重置为128*28,28的向量
X_in = tf.matmul(X,weights['in'])