Fri*_*ler 13 python numpy attributeerror tensorflow tensor
如何修复此错误我从GitHub下载了此代码.
predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].numpy()
Run Code Online (Sandbox Code Playgroud)
抛出错误
AttributeError: 'Tensor' object has no attribute 'numpy'
Run Code Online (Sandbox Code Playgroud)
请帮我解决这个问题!
我用了:
sess = tf.Session()
with sess.as_default():
predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].eval()
Run Code Online (Sandbox Code Playgroud)
我得到这个错误.有人帮助我,我只是想让它工作为什么这么难?
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)]]
Run Code Online (Sandbox Code Playgroud)
一些文字,因为我的帖子主要是代码,但没有什么可以写adad tsf sfsf文本文本文本男人我喜欢Python(不)adada da dw来吧这是enaugh文本为什么你欺负我stackoverflow我讨厌这个.谢谢.非常感谢大家.抱歉让你久等了; 复杂的业务; 复杂.非常感谢你.
我刚收到克林顿国务卿的电话.她祝贺我们,这是关于我们的胜利,我祝贺她和她的家人在非常非常激烈的竞选活动中.我的意思是,她非常努力.希拉里在很长一段时间里工作很长很长时间,我们非常感谢她为我们国家所做的贡献.我非常真诚地说.
现在是时候让美国束缚分裂的伤口,必须聚在一起.对于这个国家的所有共和党人,民主党人和独立人士,我说现在是我们团结一致的时候了.是时候了.我向所有美国公民保证,我将成为所有美国人的总统,这对我来说非常重要.
对于那些过去选择不支持我的人,其中有少数人,我会向你寻求指导和帮助,以便我们能够共同努力,统一我们伟大的国家.
正如我从一开始就表示,我们不是运动,而是一个令人难以置信的和伟大的运动由数百万个勤劳的男人和女人,谁爱自己的国家,想要更好的,更美好的未来为自己和他们的家庭.这是一个由来自所有种族,宗教,背景和信仰的美国人组成的运动,他们希望并期望我们的政府为人民服务 - 并为人民服务
Ale*_*der 35
由于接受的答案并没有为我解决问题,所以我认为它可能对一些面临问题并且已经拥有 tensorflow 版本 >= 2.2.0 并启用了急切执行的人有所帮助。
问题似乎是,对于某些功能model.fit()
,@tf.function装饰器在拟合期间禁止执行功能,例如tensor.numpy()出于性能原因。
我的解决方案是将标志传递run_eagerly=True给model.compile()这样的:
model.compile(..., run_eagerly=True)
Run Code Online (Sandbox Code Playgroud)
ash*_*ash 33
我怀疑你复制代码的地方已经启用了急切的执行,即tf.enable_eager_execution()在程序开始时调用了.
你也可以这样做.希望有所帮助.
mar*_*mus 14
如果您的代码包含在 @tf.function 或 Keras 层中,则这也可能发生在 TF2.0 中。两者都以图形模式运行。那里有很多秘密损坏的代码,因为急切模式和图形模式之间的行为不同,人们不知道他们正在切换上下文,所以要小心!
它发生在旧版本的 TF 中。所以试试pip install tensorflow --upgrade
否则运行
import tensorflow as tf
tf.enable_eager_execution()
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 Jupyter notebook,请重新启动内核。
Tensorflow 2 有一个配置选项来“热切地”运行函数,这将允许通过.numpy()方法获取 Tensor 值。要启用急切执行,请使用以下命令:
tf.config.run_functions_eagerly(True)
Run Code Online (Sandbox Code Playgroud)
请注意,这主要用于调试。
另见:https : //www.tensorflow.org/api_docs/python/tf/config/run_functions_eagerly