0

I have following tensor object, which is outpur of a=model.layers[3].output this code

a = <tf.Tensor 'dense_20/Softmax:0' shape=(None, 3) dtype=float32>

How to get values from above tensor object. I have tried print(a.numpy()) and getting following error:

AttributeError: 'Tensor' object has no attribute 'numpy'

I have also tried eval(a) method, and getting same above error.

when using a.eval(), getting following error:

ValueError: Cannot evaluate tensor using `eval()`: No default session is registered. Use `with sess.as_default()` or pass an explicit session to `eval(session=sess)`

I have also tried following:

with tf.compat.v1.Session() as sess:
    a=model.layers[3].output
    print(sess.run(a))
    sess.close()

and getting following error:

RuntimeError: The Session graph is empty.  Add operations to the graph before calling run().

I have tried many things from here, but not getting answer.

I am working on google colab, tensorflow=2.2.0, keras=2.3.1

jdehesa
  • 58,456
  • 7
  • 77
  • 121
Hitesh
  • 1,285
  • 6
  • 20
  • 36
  • You need to give some more context, like a minimal example where we can see the error. Depending on whether you are in graph or eager mode there are different things you can do, and that depends on where you are trying to get the values. Maybe [`tf.print`](https://www.tensorflow.org/api_docs/python/tf/print) can help you. – jdehesa Jun 11 '20 at 09:08
  • Please provide a minimal example to reproduce your problem. – Abhinav Goyal Jun 11 '20 at 11:58

1 Answers1

1

In order to evaluate the output of a arbitary layer in a Keras model, you need to make sure that all its inputs are available. Here is an example code which uses a dummy model to show the same. The code should work in both TF1.x and TF2.x. Note the use of Keras functions here to get rid of boiler plate code for handling tensorflow sessions.

import tensorflow as tf
print('TensorFlow: ', tf.__version__, end='\n\n')

input_layer = tf.keras.Input(shape=[100])
x = tf.keras.layers.Dense(16, activation='relu')(input_layer)
x = tf.keras.layers.Dense(64, activation='relu')(x)
x = tf.keras.layers.Dense(32, activation='relu')(x)
x = tf.keras.layers.Dense(10, activation='relu')(x)
output_layer = tf.keras.layers.Dense(5, activation='softmax')(x)

model = tf.keras.Model(inputs=[input_layer], outputs=[output_layer])

a = model.layers[3].output
print(a)

fn = tf.keras.backend.function(input_layer, a)  # create a Keras Function
random_input = tf.random.normal([1, 100])  # random noise

a_eval = fn(random_input)
print('\nLayer Output:\n', a_eval)

Output:

TensorFlow:  2.3.0-dev20200611

Tensor("dense_73/Identity:0", shape=(None, 32), dtype=float32)

Layer Output:
 [[0.         0.         0.46475422 0.0961322  0.         0.
  0.23016977 0.         0.         0.05861767 0.03298267 0.11953808
  0.         0.         0.97043467 0.         0.         0.6384926
  0.         0.         0.         0.2346505  0.1822727  0.0145395
  0.08411474 0.         0.         0.37601566 0.         0.
  0.29435986 0.44069782]]
Srihari Humbarwadi
  • 2,532
  • 1
  • 10
  • 28