Adelovi
Adelovi

Reputation: 95

How to print the values of Keras tensors?

I have this code and i want to print the keras tensor values of 'out':

import tensorflow as tf
from tensorflow.keras import Input

batch_size = 8
inp = Input(shape=(batch_size, 7, 7,2048))
inp = tf.squeeze(inp,axis=0)
print(inp.shape)
layer = Flatten()
out = layer(inp)
print(out.shape)
out = Dense(1,activation='sigmoid')(out)
print(out.shape)
print(layer.count_params())

i am running Tensorflow 2.6.0 on googele colab, i tried many way to print the out tensor values but nothing works.

Upvotes: 0

Views: 260

Answers (1)

Prefect
Prefect

Reputation: 1777

You should feed some data to your model in order to get some output values from your layers.

import tensorflow as tf
from tensorflow.keras import Model
from keras.layers import Flatten, Dense, Input

### Model setup ###
inp = Input(shape=(7, 7,2048))
# inp = tf.squeeze(inp,axis=0) # ValueError: Graph disconnected
layer = Flatten()
out = layer(inp)
out = Dense(1,activation='sigmoid')(out)

model = Model(inputs=inp,outputs=out)

### Data ###
batch_size = 8
my_input = tf.random.uniform(shape=(batch_size,7,7,2048))

print(model(my_input)) # returns a tensor with the shape (batch_size,1)

Upvotes: 0

Related Questions