【问题标题】:executing function in TensorFlow在 TensorFlow 中执行函数
【发布时间】:2017-02-26 17:42:00
【问题描述】:

我对@9​​87654321@ - 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_hw_o ,以便我可以看到函数返回

  • 同样,如何在 model() 函数中打印 h 的值?

提前致谢。我是 tensorFlow 的新手 :)

【问题讨论】:

  • 在第 37 行的每个循环之后,在第 40 行调用模型。 X[0] 没有任何意义,因为 X 只是一个占位符。第 41 行中的 teX 实现了 X 的目的
  • 只是为了澄清这一点:'model is being used' 可能更准确,因为函数 model() 只被调用一次。

标签: python tensorflow neural-network


【解决方案1】:

feed_dict 将占位符转换为实际值。因此,为feed_dicts 提供一个条目并评估py_x

以下应该有效:

对于结果(px_y):

print(sess.run(py_x, feed_dict={X: [yoursample]}))

对于h,它(几乎)是一样的。但是在链接代码中hmodel() 的私有成员,您需要引用h 才能对其进行评估。最简单的方法很可能是替换行:

(14) return tf.matmul(h, w_o)
with
(14) return (tf.matmul(h, w_o), h)

(26) py_x = model(X, w_h, w_o)
with
(26) py_x, h = model(X, w_h, w_o)

并使用:

print(sess.run(h, feed_dict={X: [yoursample]}))

或者(评估多个变量):

py_val, h_val = sess.run([py_x, h], feed_dict={X: [yoursample]})
print(py_val, h)

解释: 顺便说一下,我们告诉 Tensorflow 我们的网络是如何构建的,我们不需要显式引用(内部/隐藏)变量h。但是为了评估它,我们确实需要引用来定义究竟要评估的内容。

还有其他方法可以将变量从 Tensorflow 中取出,但是当我们在上面几行明确创建这个变量时,我会避免将某些东西放入黑盒中,然后再要求同一个黑盒给出回来了。

【讨论】:

  • 谢谢。第二个要点呢?是否应该编写另一个只返回 h 的函数?或者有更好的方法来做到这一点?
  • 因为这个问题对其他人来说可能也很有趣,所以我也讨论了这个问题并添加了一些 cmets。
  • 谢谢,得到了我想要的 :) 你能推荐一些 TensorFlow 的好教程吗,理解他们的文档有点困难。再次感谢。
  • 确实是这样。就我个人而言,我开始尝试理解他们的教程,但没有成功,因此我使用他们每个教程的最终代码来进行乒乓球尝试并理解他们的工作和解释。我喜欢 MNIST 示例,因为(书面数字的)图像更易于可视化、重现和理解。
【解决方案2】:

关于第二个问题:

  • 同样,如何在model()函数中打印h的值?

使用函数tf.Print

def model(X, w_h, w_o):
    print_h = tf.nn.sigmoid(tf.matmul(X, w_h)) # this is a basic mlp, think 2 stacked logistic regressions
    h = tf.Print(print_h,[print_h]) # add a node to the execution graph that prints h when ever h is needed and executed in the graph
    return tf.matmul(h, w_o) # note that we dont take the softmax at the end because our cost fn does that for us

可以在这里找到很好的解释:https://towardsdatascience.com/using-tf-print-in-tensorflow-aa26e1cff11e

【讨论】:

    猜你喜欢
    • 2019-12-03
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    • 2020-08-09
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    相关资源
    最近更新 更多