【发布时间】:2017-02-26 17:42:00
【问题描述】:
我对@987654321@ - TensorFlow 中的神经网络有一些疑问。
#!/usr/bin/env python
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
def init_weights(shape):
return tf.Variable(tf.random_normal(shape, stddev=0.01))
def model(X, w_h, w_o):
h = tf.nn.sigmoid(tf.matmul(X, w_h)) # this is a basic mlp, think 2 stacked logistic regressions
return tf.matmul(h, w_o) # note that we dont take the softmax at the end because our cost fn does that for us
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels
X = tf.placeholder("float", [None, 784])
Y = tf.placeholder("float", [None, 10])
w_h = init_weights([784, 625]) # create symbolic variables
w_o = init_weights([625, 10])
py_x = model(X, w_h, w_o)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y)) # compute costs
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) # construct an optimizer
predict_op = tf.argmax(py_x, 1)
# Launch the graph in a session
with tf.Session() as sess:
# you need to initialize all variables
tf.global_variables_initializer().run()
for i in range(100):
for start, end in zip(range(0, len(trX), 128), range(128, len(trX)+1, 128)):
sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})
print(i, np.mean(np.argmax(teY, axis=1) ==
sess.run(predict_op, feed_dict={X: teX})))
在第 37 行单次运行循环后,我如何使用 X[0] 和新学习的 调用 model() w_h 和 w_o ,以便我可以看到函数返回
同样,如何在 model() 函数中打印 h 的值?
提前致谢。我是 tensorFlow 的新手 :)
【问题讨论】:
-
在第 37 行的每个循环之后,在第 40 行调用模型。 X[0] 没有任何意义,因为 X 只是一个占位符。第 41 行中的 teX 实现了 X 的目的
-
只是为了澄清这一点:'model is being used' 可能更准确,因为函数
model()只被调用一次。
标签: python tensorflow neural-network