m_squared
m_squared

Reputation: 245

TensorFlow "InvalidArgumentError" for value fed to placeholder via feed_dict

I"m working through the an example problem with TensorFlow (working with placeholders specifically) and don't understand why I'm receiving (what appears to be) a shape/type error when I'm fairly confident those are what they should be.

I've tried playing around with the various float types in X_batch & y_batch, tried changing the size from being "None" (unspecified) to what I will be passing in (100), none of which have worked

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)

The error I'm getting is:

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

I've thrown a print statement printing X_batch and y_batch properties, and they are what I expect them to be but still aren't working.

Upvotes: 1

Views: 87

Answers (1)

Danny Fang
Danny Fang

Reputation: 4071

The mse you want to evaluate is also dependent on placeholder X and y therefore you need to provide with feed_dict as well. You can fix it by changing the line to

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

But since you are trying to evaluate the model, it is reasonable to use a test dataset. So ideally it would be

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

Upvotes: 2

Related Questions