[英]TensorFlow example but with middle layer
I am trying to get this code to work. 我正在尝试使此代码正常工作。 It may not look like it, but it comes mostly from the TensorFlow mnist example.
它可能看起来不像,但主要来自TensorFlow mnist示例。 I am trying to get three layers, though, and I've changed the input and output size.
不过,我尝试获得三层,并且更改了输入和输出大小。 The input size is 12, the mid size is 6, and the output size is 2. This is what happens when I run this.
输入大小为12,中间大小为6,输出大小为2。这就是我运行此代码时发生的情况。 It does not throw an error, but when I run the test option I always get 50%.
它不会引发错误,但是当我运行测试选项时,我总是得到50%。 When I go back to training it runs and I am sure the weights are changing.
当我回到训练中时,它就会运行,并且我可以确定重量正在变化。 There is code for saving the model and weights, so I'm pretty confident it's not wiping out my weights every time I re-start it.
有用于保存模型和权重的代码,因此我非常有信心每次重新启动时都不会消除权重。 The idea behind self.d_y_out is to have something that will allow me to run the model and get output for just one image.
self.d_y_out背后的想法是让我可以运行模型并仅获取一张图像的输出。 I think the problem is near the comment that says "PROBLEM??".
我认为问题就在说“问题??”的评论附近。
self.d_keep = tf.placeholder(tf.float32)
self.d_W_2 = tf.Variable(tf.random_normal([mid_num, output_num], stddev=0.0001))
self.d_b_2 = tf.Variable(tf.random_normal([output_num], stddev=0.5))
self.d_x = tf.placeholder(tf.float32, [None, input_num])
self.d_W_1 = tf.Variable(tf.random_normal([input_num, mid_num], stddev=0.0001)) # 0.0004
self.d_b_1 = tf.Variable(tf.zeros([mid_num]))
self.d_y_ = tf.placeholder(tf.float32, [None, output_num])
self.d_x_drop = tf.nn.dropout(self.d_x, self.d_keep)
self.d_y_logits_1 = tf.matmul(self.d_x_drop, self.d_W_1) + self.d_b_1
self.d_y_mid = tf.nn.relu(self.d_y_logits_1)
self.d_y_mid_drop = tf.nn.dropout(self.d_y_mid, self.d_keep)
self.d_y_logits_2 = tf.matmul(self.d_y_mid_drop, self.d_W_2) + self.d_b_2
self.d_y_softmax = tf.nn.softmax_cross_entropy_with_logits(logits=self.d_y_logits_2, labels=self.d_y_)
self.d_cross_entropy = tf.reduce_mean(self.d_y_softmax) ## PROBLEM??
self.d_train_step = tf.train.GradientDescentOptimizer(0.001).minimize(self.d_cross_entropy) # 0.0001
# train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) #0.5
#self.d_y_out = tf.argmax(self.d_y, 1) ## for prediction
self.d_y_out = tf.argmax(self.d_y_logits_2, 1, name="d_y_out")
if self.train :
for i in range(self.start_train,self.cursor_tot): #1000
batch_xs, batch_ys = self.get_nn_next_train(self.batchsize)
self.sess.run(self.d_train_step, feed_dict={self.d_x: batch_xs, self.d_y_: batch_ys, self.d_keep: 0.5})
if True: #mid_num > 0:
cost = self.sess.run([self.d_cross_entropy, self.d_train_step],
feed_dict={self.d_x: batch_xs, self.d_y_: batch_ys, self.d_keep: 0.5})
print cost[0], "cost"
if self.test :
d_correct_prediction = tf.equal(self.d_y_out, tf.argmax(self.d_y_,1))
#d_correct_prediction = tf.equal(tf.argmax(self.d_y , 1), tf.argmax(self.d_y_, 1))
d_accuracy = tf.reduce_mean(tf.cast(d_correct_prediction, tf.float32))
if self.use_loader : self.get_nn_next_test(self.batchsize)
print(self.sess.run([d_accuracy, self.d_cross_entropy],
feed_dict={self.d_x: self.mnist_test.images, self.d_y_: self.mnist_test.labels, self.d_keep: 1.0}))
if self.predict_dot :
for i in range(start, stop ) :
batch_0, batch_1 = self.get_nn_next_predict(self.batchsize)
if len(batch_0) > 0 :
out.extend( self.sess.run([self.d_y_out, self.d_cross_entropy],
feed_dict={self.d_x : batch_0, self.d_y_: batch_1, self.d_keep: 1.0})[0])
print "out" , len(out) , i, self.cursor_tot, out[:10],"..."
EDIT I've edited the code in this question significantly. 编辑我已经对这个问题中的代码进行了重大编辑。 Much thanks to vijay m for getting me this far.
非常感谢vijay m使我走了这么远。 Any help would be appreciated.
任何帮助,将不胜感激。 Thanks.
谢谢。
The problem in this code is you calling dropout
on the inputs. 这段代码中的问题是您在输入上调用了
dropout
。 Yours is a single layer network and you don't need dropout
. 您的网络是单层的,不需要
dropout
。 And use a momentum optimizer like Adam
for training faster. 并使用
Adam
类的动量优化器进行更快的训练。 The changes i made: 我所做的更改:
d_y_logits_1 = tf.matmul(d_x, d_W_1) + d_b_1
d_y_mid = tf.nn.relu(d_y_logits_1)
d_y_logits_2 = tf.matmul(d_y_mid, d_W_2) + d_b_2
d_y_softmax = tf.nn.softmax_cross_entropy_with_logits(logits=d_y_logits_2, labels=d_y_)
d_cross_entropy = tf.reduce_mean(d_y_softmax)
d_train_step = tf.train.AdamOptimizer(0.01).minimize(d_cross_entropy)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.