Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print the values of Keras tensors?

I am implementing own Keras loss function. How can I access tensor values?

What I've tried

def loss_fn(y_true, y_pred):
    print y_true

It prints

Tensor("target:0", shape=(?, ?), dtype=float32)

Is there any Keras function to access y_true values?

like image 263
ronroo Avatar asked Apr 17 '17 08:04

ronroo


People also ask

How do you print a tensor data?

To print to a file, pass a string started with "file://" followed by the file path, e.g., "file:///tmp/foo.out". The first and last summarize elements within each dimension are recursively printed per Tensor. If None, then the first 3 and last 3 elements of each dimension are printed for each tensor.

How do you print the value of a tensor object?

[A]: To print the value of a tensor without returning it to your Python program, you can use the tf. print() operator, as Andrzej suggests in another answer. According to the official documentation: To make sure the operator runs, users need to pass the produced op to tf.

How do I print a TensorFlow model?

pb tensorflow model you can use: inspect_pb.py to print model info or use tensorflow summarize_graph tool with --print_structure flag, also it's nice that it can detect input and output names.


3 Answers

Keras' backend has print_tensor which enables you to do this. You can use it this way:

import keras.backend as K

def loss_fn(y_true, y_pred):
    y_true = K.print_tensor(y_true, message='y_true = ')
    y_pred = K.print_tensor(y_pred, message='y_pred = ')
    ...

The function returns an identical tensor. When that tensor is evaluated, it will print its content, preceded by message. From the Keras docs:

Note that print_tensor returns a new tensor identical to x which should be used in the following code. Otherwise the print operation is not taken into account during evaluation.

So, make sure to use the tensor afterwards.

like image 153
nroulet Avatar answered Oct 22 '22 02:10

nroulet


Usually, y_true you know in advance - during preparation of your train corpora...

However, there's one trick to see the values inside y_true and/or y_pred. Keras gives you an opportunity to write respective callback for printing the neural network's output. It will look something like this:

def loss_fn(y_true, y_pred):
    return y_true # or y_pred
...
import keras.callbacks as cbks
class CustomMetrics(cbks.Callback):

    def on_epoch_end(self, epoch, logs=None):
        for k in logs:
            if k.endswith('loss_fn'):
               print logs[k]

Here the loss_fn is name of your loss function when you pass it into the model.compile(...,metrics=[loss_fn],) function during model's compilation.

So, finally, you have to pass this CustomMetrics callback as the argument into the model.fit():

model.fit(x=train_X, y=train_Y, ... , callbacks=[CustomMetrics()])

P.S.: If you use Theano (or TensorFlow) like here in Keras, you write a python program, and then you compile it and execute. So, in your example y_true - is just a tensor variable which is used for further compilation and loss function counting.

It means that there's no way to see the values inside it. In Theano, for example, you can look inside the only so-called shared variable after the execution of respective eval() function. See this question for more info.

like image 5
Igor Poletaev Avatar answered Oct 22 '22 02:10

Igor Poletaev


You could redefine your loss function to return the value instead:

def loss_fn(y_true, y_pred):
    return y_true

Let's create some tensors:

from keras import backend as K

a = K.constant([1,2,3])
b = K.constant([4,5,6])

And use the keras.backend.eval() API to evaluate your loss function:

loss = loss_fn(a,b)
K.eval(loss)
# array([1., 2., 3.], dtype=float32)
like image 3
remykarem Avatar answered Oct 22 '22 02:10

remykarem