簡體   English   中英

TensorFlow“ InvalidArgumentError”,用於通過feed_dict饋入占位符的值

[英]TensorFlow “InvalidArgumentError” for value fed to placeholder via feed_dict

我正在研究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})}")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM