tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode='CONSTANT')

tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode='CONSTANT')

tf.pad 是扩展的意思,其中[0, 0], [1, 0] 分别代表的是[上,下][左,右]  值为0代表相应边扩展0,比如上面代码中,左的位置的值为0,代表在左边增加一列,填充是mode='CONSTANT',代表用0填充,具体见:

https://www.tensorflow.org/api_docs/python/tf/pad


# 't' is [[1, 2, 3], [4, 5, 6]].
# 'paddings' is [[1, 1,], [2, 2]].
# 'constant_values' is 0.
# rank of 't' is 2.
pad(t, paddings, "CONSTANT") ==> [[0, 0, 0, 0, 0, 0, 0],
                                  [0, 0, 1, 2, 3, 0, 0],
                                  [0, 0, 4, 5, 6, 0, 0],
                                  [0, 0, 0, 0, 0, 0, 0]]

pad(t, paddings, "REFLECT") ==> [[6, 5, 4, 5, 6, 5, 4],
                                 [3, 2, 1, 2, 3, 2, 1],
                                 [6, 5, 4, 5, 6, 5, 4],
                                 [3, 2, 1, 2, 3, 2, 1]]pad(t, paddings, "SYMMETRIC") ==> [[2, 1, 1, 2, 3, 3, 2], 
                                 [2, 1, 1, 2, 3, 3, 2],
                                   [5, 4, 4, 5, 6, 6, 5],
                                   [5, 4, 4, 5, 6, 6, 5]]

转载于:https://www.cnblogs.com/YouXiangLiThon/p/7738259.html

对下面代码进行改错 import tensorflow.compat.v1 as tf tf.compat.v1.disable_eager_execution() from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('MNIST_data', one_hot=True) num_classes = 10 input_size = 784 hidden_units_size = 30 batch_size = 100 training_iterations = 10000 X = tf.placeholder(tf.float32, [None, input_size]) Y = tf.placeholder(tf.float32, [None, num_classes]) W1 = tf.Variable(tf.random_normal([input_size, hidden_units_size],stddev = 0.1)) B1 = tf.Variable(tf.constant([hidden_units_size])) W2 = tf.Variable(tf.random_normal ([hidden_units_size,num_classes],stddev = 0.1)) B2 = tf.Variable(tf.constant(0.1), [num_classes]) hidden_opt = tf.matmul(X, W1) + B1 hidden_opt = tf.nn.relu(hidden_opt) final_opt = tf.matmul(hidden_opt, W2) + B2 final_opt = tf.nn.relu(final_opt) loss1 = tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=final_opt) loss = tf.reduce_mean(loss1) opt = tf.train.GradientDescentOptimizer(0.05).minimize(loss) init = tf.global_variables_initializer() correct_prediction = tf.equal(tf.argmax(Y,1), tf.argmax(final_opt,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) sess = tf.Session() sess.run(init) for i in range(training_iterations): batch = mnist.train.next_batch(batch_size) batch_input = batch[0] batch_labels = batch[1] train_loss = sess.run([opt, loss], feed_dict={X: batch_input, Y: batch_labels}) if i % 100 == 0: train_accuracy = accuracy.eval (session = sess, feed_dict={X: batch_input, Y: batch_labels}) print("step %d, training accuracy %g" % (i, train_accuracy))
最新发布
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值