Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

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

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()

throws the error

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

Please help me fix this!

I used:

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:srcgithubensorflowensorflowcoreplatformcpu_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:srcgithubensorflowensorflowcoreframeworkop_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-packagesensorflowpythonclientsession.py", line 1278, in _do_call
    return fn(*args)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonclientsession.py", line 1263, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonclientsession.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-packagesensorflowpythonframeworkops.py", line 680, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonframeworkops.py", line 4951, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonclientsession.py", line 877, in run
    run_metadata_ptr)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonclientsession.py", line 1100, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonclientsession.py", line 1272, in _do_run
    run_metadata)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonclientsession.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-packagesensorflowpythonkerasenginease_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-packagesensorflowpythonkerasenginease_layer.py", line 736, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonkeraslayerscore.py", line 943, in call
    outputs = gen_math_ops.mat_mul(inputs, self.kernel)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonopsgen_math_ops.py", line 4750, in mat_mul
    name=name)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonframeworkop_def_library.py", line 510, in _apply_op_helper
    preferred_dtype=default_dtype)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonframeworkops.py", line 1094, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonops
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-packagesensorflowpythonops
esource_variable_ops.py", line 1000, in _dense_var_to_tensor
    return self.value()
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonops
esource_variable_ops.py", line 662, in value
    return self._read_variable_op()
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonops
esource_variable_ops.py", line 745, in _read_variable_op
    self._dtype)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonopsgen_resource_variable_ops.py", line 562, in read_variable_op
    "ReadVariableOp", resource=resource, dtype=dtype, name=name)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonframeworkop_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonutildeprecation.py", line 454, in new_func
    return func(*args, **kwargs)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonframeworkops.py", line 3155, in create_op
    op_def=op_def)
  File "C:UsersfriedAppDataRoamingPythonPython36site-packagesensorflowpythonframeworkops.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)]]
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...