Keras损失极高 [英] Keras Extremely High Loss
问题描述
我正在尝试通过特征来预测价格. 我选择了一个非常简单的模型,但它的工作原理很奇怪.损失函数非常高,我看不出问题出在哪里.
I'm trying to predict price by characteristics. I chose a pretty simple model, but it works very strange. Loss function is extremely high and I can't see where the problem is.
这是我的模特:
# define base model
def baseline_model():
# create model
model = Sequential()
model.add(Dense(62, input_dim = 62, kernel_initializer='normal', activation='relu'))
model.add(Dense(31, kernel_initializer='normal', activation='relu'))
model.add(Dense(15, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, kernel_initializer='normal'))
# Compile model
model.compile(loss='mean_squared_error', optimizer='adam')
return model
这就是我准备数据的方式:(一次热,我拆分了所有数据以进行训练和测试)
That's how I prepare the data: (One-Hot and I split all data to train and test)
df = encode_onehot(dataframe, cols=['Shape', 'Cut', 'Color', 'Clarity', 'Polish', 'Symmetry', 'Culet', '\tFluorescence'])
dataset = df.values
X = dataset[1:,4:66]
Y = dataset[1:,2]
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.25, random_state=42)
最后,培训:
baseline_model().fit(X_train, y_train, epochs=10, batch_size=64)
scores = baseline_model().evaluate(X_test, y_test, verbose=0)
print(baseline_model().summary())
结果非常可悲:
Epoch 1/10
149767/149767 [==============================] - 4s - loss: 104759338.0333
Epoch 2/10
149767/149767 [==============================] - 4s - loss: 104594236.9627
Epoch 3/10
149767/149767 [==============================] - 4s - loss: 104556662.2948
它并没有变得更好.
我在做什么错了?
推荐答案
正如@ Yu-Yang所说,您正在使用均方误差作为损失函数.在损失值将非常大之前,我遇到了同样的问题,将损失函数更改为 mean_squared_logarithmic_error 时,我得到了预期的结果.
As @Yu-Yang said you are using mean squared error as loss function. I had this same problem before where the loss value will be very large, on changing the loss function to mean_squared_logarithmic_error, I got the desired result.
model %>% compile(
optimizer = optimizer_rmsprop(lr=0.0001),
loss = loss_mean_squared_logarithmic_error,
metrics = c("accuracy")
)
损失值更改为
史诗1/10
326981/326981 [==============================]-17s-损失:0.0048-acc:0.9896
Epoch 1/10
326981/326981 [==============================] - 17s - loss: 0.0048 - acc: 0.9896
希望这对您有用!
这篇关于Keras损失极高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!