我对 keras 很陌生,并试图构建一个模型,该模型将列表作为输入并返回 1 到 16 之间的数字(或 0 到 15,我有 16 个类)
到目前为止,这是我的代码:
import numpy as np
from tensorflow.keras.layers.experimental.preprocessing import TextVectorization
from tensorflow.keras import layers
from tensorflow import keras
import tensorflow as tf
vectorizer = TextVectorization(output_mode = "int")
'''
vocab_data = np.array(["In animus fert nova"])
vectorizer.adapt(vocab_data) #wichtig
print(vectorizer(vocab_data))
'''
# 2 Möglichkeiten für die ersten 4 Metren --> 2^4 = 16 verschiedene Hexameter
num_Hexameter = 16
inputs = keras.Input(shape = (None,1), dtype=tf.int64)
x = layers.Dense(30)(inputs)
x = layers.Dense(20)(x)
x = layers.Dense(15)(x)
x = layers.Dense(10)(x)
x = layers.Dense(5)(x)
outputs = layers.Dense(num_Hexameter, activation = "softmax")(x)
model = keras.Model(inputs = inputs, outputs = outputs)
model.summary()
model.compile(optimizer = keras.optimizers.RMSprop(learning_rate=1e-3),
loss=keras.losses.SparseCategoricalCrossentropy(), metrics=["accuracy"])
test_train_data_raw = np.array([["In nova fert animus mutatas dicere formas"],
["corpora di coeptis nam vos mutastis et illas"],
["adspirate meis primaque ab origine mundi"],
["ad mea perpetuum deducite tempora carmen"]])
vectorizer.adapt(test_train_data_raw)
test_train_data = vectorizer(test_train_data_raw)
test_train_data_lbl = np.array([[0, 0, 0, 0, 0, 0, 0, 3],[0, 0, 0, 0, 0, 0, 0, 7], [0, 0, 0, 0, 0, 0, 0, 10], [0, 0, 0, 0, 0, 0, 0, 2]])
test_train_data_lbl = np.array([3, 7, 10, 2])
print(test_train_data_lbl.shape)
history = model.fit(test_train_data, test_train_data_lbl, batch_size = 2, epochs = 10)
print(history.history)
test = vectorizer(np.array([["In nova fert animus mutatas dicere formas"]]))
print(test)
print(model.predict(test).shape)
问题是如果我不test_train_data_lbl = np.array([3, 7, 10, 2])注释该行,它会给我错误:ValueError: Shape mismatch: The shape of labels (received (2, 1)) should equal the shape of logits except for the last dimension (received (2, 8, 16))
注释该行不会导致错误,但结果model.predict(test)将是一个带有 shape 的数组(1, 7, 16)。我知道 7 来自我拥有的 7 个单词和 16 来自类的数量,但我需要这个形状(1, 16),以便我可以预测该行将在哪个类中。
我现在也必须减少训练数据,但我首先想让模型在生成训练数据之前没有错误地工作。