【问题标题】:'tuple' object has no attribute 'shape'“元组”对象没有属性“形状”
【发布时间】:2020-07-25 16:54:49
【问题描述】:

我在 Google Colab 中运行此代码并收到此错误

这是 Google Colab 的代码链接:

https://colab.research.google.com/drive/1DbeK9Qz_Q31gM6lmERNOaegP2irTuv0a?usp=sharing

这是错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-7-e258b3312a90> in <module>()
     31 print(f"acc: {val_acc}")
     32 
---> 33 predictions = model.predict([x_test])
     34 
     35 print(np.argmax(predictions[0]))

10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    966           except Exception as e:  # pylint:disable=broad-except
    967             if hasattr(e, "ag_error_metadata"):
--> 968               raise e.ag_error_metadata.to_exception(e)
    969             else:
    970               raise

AttributeError: in user code:

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1147 predict_function  *
        outputs = self.distribute_strategy.run(
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:951 run  **
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1122 predict_step  
        return self(x, training=False)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:927 __call__
        outputs = call_fn(cast_inputs, *args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/sequential.py:291 call
        outputs = layer(inputs, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:927 __call__
        outputs = call_fn(cast_inputs, *args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py:642 call
        input_shape = inputs.shape

    AttributeError: 'tuple' object has no attribute 'shape'

【问题讨论】:

  • 我无法重现此错误。也许重新启动你的内核,然后再试一次。
  • 请包含您的代码,不是作为链接,而是作为 sn-p。

标签: python keras tuples prediction


【解决方案1】:

改变

model.predict([x_test])

model.predict(x_test)

【讨论】:

    猜你喜欢
    • 2017-05-03
    • 2021-12-31
    • 2019-06-26
    • 2020-05-18
    • 2014-01-27
    • 2021-09-05
    • 2016-05-09
    • 2015-05-07
    • 2017-02-11
    相关资源
    最近更新 更多