quarkpol
quarkpol

Reputation: 495

Problem with tensorflow, TF_SessionRun_wrapper: expected all values in input dict to be ndarray

I try to learn tensorflow, and I wrote my first model. When I try to run this model tensorflow gives

TypeError: TF_SessionRun_wrapper: expected all values in input dict to be ndarray.

I check type of data in input dict, and they are ndarrays. Probably I wrongly preprocess data, that I feed to model.

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

np.random.seed(0)
data, labels = load_iris(return_X_y=True)
num_elements = len(labels)

shuffled_indices = np.arange(len(labels))
np.random.shuffle(shuffled_indices)
shuffled_data = data[shuffled_indices]
shuffled_labels = labels[shuffled_indices]

one_hot_labels = np.zeros([num_elements, 3], dtype=int)
one_hot_labels[np.arange(num_elements), shuffled_labels] = 1

train_data = shuffled_data[0:105]
train_labels = one_hot_labels[0:105]
test_data = shuffled_data[105:]
test_labels = one_hot_labels[105:]


def linear_model(input):
    my_weights = tf.get_variable(name="weights", shape=[4, 3])
    my_bias = tf.get_variable(name="bias", shape=[3])

    linear_layer = tf.matmul(input, my_weights)
    linear_layer_out = tf.nn.bias_add(value=linear_layer, bias=my_bias)
    return linear_layer_out

x = tf.placeholder(tf.float32, shape=[None, 4], name="data_in")
y = tf.placeholder(tf.int32, shape=[None, 3], name="target_labels")

model_out = linear_model(x)

initializer = tf.global_variables_initializer()

loss = tf.reduce_mean(tf.losses.hinge_loss(logits=model_out, labels=y))

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.5).minimize(loss)

correct_prediction = tf.equal(tf.argmax(model_out, 1), tf.argmax(y, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

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

    for i in range(1000):
        batch_x, batch_y = train_data[:, :], train_labels[:, :]
        loss_val, _ = sess.run([loss, optimizer], feed_dict={x: batch_x, y: batch_y})

Upvotes: 0

Views: 3150

Answers (2)

drorhun
drorhun

Reputation: 584

If you have two different versions of numpy, it can cause this problem. To solve the problem, you need to delete all numpy libraries after that you can install it again.

2 times run below code ( why two times because most probably you have two different versions.)

pip uninstall numpy

after that

pip install numpy

Reference: https://github.com/tensorflow/tensorflow/issues/25729

Upvotes: 0

Jasper Chih
Jasper Chih

Reputation: 107

I fixed it by upgrading

  • the numpy package to (1.16.2).
  • tensorflow to 1.13.1 ,
  • Python to 3.6.8 ,
  • tensorflow-gpu to 1.13.1

Does not work with version 2.0.A.

Upvotes: 1

Related Questions