训练 Keras 回归模型时出错

Error while training Keras regression model

对于这个新手问题深表歉意,我正在尝试使用 Keras 训练回归模型,但我在 model.fit() 中遇到错误。

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np

inputs = keras.Input(shape=(6,5), name="digits")
x = layers.Dense(64, activation="relu", name="dense_1")(inputs)
x = layers.Dense(64, activation="relu", name="dense_2")(x)
outputs = layers.Dense(1, activation="softmax", name="predictions")(x)

model = keras.Model(inputs=inputs, outputs=outputs)

x_train = np.array([[ 0,  1,  2,  3,  4],
                    [ 5,  6,  7,  8,  9],
                    [10, 11, 12, 13, 14],
                    [ 0,  1,  2,  3,  4],
                    [ 5,  6,  7,  8,  9],
                    [10, 11, 12, 13, 14]])

y_train = np.array([1, 2, 3, 1, 2, 3])

model.compile(loss=keras.losses.SparseCategoricalCrossentropy())

history = model.fit(x_train,y_train)

这是错误,这是什么意思,如何解决?我正在使用 TensorFlow 2.7.0.

Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 6, 5), found shape=(None, 5)

要修复错误,您需要完全清楚数据的输入形状和输出形状。从您的代码推断,有 3 个数据点要映射 [0,1,2,3,4]1[5,6,7,8,9]2[10,11,12,13,14]3 .

因此,输入形状为(5,),输出形状为(1,),即tf.keras.Inputy_train需要使用(5,)重塑为 (6,1).

此外,由于要进行回归,因此应使用适当的输出层激活函数和损失函数。 (见下面的例子)

最后,调整优化器类型、学习率和其他超参数以获得更好的性能。

示范:

inputs = tf.keras.Input(shape=(5,), name="digits")#input shape is (5,)
x = tf.keras.layers.Dense(64, activation="relu", name="dense_1")(inputs)
x = tf.keras.layers.Dense(64, activation="relu", name="dense_2")(x)
outputs = tf.keras.layers.Dense(1, name="predictions")(x)#use linear activation

model = tf.keras.Model(inputs, outputs)

x_train = np.array([[ 0,  1,  2,  3,  4],
                    [ 5,  6,  7,  8,  9],
                    [10, 11, 12, 13, 14],
                    [ 0,  1,  2,  3,  4],
                    [ 5,  6,  7,  8,  9],
                    [10, 11, 12, 13, 14]])

y_train = np.array([1, 2, 3, 1, 2, 3])[:,None]#reshape

model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001,momentum=0.99)
        ,loss=tf.keras.losses.MeanSquaredError())#use MSE

model.fit(x_train,y_train,epochs=500,verbose=0)

print(model.predict(x_train))
'''
outputs:
[[1.0019126]
 [2.010047 ]
 [3.0027502]
 [1.0019126]
 [2.010047 ]
 [3.0027502]]
'''