欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Tensorflow 学习遇到的问题

程序员文章站 2024-01-19 08:13:58
...

参考博客为:https://blog.csdn.net/xierhacker/article/details/53102355

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

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

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

由于该方法需要启用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'>
'''