gpt4 book ai didi

python - 无效类型,必须是字符串或 Tensor [TensorFlow]

转载 作者:太空宇宙 更新时间:2023-11-04 10:10:16 24 4
gpt4 key购买 nike

我在 Google - Tensorflow 的机器学习库中遇到问题。当我想初始化我的 session 时,它告诉我必须是字符串或张量。我没有发现任何错误。

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

sess = tf.InteractiveSession()

x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])

W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

sess.run(tf.initialize_all_variables)

y = tf.nn.softmax(tf.matmul(x,W) + b)

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

for i in range(1000):
batch = mnist.train.next_batch(50)
train_step.run(feed_dict={x: batch[0], y_: batch[1]})

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels})

这是终端中以下程序的输出:

(tensorflow) juldou-box@juldou-box:~/tensorflow$ python mnist_e.py 
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
Traceback (most recent call last):
File "mnist_e.py", line 13, in <module>
sess.run(tf.initialize_all_variables)
File "/home/juldou-box/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 372, in run
run_metadata_ptr)
File "/home/juldou-box/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 584, in _run
processed_fetches = self._process_fetches(fetches)
File "/home/juldou-box/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 540, in _process_fetches
% (subfetch, fetch, type(subfetch), str(e)))
TypeError: Fetch argument <function initialize_all_variables at 0x7fe4ca157c80> of <function initialize_all_variables at 0x7fe4ca157c80> has invalid type <type 'function'>, must be a string or Tensor. (Can not convert a function into a Tensor or Operation.)

最佳答案

我认为您只是在 tf.initialize_all_variables 之后遗漏了一对括号 () ;)

如python所说,在第13行,看好

sess.run(tf.initialize_all_variables)

关于python - 无效类型,必须是字符串或 Tensor [TensorFlow],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38765676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com