根据预测,我加载的模型给我一个AttributeError

时间:2019-09-05 19:00:41

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

总体上来说,我对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'

1 个答案:

答案 0 :(得分: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的情况下,我们也不知道。