gpt4 book ai didi

python - Keras 使用什么损失函数?

转载 作者:行者123 更新时间:2023-11-30 09:26:54 24 4
gpt4 key购买 nike

import numpy as np
from random import randint
from sklearn.preprocessing import MinMaxScaler
import keras
from keras import backend as K
from keras.models import Sequential
from keras.layers import Activation
from keras.layers.core import Dense
from keras.optimizers import Adam
from keras.metrics import categorical_crossentropy

train_labels = [68, 65, 67, 71, 69, 72, 75, 70, 85, 83, 88, 80, 80, 78, 79, 85, 88, 86, 92, 91, 91, 93, 93, 90, 96, 97, 100, 100]

train_samples = [[2, 1, 73],[4, 0.5, 65],[3, 1, 70],[6, 1, 75],[7, 0.5, 68],[9, 1, 72],[3, 5, 70],[2, 6, 65],[4, 5, 78],[8, 3, 75],[9, 2, 80],[9, 4, 69],[2, 2, 88],[3, 1, 85],[7, 1, 83],[9, 1, 87],[3, 5, 88],[2, 7, 84],[7, 3, 88],[9, 4, 85],[4, 1, 93],[3, 1, 95],[8, 1, 93], [9, 0.5, 92], [3, 5, 94], [2, 7, 96], [8, 4, 97], [7, 5, 94]]

train_labels = np.array(train_labels)
train_samples = np.array(train_samples)

model = Sequential([
Dense(8, input_shape=(3,)),
Dense(16),
Dense(1)
])

print(model.summary())

model.compile(Adam(lr=0.0001), loss="????", metrics = ["accuracy"])

model.fit(train_samples, train_labels, validation_split = 0.1, batch_size=1, epochs=100, verbose = 2)

我正在尝试训练神经网络根据 sleep 时间、学习时间和当前类(class)的平均时间来预测考试成绩。我对神经网络很了解,所以我不知道该使用什么损失函数。我制作了一些 NN 的以下教程,准确率始终在 95% 左右,但是,无论我使用什么损失函数,准确率都完全为 0。有谁知道这是因为我还没有缩放我的训练集,或者可能知道损失函数使用?谢谢。

最佳答案

您预测的目标存在于连续空间中(回归,而不是分类)。损失应为 "mse"/"mean_squared_error"metric = ["mse"]

对于一般的神经网络,建议将输入标准化为大致 mean = 0std = 1。您可以轻松使用 scikit-learn 的 sklearn.preprocessing.StandardScaler() 来实现此目的。

您的项目的重构代码将如下所示(在 tensorflow-cpu==1.9keras==2.2.2 上测试):

import numpy as np
from sklearn.preprocessing import StandardScaler
import keras
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
print(f"Tensorflow version: {tf.__version__}, Keras version: {keras.__version__}")

# data
train_labels = [68, 65, 67, 71, 69, 72, 75, 70, 85, 83, 88, 80, 80, 78, 79, 85, 88, 86, 92, 91, 91, 93, 93, 90, 96, 97, 100, 100]
train_samples = [[2, 1, 73],[4, 0.5, 65],[3, 1, 70],[6, 1, 75],[7, 0.5, 68],[9, 1, 72],[3, 5, 70],[2, 6, 65],[4, 5, 78],[8, 3, 75],[9, 2, 80],[9, 4, 69],[2, 2, 88],[3, 1, 85],[7, 1, 83],[9, 1, 87],[3, 5, 88],[2, 7, 84],[7, 3, 88],[9, 4, 85],[4, 1, 93],[3, 1, 95],[8, 1, 93], [9, 0.5, 92], [3, 5, 94], [2, 7, 96], [8, 4, 97], [7, 5, 94]]
train_labels = np.array(train_labels)
train_samples = np.array(train_samples)
# preprocessing (min-max or standard scaler is fine)
sc = StandardScaler()
train_samples_scaled = sc.fit_transform(train_samples)
print(f"Feature means before scaling: {train_samples.mean(axis=0)}, Feature means after scaling: {train_samples_scaled.mean(axis=0)}")

# neural network
model = Sequential([
Dense(8, input_shape=(3,), activation='relu'),
Dense(16, activation='relu'),
Dense(1, activation='linear')
])
model.compile(Adam(lr=0.001), loss="mse", metrics = ["mse"])
# training
model.fit(train_samples_scaled, train_labels, validation_split = 0.1, batch_size=5, epochs=20, verbose = 2)

关于python - Keras 使用什么损失函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51575306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com