gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:03:16 27 4
gpt4 key购买 nike

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

最佳答案

确保您提供的输入格式正确,适用于您构建的模型。在您的情况下,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

关于python - 根据预测,我加载的模型给我一个 AttributeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57811573/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com