gpt4 book ai didi

python - TensorFlow "InvalidArgumentError"用于通过 feed_dict 馈送到占位符的值

转载 作者:行者123 更新时间:2023-12-01 00:56:20 26 4
gpt4 key购买 nike

我正在解决 TensorFlow 的示例问题(特别是使用占位符),并且不明白为什么我收到(看起来是)形状/类型错误,而我相当有信心这些错误是什么他们应该是。

我尝试过使用 X_batch 和 y_batch 中的各种浮点类型,尝试将大小从“无”(未指定)更改为我将传递的值(100),但没有一个起作用

import tensorflow as tf
import numpy as np
from sklearn.datasets import fetch_california_housing

def fetch_batch(epoch, batch_index, batch_size, X, y):

np.random.seed(epoch * batch_index)
indices = np.random.randint(m, size=batch_size)
X_batch = X[indices]
y_batch = y[indices]
return X_batch.astype('float32'), y_batch.astype('float32')

if __name__ == "__main__":

housing = fetch_california_housing()

m, n = housing.data.shape

# standardizing input data
standardized_housing = (housing.data - np.mean(housing.data)) / np.std(housing.data)

std_housing_bias = np.c_[np.ones((m, 1)), standardized_housing]

# using the size "n+1" to account for the bias term
X = tf.placeholder(tf.float32, shape=(None, n+1), name='X')
y = tf.placeholder(tf.float32, shape=(None, 1), name='y')

theta = tf.Variable(tf.random_uniform([n + 1, 1], -1, 1), dtype=tf.float32, name='theta')

y_pred = tf.matmul(X, theta, name='predictions')

error = y_pred - y

mse = tf.reduce_mean(tf.square(error), name='mse')

n_epochs = 1000
learning_rate = 0.01
batch_size = 100
n_batches = int(np.ceil(m / batch_size))

# using the Gradient Descent Optimizer class from tensorflow's optimizer selection
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)

training_op = optimizer.minimize(mse)

# creates a node in the computational graph that initializes all variables when it is run
init = tf.global_variables_initializer()

with tf.Session() as sess:
sess.run(init)

for epoch in range(n_epochs):
for batch_index in range(n_batches):
X_batch, y_batch = fetch_batch(epoch, batch_index, batch_size, std_housing_bias, \
housing.target.reshape(-1, 1))

print(X_batch.shape, X_batch.dtype, y_batch.shape, y_batch.dtype)
sess.run(training_op, feed_dict={X: X_batch, y: y_batch})

if epoch % 100 == 0:
print(f"Epoch {epoch} MSE = {mse.eval()}")


best_theta = theta.eval()

print("Mini Batch Gradient Descent Beta Estimates")
print(best_theta)

我收到的错误是:

InvalidArgumentError: You must feed a value for placeholder tensor 'X' with dtype float and shape [?,9]
[[node X (defined at /Users/marshallmcquillen/Scripts/lab.py:25) ]]

我抛出了一条打印 X_batch 和 y_batch 属性的打印语句,它们是我所期望的,但仍然不起作用。

最佳答案

您要评估的 mse 还取决于占位符 Xy,因此您需要提供 feed_dict 也是如此。您可以通过将行更改为

来修复它
if epoch % 100 == 0:
print(f"Epoch {epoch} MSE = {mse.eval(feed_dict={X: X_batch, y: y_batch})}")

但是由于您正在尝试评估模型,因此使用测试数据集是合理的。所以理想情况下应该是

if epoch % 100 == 0:
print(f"Epoch {epoch} MSE = {mse.eval(feed_dict={X: X_test, y: y_test})}")

关于python - TensorFlow "InvalidArgumentError"用于通过 feed_dict 馈送到占位符的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56219809/

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