【问题标题】:TypeError: Exception encountered when calling layer "conv1d" (type Conv1D)TypeError:调用层“conv1d”时遇到异常(类型Conv1D)
【发布时间】:2021-11-26 03:33:01
【问题描述】:

我正在从事一个项目“新闻分类”。模型必须将给定文本分类(多类分类问题)为商业、娱乐、政治、体育和科技。

我在 Google Colab 上使用 TensorFlow==2.7.0。我训练了 7 个不同的模型。之后,对其进行训练和预测。与所有模型相比,Conv1d 表现最好。表现最好的模型保存了model_2.save('saved_model/my_model')。它一直做得很好。

但是,当我想使用代码加载保存的模型时

loaded_model = tf.keras.models.load_model('saved_model/my_model') 那么,我得到以下异常:

TypeError                                 Traceback (most recent call last)
<ipython-input-129-c92edaf0db7f> in <module>()
----> 1 load_model = tf.keras.models.load_model('saved_model/my_model')
      2 # load_model.preditct(val_sentences)

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape, allow_broadcast)
    547       str_values = [compat.as_bytes(x) for x in proto_values]
    548     except TypeError:
--> 549       raise TypeError(f"Failed to convert elements of {values} to Tensor. "
    550                       "Consider casting elements to a supported type. See "
    551                       "https://www.tensorflow.org/api_docs/python/tf/dtypes "

TypeError: Exception encountered when calling layer "conv1d" (type Conv1D).

Failed to convert elements of tf.RaggedTensor(values=tf.RaggedTensor(values=Tensor("Placeholder:0", shape=(None, 128), dtype=float32), row_splits=Tensor("Placeholder_1:0", shape=(None,), dtype=int64)), row_splits=Tensor("conv1d/Conv1D/RaggedExpandDims/RaggedFromUniformRowLength/RowPartitionFromUniformRowLength/mul:0", shape=(None,), dtype=int64)) to Tensor. Consider casting elements to a supported type. See https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes.

Call arguments received:
  • inputs=tf.RaggedTensor(values=Tensor("Placeholder:0", shape=(None, 128), dtype=float32), row_splits=Tensor("Placeholder_1:0", shape=(None,), dtype=int64))

【问题讨论】:

标签: python tensorflow machine-learning keras deep-learning


【解决方案1】:

看起来您需要在文件名上添加 .h5。您需要将 .h5 添加到文件名的末尾。 而不是:

model_2.save('saved_model/my_model') 试试

model_2.save('saved_model/my_model.h5')
#Notice the .h5 -------------------^

您还需要更改loaded_model = tf.keras.models.load_model('saved_model/my_model')loaded_model = tf.keras.models.load_model('saved_model/my_model.h5')

【讨论】:

    猜你喜欢
    • 2022-06-27
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2023-02-09
    • 2021-12-31
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多