简体   繁体   English

训练 Keras 回归 model 时出错

[英]Error while training Keras regression model

Apologies for this newbie question, I'm trying to train a regression model with Keras, but I get an error in model.fit() .为这个新手问题道歉,我正在尝试用 Keras 训练回归 model,但在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)

This is the error, what does it mean and how to fix this?这是错误,它是什么意思以及如何解决这个问题? I'm using TensorFlow 2.7.0.我正在使用 TensorFlow 2.7.0。

Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 6, 5), found shape=(None, 5)图层“模型”的输入 0 与图层不兼容:预期形状=(None, 6, 5),发现形状=(None, 5)

To fix the error, you need to be completely clear about the input shape and output shape of the data.要修复错误,您需要完全清楚数据的输入形状和 output 形状。 Inferring from your codes, there are 3 data points where you want to map [0,1,2,3,4] to 1 , [5,6,7,8,9] to 2 and [10,11,12,13,14] to 3 .从您的代码推断,有 3 个数据点您想要 map [0,1,2,3,4]1[5,6,7,8,9]2[10,11,12,13,14]3

Therefore, the input shape is (5,) and the output shape is (1,) ,ie, (5,) should be used in tf.keras.Input and y_train needs to be reshaped into (6,1) .因此,输入形状为(5,) ,output 形状为(1,) ,即(5,)应在tf.keras.Input中使用,并且y_train需要重新整形为(6,1)

Moreover, as you want to do regression, an appropriate activation function of the output layer and loss function should be used.此外,由于要进行回归,因此应使用 output 层的适当激活 function 和损失 function。 (See example below) (见下面的例子)

Finally, adjust the optimizer type, learning rate and other hyperparameters for better performance.最后,调整优化器类型、学习率和其他超参数以获得更好的性能。

Demonstration:示范:

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]]
'''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 训练逻辑回归 model 时出错 - Getting an error while training a logistic regression model 在 Keras 中训练 model 时出现值错误 - Value error while training model in Keras 在 4600000 行数据上训练 keras model 时出现 Memory 错误 - Memory error while training the keras model on 4600000 rows data 当训练时使用的 keras 版本未知时,如何加载具有版本不匹配错误的 Keras 模型 - How to load Keras model with version mismatch error when keras version used while training is not known 在Keras中训练多元回归模型时损失值非常大 - Very large loss values when training multiple regression model in Keras 训练Keras顺序模型时损失不会减少 - Loss is not decreasing while training Keras Sequential Model 使用 keras 训练模型时出现 TypeError 和 ValueError - TypeError and ValueError while training model with keras Keras:如何在每批训练时修改 keras model 的输入 - Keras: how to modify input of keras model while training for every batch 在 tensorflow 中训练线性回归 model 时出现“字典更新序列元素 #0 错误” - Getting "dictionary update sequence element #0 error" while training linear regression model in tensorflow 使用Keras(Tensorflow)训练模型时出现形状错误 - Shape error when training a model with Keras (Tensorflow)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM