Why this code in tensorflow doesn't work?
NickName:xxx222 Ask DateTime:2016-11-07T12:38:25

Why this code in tensorflow doesn't work?

My code is written as this.

def __init__(self, X):
    ops.reset_default_graph()
    tl.layers.clear_layers_name()

    self.sess = tf.Session()

    self.input_x = tf.placeholder(tf.float32, shape=[None, 784],name="input")  

    input_layer = tl.layers.InputLayer(self.input_x)
    drop1 = tl.layers.DropoutLayer(input_layer, keep=0.8, name="drop1")
    relu1 = tl.layers.DenseLayer(drop1, n_units=800, act = tf.nn.relu)
    drop2 = tl.layers.DropoutLayer(relu1, keep=0.5, name="drop2")

    self.output = drop2.all_layers[-1]

    self.gradient = tf.gradients(self.output,self.input_x)

    init_op = tf.initialize_all_variables()
    self.sess.run(init_op)
    self.output.eval(session=self.sess, feed_dict={self.input_x:X})

As you can see, there is only one placeholder initiated, however, I met

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float [[Node: Placeholder = Placeholderdtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]]

I am one hundred percent sure that the X I fed in has type float32, and the shape [1000,784].

Copyright Notice:Content Author:「xxx222」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40457938/why-this-code-in-tensorflow-doesnt-work

Answers
Daniel De Freitas 2016-11-07T09:22:04

As Olivier correctly pointed out, the name of the placeholder tensor missing a feed value is different from the name of the placeholder tensor you directly created (\"input\"). \n\nIf you're using TensorLayer, you probably won't be able to just call session.run or some_tensor.eval without understanding TensorLayer layer insternals. For example, each of their DropoutLayer instances creates internally a tf.placeholder for the keep probability. \n\nThat said this library seems to expect you only interact with your model via their APIs (e.g. fit and test) like in the following example:\n\n# Train the network, we recommend to use tl.iterate.minibatches()\ntl.utils.fit(sess, network, train_op, cost, X_train, y_train, x, y_,\n acc=acc, batch_size=500, n_epoch=500, print_freq=5,\n X_val=X_val, y_val=y_val, eval_train=False)\n\n# Evaluation\ntl.utils.test(sess, network, acc, X_test, y_test, x, y_, batch_size=None, cost=cost)\n\n\nFrom: https://github.com/zsdonghao/tensorlayer#your-first-program ",


More about “Why this code in tensorflow doesn't work?” related questions

Why doesn't Tensorflow have if conditions?

Recently I was implementing a custom loss function in Tensorflow and I realized, Tensorflow doesn't have if conditions. I feel like if conditions are such an inherent programming feature why would

Show Detail

Why TensorFlow Serving doesn't leverage the configured GPU?

I'm serving a model using TensorFlow serving. After attacking to the system to serve 10 requests per second, the status of my server is: It shows that all CPUs are busy while my GPU is idle. I found

Show Detail

Why the import "from tensorflow.train import Feature" doesn't work

That's probably totally noob question which has something to do with python module importing, but I can't understand why the following is valid: > import tensorflow as tf > f = tf.train.Feat...

Show Detail

Tensorflow/Keras not importing correctly on VS code 2022. Tensorflow Version == 2.8

No idea why this is happening but no matter what I try I can't get TensorFlow or Keras to be read properly so VScode actually sees them. No matter how I import them, it always comes up with "

Show Detail

Why does the Keras implementation for the Adam optimizer have the decay argument and Tensorflow doesn't?

Why does the Keras implementation for the Adam optimizer have the decay argument and Tensorflow doesn't? And what idea of this argument?

Show Detail

ipython doesn't work in TensorFlow?

Why doesn't ipython find the tensorflow package, as below? (tensorflow) [abigail@localhost anaconda3]$ which ipython ~/anaconda3/envs/tensorflow/bin/ipython (tensorflow) [abigail@localhost anacon...

Show Detail

Why the following Tensorflow code produces the shown result?

I am following the tensorflow tutorial to do some tests. In the section of Variable of the official tutorial, there is a code snippet indicating how to use tensorflow to implement a counter. I chan...

Show Detail

Why doesn't tensorflow allow me to write file?

I am a newbie to tensorflow. I have trained a model to classify images by tensorflow, and the prediction works well. However, when I try to write the classification result of test dataset into a re...

Show Detail

Pycharm highlights code import tensorflow.compat.v1

I've got code that does import tensorflow.compat.v1. Import is going with no errors but PyCharm highlighted this code and says No module named compat. My tensorflow version is 2.4.1 Why does PyCha...

Show Detail

What is the difference between Tensorflow Hub vs Tensorflow Official Models?

Regarding Tensorflow models in Tensorflow Hub (https://tfhub.dev/tensorflow) vs models found on official Tensorflow Github Repository (https://github.com/tensorflow/models/tree/master/official): D...

Show Detail