0

So I was trying to make a tensorflow model with my own data but it seems like numpy array isnt a feedable type in tf.Session.run()?

I checked the website and it is supposed to be feedable as shown in one of their example:

x = tf.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)

with tf.Session() as sess:
  print(sess.run(y))  # ERROR: will fail because x was not fed.

  rand_array = np.random.rand(1024, 1024) #HERE IS THE NUMPY ARRAY

  print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.

After searching a couple of time, it seems like most of the time the problem is with renaming placeholder variable during the code which mess stuff up but it isn't the case in my code, at least I don't think so.

Here is the full working code and the input file data:

import tensorflow as tf
import numpy as np

data = np.load("test_data.npz")
trng_input = np.array(data['Input'], dtype=np.float64)
trng_output = np.array(data['Output'], dtype=np.float64)

nhl1 = 16
nhl2 = 8
n_classes = 4


x = tf.placeholder(dtype=tf.float64, shape=[len(trng_input),24])
y = tf.placeholder(dtype=tf.float64, shape=[len(trng_output),n_classes])

def NN(data):
    hl1 = {"weights":tf.Variable(tf.random_normal([24, nhl1], dtype=tf.float64)),
           "biases":tf.Variable(tf.random_normal([nhl1], dtype=tf.float64))}

    hl2 = {"weights":tf.Variable(tf.random_normal([nhl1, nhl2], dtype=tf.float64)),
           "biases":tf.Variable(tf.random_normal([nhl2], dtype=tf.float64))}

    output_layer = {"weights":tf.Variable(tf.random_normal([nhl2, n_classes], dtype=tf.float64)),
                    "biases":tf.Variable(tf.random_normal([n_classes], dtype=tf.float64))}

    l1 = tf.add(tf.matmul(data, hl1["weights"]), hl1["biases"])
    l1 = tf.nn.leaky_relu(l1, alpha=0.2)

    l2 = tf.add(tf.matmul(l1, hl2["weights"]), hl2["biases"])
    l2 = tf.nn.leaky_relu(l2, alpha=0.2)

    output = tf.add(tf.matmul(l2, output_layer["weights"]), output_layer["biases"])
##    output = tf.nn.leaky_relu(l1, alpha=0.2)

    return output

def train(x):
    prediction = NN(x)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=y))
    optimizer = tf.train.AdamOptimizer().minimize(cost)

    epochs = 100

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

        for epoch in range(epochs):
            epoch_loss = 0
            _, c = sess.run([optimizer, cost], feed_dict={x: trng_input, y: trng_output})
            epoch_loss += c
            print(F"Epoch {epoch} completed out of {epochs}. \nloss:{epoch_loss}")

        correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y, 1))

        accuracy = tf.reduce_mean(tf.cast(correct,"float"))
        Eval = accuracy.eval({x:trng_input, y:trng_output})
        print(F"Accuracy:{Eval}")

train(trng_input)

Full error as requested:

Traceback (most recent call last):
  File "C:\Python36\machine_learning\real_tf_folder\test.py", line 59, in <module>
    train(trng_input)
  File "C:\Python36\machine_learning\real_tf_folder\test.py", line 49, in train
    _, c = sess.run([optimizer, cost], feed_dict={x: trng_input, y: trng_output})
 TypeError: unhashable type: 'numpy.ndarray'
1
  • Please post the whole Error with its stacktrace. Commented Jun 15, 2018 at 17:47

1 Answer 1

3

As you correctly guessed, you are actually "renaming" your placeholder x. The x inside the function train(x) takes the value of the arguments (e.g. trng_input) and doesn't point to the outer-scope placeholder x.


Note: if trng_input and trng_output represent your full dataset, you may want to iterate over mini-batches instead of them whole.

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

2 Comments

oh lord that little x was all that was the problem, my data only has 3000 examples so I didnt see the point of batching and the labels arent onehot array, the problem wasnt there
Right, my bad for the onehot thing. I edited my answer. Happy it helped though.

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.