Keras 中使用神经网络的多输出回归

数据挖掘 机器学习 神经网络 喀拉斯 回归
2022-03-12 17:59:05

我有一个带有输入和 2 个输出列的 .xlsx Excel 文件。该文件中有一些坐标和输出,例如: x= 10 y1=15 y2=20 x= 20 y1=14 y2=22 ...我正在尝试使用 tensorflow 进行回归。但不知何故,我无法做到。我将代码留在这里,如果有人可以提供帮助,我会很高兴!我也准备好了测试数据。

training_data = pd.read_excel(...\training_data.xlsx',sheet_name="i1-o2")

training_data_X = training_data['i1']

training_data_Y = training_data[['o1','o2']]

testing_data = data = pd.read_excel(....\testing_data.xlsx',sheet_name="i1-o2")

testing_data_X = testing_data['i1']

testing_data_Y = testing_data[['o1','o2']]


model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation=tf.nn.relu),
    tf.keras.layers.Dense(128, activation=tf.nn.relu),
    tf.keras.layers.Dense(128, activation=tf.nn.relu),
    tf.keras.layers.Dense(128, activation=tf.nn.relu),
    tf.keras.layers.Dense(128, activation=tf.nn.relu),
    tf.keras.layers.Dense(128, activation=tf.nn.relu),
    tf.keras.layers.Dense(6, activation='linear')
])

model.compile(optimizer='adam',
              loss='mean_squared_error',
              metrics=['accuracy'])
model.fit(training_data_X,training_data_Y,epochs=10,batch_size=100)

val_loss,val_acc = model.evaluate(testing_data_X,testing_data_Y)
print(val_loss,val_acc)
2个回答

我发现了一些错误:

  1. 输入数据必须是numpy对象,而不是pandas
  2. 该网络有 6 个输出节点,而不是 2 个
  3. 层数完全夸大了恕我直言
  4. Flatten()开头的图层不正确
  5. 您调用 ReLU 的方式不正确

这应该足够了:

from tf.keras.models import Sequential
from tf.keras.layers import Dense
from tf.keras.activations import relu

model = Sequential([
    tf.keras.layers.Dense(128, activation = relu),

    tf.keras.layers.Dense(128, activation = relu),

    tf.keras.layers.Dense(2, activation = None)
])

检查损失是否在此时起作用。或者,您需要使用 Keras 后端函数编写自己的自定义损失函数。

为什么要在此使用 Flatten 层?似乎它已经是一个数字数据。

正如@desertnaut 提到的,因为这是一个回归设置,你应该使用 mse 或 mae