【问题标题】:Upon prediction, my loaded model is giving me an AttributeError根据预测,我加载的模型给了我一个 AttributeError
【发布时间】:2020-01-08 17:10:30
【问题描述】:

总的来说,我对 Tensorflow 和机器学习还很陌生,但我知道我已经建立了一个小型模型。虽然,当我加载并使用model.predict 时,我得到一个属性错误:

import tensorflow as tf
import numpy as np

checkpoint_path = "training_1/cp.ckpt"
# Hyperparamters
vocab_size = 2000
embedding_dim = 16
max_length = 1
trunc_type = "post"
padding_type = "post"
oov_tok = "<OOV>"
training_size = 100

model = tf.keras.Sequential([
    tf.keras.layers.Embedding(
        vocab_size, embedding_dim, input_length=max_length),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
    tf.keras.layers.Dense(128, activation="relu"),
    tf.keras.layers.Dense(3, activation="softmax")
])

# Compile the model
model.compile(loss="sparse_categorical_crossentropy",
              optimizer="adam", metrics=["accuracy"])

model.load_weights(checkpoint_path)


test = ["Example of text here"]


prediction = model.predict(test)
print(prediction)
Traceback (most recent call last):
  File "./ModelTest.py", line 36, in <module>
    prediction = model.predict(test)
  File "/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 1060, in predict
    x, check_steps=True, steps_name='steps', steps=steps)
  File "/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 2651, in _standardize_user_data
    exception_prefix='input')
  File "/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_utils.py", line 334, in standardize_input_data
    standardize_single_array(x, shape) for (x, shape) in zip(data, shapes)
  File "/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_utils.py", line 334, in <listcomp>
    standardize_single_array(x, shape) for (x, shape) in zip(data, shapes)
  File "/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_utils.py", line 265, in standardize_single_array
    if (x.shape is not None and len(x.shape) == 1 and
AttributeError: 'str' object has no attribute 'shape'

【问题讨论】:

标签: python machine-learning keras deep-learning tf.keras


【解决方案1】:

确保您提供的输入格式适合您构建的模型。在您的情况下,Embedding 层需要一个 2D 张量。数据应该是一个看起来像这样的 numpy 数组:[[0, 2, 64], [24, 6, 8]]。那里的每个数字代表一个单词,每个数字序列代表一个短语。整个张量代表一批序列。在我的示例中,这是一组 2 个序列,每个序列有 3 个单词。

您需要做的是使用您正在加载的模型的正确词汇表标记"Example of text here"。完成后,您将得到一个类似[[3, 8, 4, 6]] 的数组,其中每个数字对应于"Example of text here" 中的一个单词。如何正确标记它取决于它训练的数据是如何标记的,如果不知道你从哪里得到training_1/cp.ckpt,我们不知道这一点。

【讨论】:

  • 哦,我确实尝试过标记化和填充以匹配模型中的数据,但我认为我做错了,因为我的值到处都是。
  • @deluxeme 这些值应该到处可见。标记化序列中的每个数字只是词汇表中单词的任意 ID。当我将"example of text here" 与我现在恰好用于我的项目的词汇进行标记时,我得到[460, 4, 3001, 130]。你的看起来会有所不同(除非你使用的是 keras 的内置 IMDB 数据集),但它应该看起来同样随机。
  • 我知道这些词是被标记的,但 model.predict 的输出是:[[0.30352557 0.26858705 0.42788738]] 这是否意味着在 3 个类中。它认为三等舱最合适?
  • @deluxeme 是的,就是这个意思。
  • 抱歉一连串的问题,但我应该使用与初始模型相同的标记器实例还是创建一个新的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-23
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-12
  • 2020-06-03
相关资源
最近更新 更多