0

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})
3
  • I was able to run your code without any problem. What is the version of tensorflow you are using ? Commented Mar 1, 2019 at 11:06
  • I use 1.13.1 tensorflow version, and 1.15.4 numpy version. Commented Mar 1, 2019 at 11:10
  • 1
    It is problem in environment, but I can not say exactly which package. I had this problem using Anaconda with Windows, tried different versions python, tensorflow, numpy, nothing worked out. It helped when I changed today Anaconda to WinPython (WPy-3670), python 3.6.7, tensorflow 1.12.0rc2 Commented Mar 6, 2019 at 22:53

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.