tensorflow中的.numpy()方法

.numpy方法只用在使用tf.enable_eager_execution()(命令式编程开启)后才有的方法, 否则会有==AttributeError: ‘Tensor’ object has no attribute ‘numpy’==报错

from math import pi

def f(x):
  return tf.square(tf.sin(x))

assert f(pi/2).numpy() == 1.0
print(f(pi/2).numpy())
print(f(pi/2))

输出:

1.0
tf.Tensor(1.0, shape=(), dtype=float32)

猜你喜欢

转载自blog.csdn.net/qq_39124762/article/details/82952465