Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value of a Keras tensor in TensorFlow 2?

TF1 had sess.run() and .eval() to get values of tensors - and Keras had K.get_value(); now, neither work the same (former two at all).

K.eager(K.get_value)(tensor) appears to work inside Keras graph by exiting it, and K.get_value(tensor) outside the graph - both w/ TF2's default eagerly (which is off in former). However, this fails if tensor is a Keras backend operation:

import keras.backend as K
def tensor_info(x):
    print(x)
    print("Type: %s" % type(x))
    try:        
        x_value = K.get_value(x)
    except:
        try:    x_value = K.eager(K.get_value)(x)
        except: x_value = x.numpy()
    print("Value: %s" % x_value)  # three methods

ones = K.ones(1)
ones_sqrt = K.sqrt(ones)

tensor_info(ones); print()
tensor_info(ones_sqrt)
<tf.Variable 'Variable:0' shape=(1,) dtype=float32, numpy=array([1.], dtype=float32)>
Type: <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>
Value: [1.]

Tensor("Sqrt:0", shape=(1,), dtype=float32)
Type: <class 'tensorflow.python.framework.ops.Tensor'>
# third print fails w/ below
AttributeError: 'Tensor' object has no attribute 'numpy' 


This is a non-issue in TF < 2.0. Github's been silent. I'm aware of ways to rewrite the code as a workaround, but it'll eliminate Keras' backend-neutrality and work akin to tf.keras. Is there a way to get Keras 2.3 tensor values in TensorFlow 2.0 while retaining backend-neutrality?
like image 406
OverLordGoldDragon Avatar asked Oct 06 '19 00:10

OverLordGoldDragon


People also ask

How can I get TensorFlow tensor value?

The easiest[A] way to evaluate the actual value of a Tensor object is to pass it to the Session. run() method, or call Tensor. eval() when you have a default session (i.e. in a with tf. Session(): block, or see below).

How do you access values in a tensor?

We can access the value of a tensor by using indexing and slicing. Indexing is used to access a single value in the tensor. slicing is used to access the sequence of values in a tensor. we can modify a tensor by using the assignment operator.

How can I print my Keras tensor value?

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.

How do you find the value of TensorFlow variable?

To get the current value of a variable x in TensorFlow 2, you can simply print it with print(x) . This prints a representation of the tf.


1 Answers

I think you want K.eval:

>>> v = K.ones(1)
>>> K.eval(v)
array([1.], dtype=float32)
>>> K.eval(K.sqrt(v))
array([1.], dtype=float32)

Note that K.get_value is reserved for use with variables (e.g. v here) while K.eval works with any tensor.

like image 133
Sergei Lebedev Avatar answered Oct 07 '22 14:10

Sergei Lebedev