AttributeError: 'Tensor' 对象没有属性 'numpy' [英] AttributeError: 'Tensor' object has no attribute 'numpy'

查看:156
本文介绍了AttributeError: 'Tensor' 对象没有属性 'numpy'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何修复这个错误我从 GitHub 下载了这段代码.

How can I fix this error I downloaded this code from 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()

我收到这个错误.有人帮我,我只是想让它工作,为什么这么难?

And i get this error. Someone help me i just want it to work why is this so hard?

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:srcgithub	ensorflow	ensorflowcoreplatformcpu_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:srcgithub	ensorflow	ensorflowcoreframeworkop_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:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonclientsession.py", line 1278, in _do_call
    return fn(*args)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonclientsession.py", line 1263, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonclientsession.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:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonframeworkops.py", line 680, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonframeworkops.py", line 4951, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonclientsession.py", line 877, in run
    run_metadata_ptr)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonclientsession.py", line 1100, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonclientsession.py", line 1272, in _do_run
    run_metadata)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonclientsession.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:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonkerasenginease_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:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonkerasenginease_layer.py", line 736, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonkeraslayerscore.py", line 943, in call
    outputs = gen_math_ops.mat_mul(inputs, self.kernel)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonopsgen_math_ops.py", line 4750, in mat_mul
    name=name)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonframeworkop_def_library.py", line 510, in _apply_op_helper
    preferred_dtype=default_dtype)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonframeworkops.py", line 1094, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonops
esource_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:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonops
esource_variable_ops.py", line 1000, in _dense_var_to_tensor
    return self.value()
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonops
esource_variable_ops.py", line 662, in value
    return self._read_variable_op()
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonops
esource_variable_ops.py", line 745, in _read_variable_op
    self._dtype)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonopsgen_resource_variable_ops.py", line 562, in read_variable_op
    "ReadVariableOp", resource=resource, dtype=dtype, name=name)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonframeworkop_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonutildeprecation.py", line 454, in new_func
    return func(*args, **kwargs)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonframeworkops.py", line 3155, in create_op
    op_def=op_def)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packages	ensorflowpythonframeworkops.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)]]

推荐答案

我怀疑你复制代码的地方有 启用eager执行,即调用了tf.enable_eager_execution() 在程序的开头.

I suspect the place where you copied the code from had eager execution enabled, i.e. had invoked tf.enable_eager_execution() at the start of the program.

你也可以这样做.希望有帮助.

You could do the same. Hope that helps.

更新:请注意,在 TensorFlow 2.0 中默认启用 Eager Execution.所以上面的答案只适用于 TensorFlow 1.x

UPDATE: Note that eager execution is enabled by default in TensorFlow 2.0. So the answer above applies only to TensorFlow 1.x

这篇关于AttributeError: 'Tensor' 对象没有属性 'numpy'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆