c语言sscanf函数的用法是什么
257
2022-11-15
tensorflow RNN
import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_dataimport numpy as npimport matplotlib.pyplot as plttf.set_random_seed(1)np.random.seed(1)# Hyper ParametersBATCH_SIZE = 64TIME_STEP = 28 # rnn time step / image heightINPUT_SIZE = 28 # rnn input size / image widthLR = 0.01 # learning rate# datamnist = input_data.read_data_sets('MNIST_data', one_hot=True) # they has been normalized to range (0,1)test_x = mnist.test.images[:2000]test_y = mnist.test.labels[:2000]# plot one exampleprint(mnist.train.images.shape) # (55000, 28 * 28)print(mnist.train.labels.shape) # (55000, 10)# plt.imshow(mnist.train.images[0].reshape((28, 28)), cmap='gray')# plt.title('%i' % np.argmax(mnist.train.labels[0]))# plt.show()# tensorflow placeholderstf_x = tf.placeholder(tf.float32, [None, TIME_STEP * INPUT_SIZE]) # shape(batch, 784)image = tf.reshape(tf_x, [-1, TIME_STEP, INPUT_SIZE]) # (batch, height, width, channel)tf_y = tf.placeholder(tf.int32, [None, 10]) # input y# RNNrnn_cell = tf.contrib.rnn.BasicLSTMCell(num_units=64)outputs, (h_c, h_n) = tf.nn.dynamic_rnn( rnn_cell, # cell you have chosen image, # input initial_state=None, # the initial hidden state dtype=tf.float32, # must given if set initial_state = None time_major=False, # False: (batch, time step, input); True: (time step, batch, input))output = tf.layers.dense(outputs[:, -1, :], 10) # output based on the last output steploss = tf.losses.softmax_cross_entropy(onehot_labels=tf_y, logits=output) # compute costtrain_op = tf.train.AdamOptimizer(LR).minimize(loss)accuracy = tf.metrics.accuracy( # return (acc, update_op), and create 2 local variables labels=tf.argmax(tf_y, axis=1), predictions=tf.argmax(output, axis=1),)[1]sess = tf.Session()init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) # the local var is for accuracy_opsess.run(init_op) # initialize var in graphfor step in range(1200): # training b_x, b_y = mnist.train.next_batch(BATCH_SIZE) _, loss_ = sess.run([train_op, loss], {tf_x: b_x, tf_y: b_y}) if step % 50 == 0: # testing accuracy_ = sess.run(accuracy, {tf_x: test_x, tf_y: test_y}) print('train loss: %.4f' % loss_, '| test accuracy: %.2f' % accuracy_)# print 10 predictions from test datatest_output = sess.run(output, {tf_x: test_x[:10]})pred_y = np.argmax(test_output, 1)print(pred_y, 'prediction number')print(np.argmax(test_y[:10], 1), 'real number')
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~