Tensorflow feed_dict包含浮点数列表和InvalidArgumentError

时间:2016-12-27 17:21:31

标签: python tensorflow

无法将列表提供给Tensorflow feed_dict。代码:

import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split
from math import ceil
BATCH_SIZE = 100

# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x_data = np.random.rand(13000).astype(np.float32)
y_data = x_data * 0.1 + 0.3

x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.3)


# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)


x_in = tf.placeholder(tf.float32, shape=[BATCH_SIZE], name='x_in')
y_in = tf.placeholder(tf.float32, shape=[BATCH_SIZE], name='y_in')

W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_in + b

# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_in))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# Before starting, initialize the variables.  We will 'run' this first.
init = tf.global_variables_initializer()

# Launch the graph.
sess = tf.Session()
sess.run(init)


batchesCount = ceil(len(x_train) / BATCH_SIZE)

# Fit the line.
for curBatchId in range(batchesCount):
    batchStart = curBatchId * BATCH_SIZE
    xf = x_train[batchStart: BATCH_SIZE]
    yf = y_train[batchStart: BATCH_SIZE]

    sess.run(train, feed_dict={x_in: xf, y_in:yf})

给我:

  

InvalidArgumentError(请参阅上面的回溯):您必须提供值   对于占位符张量' x_in'用dtype浮点数和形状[100]
  [[Node:x_in = Placeholderdtype = DT_FLOAT,shape = [100],   _device =" /作业:本地主机/复制:0 /任务:0 / CPU:0"]]

代码有什么问题,对我而言似乎已经过了

  

占位符张量' x_in'使用dtype float和shape [100]

1 个答案:

答案 0 :(得分:2)

你应该传递一个形状张量[100],所以我想你想做:

for curBatchId in range(batchesCount):
    batchStart = curBatchId * BATCH_SIZE
    xf = x_train[batchStart: batchStart + BATCH_SIZE]
    yf = y_train[batchStart: batchStart + BATCH_SIZE]