TF1具有张量的值,sess.run()
并.eval()
得到Keras的值K.get_value()
; 现在,两者都不一样(以前是两个)。
K.eager(K.get_value)(tensor)
似乎可以通过退出它在Keras图内和K.get_value(tensor)
图外工作-带有TF2的默认值(在以前是关闭的)。但是,如果tensor
是Keras后端操作,则此操作将失败:
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'
tf.keras
。有没有办法在TensorFlow 2.0中获得Keras 2.3张量值同时保持后端中立性?
我想你要K.eval
:
>>> v = K.ones(1)
>>> K.eval(v)
array([1.], dtype=float32)
>>> K.eval(K.sqrt(v))
array([1.], dtype=float32)
请注意,当与任何张量一起K.get_value
使用时,保留用于变量(例如,v
此处)K.eval
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句