日萌社

人工智能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

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐