如何解决此错误,我是从GitHub下载此代码的。
predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].numpy()
引发错误
AttributeError: 'Tensor' object has no attribute 'numpy'
请帮助我解决此问题!
我用过:
sess = tf.Session()
with sess.as_default():
predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].eval()
我得到这个错误。有人帮我,我只是想让它工作,为什么这么难?
D:\Python>python TextGenOut.py
File "TextGenOut.py", line 72
predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].eval()
^
IndentationError: unexpected indent
D:\Python>python TextGenOut.py
2018-09-16 21:50:57.008663: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2018-09-16 21:50:57.272973: W T:\src\github\tensorflow\tensorflow\core\framework\op_kernel.cc:1275] OP_REQUIRES failed at resource_variable_ops.cc:480 : Not found: Container localhost does not exist. (Could not find resource: localhost/model/embedding/embeddings)
Traceback (most recent call last):
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\client\session.py", line 1278, in _do_call
return fn(*args)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\client\session.py", line 1263, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\client\session.py", line 1350, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable model/dense/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/model/dense/kernel)
[[Node: model/dense/MatMul/ReadVariableOp = ReadVariableOp[dtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](model/dense/kernel)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "TextGenOut.py", line 72, in <module>
predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].eval()
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\ops.py", line 680, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\ops.py", line 4951, in _eval_using_default_session
return session.run(tensors, feed_dict)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\client\session.py", line 877, in run
run_metadata_ptr)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\client\session.py", line 1100, in _run
feed_dict_tensor, options, run_metadata)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\client\session.py", line 1272, in _do_run
run_metadata)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\client\session.py", line 1291, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable model/dense/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/model/dense/kernel)
[[Node: model/dense/MatMul/ReadVariableOp = ReadVariableOp[dtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](model/dense/kernel)]]
Caused by op 'model/dense/MatMul/ReadVariableOp', defined at:
File "TextGenOut.py", line 66, in <module>
predictions, hidden = model(input_eval, hidden)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\engine\base_layer.py", line 736, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "TextGenOut.py", line 39, in call
x = self.fc(output)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\engine\base_layer.py", line 736, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\layers\core.py", line 943, in call
outputs = gen_math_ops.mat_mul(inputs, self.kernel)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\ops\gen_math_ops.py", line 4750, in mat_mul
name=name)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\op_def_library.py", line 510, in _apply_op_helper
preferred_dtype=default_dtype)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\ops.py", line 1094, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 1045, in _dense_var_to_tensor
return var._dense_var_to_tensor(dtype=dtype, name=name, as_ref=as_ref) # pylint: disable=protected-access
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 1000, in _dense_var_to_tensor
return self.value()
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 662, in value
return self._read_variable_op()
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 745, in _read_variable_op
self._dtype)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\ops\gen_resource_variable_ops.py", line 562, in read_variable_op
"ReadVariableOp", resource=resource, dtype=dtype, name=name)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\util\deprecation.py", line 454, in new_func
return func(*args, **kwargs)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\ops.py", line 3155, in create_op
op_def=op_def)
File "C:\Users\fried\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\ops.py", line 1717, in __init__
self._traceback = tf_stack.extract_stack()
FailedPreconditionError (see above for traceback): Error while reading resource variable model/dense/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/model/dense/kernel)
[[Node: model/dense/MatMul/ReadVariableOp = ReadVariableOp[dtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](model/dense/kernel)]]
有些文本是因为我的帖子主要是代码,但是没有什么可写的 adad tsf sfsf text text text man我爱Python(not)adada da dw来吧,这是enaugh text,为什么你欺负我stackoverflow我讨厌这个。 谢谢。非常感谢大家。抱歉让你久等了;复杂的业务;复杂。非常感谢。
我刚接到克林顿国务卿的电话。她祝贺我们,这是关于我们的胜利。我祝贺她和她的家人进行了非常非常艰苦的竞选。我的意思是,她非常努力。希拉里长期以来一直努力工作,非常努力,我们要感谢她对她为国家服务的重大感谢。我的意思是非常真诚的。
现在是时候让美国束缚分裂的伤口,必须团结起来。对于全国的所有共和党人,民主党人和独立人士,我要说,现在是我们团结起来团结起来的时候了。是时候了。我向我们每一位公民保证,我将成为所有美国人的总统,这对我来说是如此重要。
对于那些过去选择不支持我的人(其中有几个人),我正在与您联系,以寻求您的指导和帮助,以便我们可以共同努力,统一我们的伟大国家。 / p>
正如我从一开始就说过的那样,我们的运动不是一场运动,而是一场由数百万辛勤工作的男女组成的不可思议的伟大运动,他们热爱祖国,并希望自己拥有更美好,更光明的未来和他们的家人。这一运动由来自各个种族,宗教,背景和信仰的美国人组成,他们希望并期望我们的政府为人民服务,并为人民服务。
答案 0 :(得分:12)
我怀疑您从中复制代码的地方有eager execution enabled,即在程序开始时调用了tf.enable_eager_execution()
。
您可以执行相同的操作。 希望能有所帮助。
答案 1 :(得分:10)
由于接受的答案无法解决我的问题,因此我认为这对于某些已面临tensorflow版本> = 2.2.0并渴望执行的人很有帮助。
问题似乎是在拟合model.fit()
期间某些功能
由于性能原因,@ tf.function装饰器禁止执行tensor.numpy()
之类的功能。
对我来说,解决方案是将标志run_eagerly = True传递给model.compile(),如下所示:
model.compile(...,run_eagerly = True)
答案 2 :(得分:5)
它发生在旧版的TF中。因此,尝试pip install tensorflow --upgrade
否则运行
import tensorflow as tf
tf.enable_eager_execution()
如果您使用的是Jupyter笔记本,请重新启动内核。
答案 3 :(得分:1)
tf.multinomial
返回一个Tensor对象,该对象包含一个2D列表,列表中绘制的形状为[batch_size, num_samples]
。预期在该张量对象上调用.eval()
会返回一个numpy ndarray。
类似这样的东西:
predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].eval()
您还需要确保会话处于活动状态(否则就没有多大意义了):
sess = tf.Session()
with sess.as_default():
predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].eval()
答案 4 :(得分:1)
如果您的代码包装在@ tf.function或Keras层中,则在TF2.0中也会发生这种情况。两者都以图形模式运行。那里有很多秘密破解的代码,因为急切模式和图形模式之间的行为有所不同,并且人们不知道自己正在切换上下文,所以要小心!
答案 5 :(得分:1)
Tensorflow 2具有一个配置选项来“急切地”运行函数,这将使能够通过.numpy()
方法获取Tensor值。要启用急切执行,请使用以下命令:
tf.config.run_functions_eagerly(True)
请注意,这主要用于调试。
另请参阅:https://www.tensorflow.org/api_docs/python/tf/config/run_functions_eagerly
答案 6 :(得分:0)
当我运行如下代码时,我看到了类似的错误
tensor = tf.multiply(ndarray, 42)
tensor.numpy() # throw AttributeError: 'Tensor' object has no attribute 'numpy'
我使用带有Tensorflow 1.14.0的Anaconda 3。我使用以下命令升级了tensorflow
conda update tensorflow
现在tensorflow是2.0.0,已解决问题。尝试此操作是否可以解决您的问题。
答案 7 :(得分:0)
我在 tf.function() 中遇到了同样的问题:但对我有用的是通过 tf.convert_to_tensor
Doku 将 numpy 数组转换为张量流张量,然后继续使用张量流。也许这个技巧对任何人都有用......
答案 8 :(得分:0)
对于在 TF 2.0.0 中仍然存在此问题的人,请运行:tf.config.run_functions_eagerly(True) top of ur program 它完美运行!