TypeError:Feed的值不能是tf.Tensor对象

时间:2017-06-27 03:41:06

标签: python tensorflow

我是张力流的新手,从一开始就学习。我尝试使用tf.decode_csv阅读csv文件并进行解码,我试图通过示例了解batchgraph

我的示例csv是8个数据和1个逻辑形状,用逗号分隔。 这是我文件的前四行。

-0.294118,0.487437,0.180328,-0.292929,0,0.00149028,-0.53117,-0.0333333,0
-0.882353,-0.145729,0.0819672,-0.414141,0,-0.207153,-0.766866,-0.666667,1
-0.0588235,0.839196,0.0491803,0,0,-0.305514,-0.492741,-0.633333,0
-0.882353,-0.105528,0.0819672,-0.535354,-0.777778,-0.162444,-0.923997,0,1

我的代码是,

import tensorflow as tf

filename_queue = tf.train.string_input_producer(['data-03-diabetes.csv'],
                                                shuffle=False,
                                                name='filename_queue')
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)

record_defaults = [[0.],[0.],[0.],[0.],[0.],[0.],[0.],[0.],[0.]]
xy = tf.decode_csv(value, record_defaults=record_defaults, field_delim=',')

train_x_batch, train_y_batch = tf.train.batch([xy[0:-1], xy[-1:]],
                                              batch_size=10)

X = tf.placeholder(tf.float32, shape=[None, 8])
Y = tf.placeholder(tf.float32, shape=[None, 1])

W = tf.Variable(tf.random_normal([8,1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')

hypothesis = tf.sigmoid(tf.matmul(X, W) + b)
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis))

train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)

predicted = tf.cast(hypothesis > 0.5, dtype = tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))

with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    sess.run(tf.global_variables_initializer())
    feed = {X: train_x_batch, Y: train_y_batch}
    for step in range(10001):
        x_batch, y_batch = sess.run([train_x_batch, train_y_batch])
        if step % 200 == 0:
            print(step, sess.run(cost, feed_dict=feed))
    h, c, a = sess.run([hypothesis, predicted, accuracy], feed_dict=feed)
    print("\nHypothesis", h, "\nCorrect (Y):", c, "\nAccuracy", a)

然后我收到了这个错误代码,

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.

这是否意味着我声明错误placeholderVariable? 提前谢谢!

0 个答案:

没有答案