Tensorflow 学习遇到的问题

博主在初步学习TensorFlow时,遇到tensorflow实例.numpy()报错问题,国内网站未找到解决办法,后在StackOverflow得知需启用eager_execution(),即启用eager,在代码中加入相关内容即可解决,还给出了HelloWord程序参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考博客为:https://blog.youkuaiyun.com/xierhacker/article/details/53102355

在初步学习tensorflow的过程中,遇到了tensorflow实例.numpy() 报错

AttributeError: 'Tensor' object has no attribute 'numpy'

在国内网站上似乎没有找到解决办法,后来在StackOverflow中找到了答案:

由于该方法需要启用eager_execution(),也就是需要启用eager 链接在这

所以只需要在代码中加入

tf.enable_eager_execution()

即可。官方API 在这 

HelloWord程序参考如下:

from __future__ import print_function,division
import tensorflow as tf

#define the graph
tf.enable_eager_execution()
info = tf.constant("hello world")
a=tf.constant([[1,2],[3,4]])
b=tf.constant([[1,1],[0,1]])
print("info:",info)
print("a:",a)
print("b:",b)
print("type of a:",type(a))
# mul option
c=tf.matmul(a,b)
print("c:",c)
print("c.numpy:\n",c.numpy())
print("type of c.numpy():",type(c.numpy()))
print("\n")

'''
output:
info: tf.Tensor(b'hello world', shape=(), dtype=string)
a: tf.Tensor(
[[1 2]
 [3 4]], shape=(2, 2), dtype=int32)
b: tf.Tensor(
[[1 1]
 [0 1]], shape=(2, 2), dtype=int32)
type of a: <class 'EagerTensor'>
c: tf.Tensor(
[[1 3]
 [3 7]], shape=(2, 2), dtype=int32)
c.numpy:
 [[1 3]
 [3 7]]
type of c.numpy(): <class 'numpy.ndarray'>
'''

#show the dedvice
print("device:",c.device)
print("dtype:",c.dtype)
print("shape:",type(c.shape))

'''
output:
device: /job:localhost/replica:0/task:0/device:CPU:0
dtype: <dtype: 'int32'>
shape: <class 'tensorflow.python.framework.tensor_shape.TensorShape'>
'''

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值