0

I am trying to get the value of tensor.

# First Layer
encoder_layer1 = tflearn.fully_connected(x,41,activation='relu',bias=True)
layer1_weights = encoder_layer1.W
layer1_bias = encoder_layer1.b

result of printing it out is:

The layer 1 weights are:   <tf.Variable 'FullyConnected/W:0' shape=(41, 41) dtype=float32_ref>

even eval() doesn't seem to work. it throws an error

tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value FullyConnected/W
     [[Node: _send_FullyConnected/W_0 = _Send[T=DT_FLOAT, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=-6055748491062458677, tensor_name="FullyConnected/W:0", _device="/job:localhost/replica:0/task:0/cpu:0"](FullyConnected/W)]]

I have tried all the methods metiond but it doesn't seem to work.

Thanks in advance

WiLL_K
  • 575
  • 1
  • 3
  • 22
  • Possible duplicate of [How to print the value of a Tensor object in TensorFlow?](http://stackoverflow.com/questions/33633370/how-to-print-the-value-of-a-tensor-object-in-tensorflow) – Salvador Dali Apr 23 '17 at 19:51
  • Problem has been resolved! Thanks for pointing out – WiLL_K Apr 23 '17 at 19:52

1 Answers1

0

Shortly:

You can't evaluate any tensor variable outside of session, you must do it inside a session,

Why can't:

In order to understand why we can't do this, at first we should now what's going on the behind of tensorflow, since every thing in tensorflow is a node of graph, when we define variables and assign values to them, actually we are designing the graph and values are not assigned until we run the graph.

How to run graph:

Session executes the graph, consider the code blocks when we design a network in tensorflow, everything before session like blueprint and after this tf.session as sess line, like construction site, so you can only evaluate (eval()) tensors inside session. In the other words, graph defines operations, and operations only execute inside a session.

Hope this was useful. For more, read this

Ali Abbasi
  • 894
  • 9
  • 22
  • I have a session in my trainer function and I am running the eval() in 'with trainer.session.as_default()', its then I get the second error – WiLL_K Apr 23 '17 at 15:44
  • 1
    @WiLL_K the error states clearly: you first have to initialize the variable – dv3 Apr 23 '17 at 16:36
  • I had initialized the variables before hand, I have the problem solved, it was just that tflean and tensorflow were unable to recognize the with `trainer.session.as_default()`, I had to declare the session inside of the `eval(session=trainer.session)`. Problem is solved. I think i have messed up when I was building it with some more instruction set. – WiLL_K Apr 23 '17 at 16:40
  • Thanks @dv3 and Ali Abbasi – WiLL_K Apr 23 '17 at 16:42