当前位置: 首页 > 面试题库 >

AttributeError:“ Tensor”对象没有属性“ numpy”

平元明
2023-03-14
问题内容

我如何解决此错误,我是从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)]]

问题答案:

我怀疑您从中复制代码的地方启用了急切执行功能,即tf.enable_eager_execution()在程序开始时调用了该位置。

您也可以这样做。希望能有所帮助。

更新:请注意,默认情况下,TensorFlow 2.0中启用了急切执行。因此,以上答案仅适用于TensorFlow 1.x



 类似资料:
  • 问题内容: 我正在尝试使用sklearn.LinearRegression查找大量短序列的对数斜率。数据是从熊猫数据帧的行中提取的,如下所示: 但是,当我尝试使用np.log10时,在该系列上出现以下错误: 我不确定为什么会出现此错误,根据我所看到的,np.log10是否可以与numpy.float64一起使用。有想法吗? 问题答案: 是“ ufunc”,并且该方法对numpy ufuncs进行了

  • 问题内容: 我想转换火花数据框架以使用以下代码添加: 详细的错误消息是: 有人知道我在这里做错了吗?谢谢! 问题答案: 您无法使用数据框,但可以将数据框转换为RDD并通过映射将其映射。在Spark 2.0之前,别名为。使用Spark 2.0,您必须先明确调用。

  • 问题内容: 下面的代码给出了错误: 码: 问题答案: 从代码中,我可以看到你希望允许用户下载pdf。 现在开始 去 http://localhost:5000

  • 问题内容: 当我尝试时,会发生错误: 我找到了带有pyelasticsearch.py 的链接https://github.com/toastdriven/pyelasticsearch/blob/master/pyelasticsearch.py​​#L424-469,但我不知道它是哪个版本。无论如何,该代码中没有购买我的pyelasticsearch.py​​。任何人都有相同的经历吗?感谢您的

  • 我和cloud composer一起策划了两个数据流工作,它已经工作了一个月了。突然,这两个作业停止工作,并出现以下错误消息: 在作业中,我用存储客户端从google cloud storage下载一个文件。我以为这是因为一些依赖问题。在composer环境中,我安装了google-cloud-storage,但没有指定版本。我尝试指定包的不同版本,但似乎没有任何工作。 谢了!

  • 问题内容: 我正在使用Flask开发论坛模板。当我尝试使用表单在浏览器中创建新线程时,SQLAlchemy抛出AttributeError。当我尝试与论坛到线程实现一对多关系以及与线程到用户实现一对多关系时,出现了问题。 models.py 所有新的帖子/主题,并在views.py中处理 views.py 问题答案: 问题是这样的: 你要使用ORM对象,而不是主键列: 该错误表示整数被解释为ORM