日萌社

人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新)


报错AttributeError: 'Tensor' object has no attribute 'numpy'
解决:tf.enable_eager_execution()
拓展:获取tensor类型的变量值

方法一:在会话中print( sess.run(x) )
	import tensorflow as tf
	#定义tensor常量
	x = tf.random_uniform((2, 3), -1, 1)
	#开启会话
	with tf.Session() as sess:
	    print (sess.run(x))
	#[[ 0.46468353 -0.61598516 -0.9036629 ]
	# [ 0.68292856  0.65335464  0.00641084]]
	
方法二:使用.eval()相当于将tensor类数据转为numpy,再输出
	import tensorflow as tf
	#定义tensor常量
	x = tf.random_uniform((2, 2), -1, 1)
	#开启会话
	with tf.Session() as sess:
		print (x.eval())
	#[[-0.8492842   0.50390124]
         # [-0.9012234   0.77892494]]

方法三:
    开启紧急执行
    tf.enable_eager_execution()

方法四:
    tensor类型变量.numpy()

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐