且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AttributeError:“ str”对象没有属性“ ndim”

更新时间:2023-12-02 22:52:40

您正在输入字符串列表模型,这是意料之外的。您可以使用 keras.preprocessing.text 模块将文本转换为整数序列。更具体地说,您可以准备数据,例如:

You are feeding a list of strings to a model which is something it does not expect. You can use keras.preprocessing.text module to convert the text to an integer sequence. More specifically you can prepare data like:

from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
tk = Tokenizer()
tk.fit_on_texts(texts)
index_list = tk.texts_to_sequences(texts)
x_train = pad_sequences(index_list, maxlen=maxlen)

现在 x_train (a n_samples *类型为 np.int 的maxlen ndarray是模型的合法输入。

Now x_train (a n_samples * maxlen ndarray of type np.int) is a legitimate input for the model.