我用tensorflow实现RNN为什么在初始化变量的时候会invalid syntax
我是小白一个,刚刚开始学tensorflow,在我用tensorflow实现RNN的时候init = tf.global_variables_initializer()报错,好懵逼啊-- 求大神!! 感谢!!
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data',one_hot=True)
n_inputs = 28
max_time = 28
lstm_size = 100
batch_size = 50
n_batch = mnist.train.num_examples // batch_size
n_class = 10
x= tf.placeholder(tf.float32,[None,784])
y= tf.placeholder(tf.float32,[None,10])
weight = tf.Variable(tf.truncated_normal([lstm_size,n_class],stddev=0.1))###
bias = tf.Variable(tf.constant(0.1,shape=[n_class]))
def rnn(X,weight,bias):
#inputs = [batch_size,max_time,n_inputs]
inputs = tf.reshape(X,[-1,max_time,n_inputs])
lstm_cell = tf.contrib.rnn.BasicLSTMCell(lstm_size)
#final_state[0]是cell_state
#final_state[1]是hidden_state
outputs,final_state = tf.nn.dynamic_rnn(lstm_cell,inputs,dtype=tf.float32)
results = tf.nn.softmax(tf.matmul(final_state[1],weight) + bias)
return results
prediction = rnn(x,weight,bias)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
acc = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)
--> init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(6):
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images[:10000],y:mnist.train.labels[:10000]})
test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print('Iter: ' + str(i) + ' , Train_acc: ' + str(train_acc) + ' , Test_acc' + str(test_acc))
报错信息是
File "u8.py", line 47
init = tf.global_variables_initializer()
^
SyntaxError: invalid syntax
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是粗心了,在上一行代码中漏打一个)