简体   繁体   中英

Tensorflow feed_dict with list of floats and InvalidArgumentError

Can't feed list to Tensorflow feed_dict. The code:

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})

Gives me:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'x_in' with dtype float and shape [100]
[[Node: x_in = Placeholderdtype=DT_FLOAT, shape=[100], _device="/job:localhost/replica:0/task:0/cpu:0"]]

What is wrong in the code, for me looks like it passes

placeholder tensor 'x_in' with dtype float and shape [100]

?

You should pass a tensor of shape [100] , so I guess you wanted to do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM