[英]How to to train multi regression output with tf.data.Dataset?
Only the first output parameter is learned to be properly estimated during training of a multi regression output.net.在多元回归 output.net 的训练过程中,只有第一个 output 参数被学习到可以被正确估计。 Second and subsequent parameters only seem to follow first parameter.
第二个和后续参数似乎只跟在第一个参数之后。 It seems, that ground truth for second output parameter is not used during training.
似乎在训练期间没有使用第二个 output 参数的基本事实。 How do I shape tf.data.Dataset and input it into model.fit() function so second output parameter is trained?
我如何塑造 tf.data.Dataset 并将其输入到 model.fit() function 以便训练第二个 output 参数?
import tensorflow as tf
import pandas as pd
from tensorflow import keras
from keras import layers
#create dataset from csv
file = pd.read_csv( 'minimalDataset.csv', skipinitialspace = True)
input = file["input"].values
output1 = file["output1"].values
output2 = file["output2"].values
dataset = tf.data.Dataset.from_tensor_slices((input, (output1, output2))).batch(4)
#create multi output regression net
input_layer = keras.Input(shape=(1,))
x = layers.Dense(20, activation="relu")(input_layer)
x = layers.Dense(60, activation="relu")(x)
output_layer = layers.Dense(2)(x)
model = keras.Model(input_layer, output_layer)
model.compile(optimizer="adam", loss="mean_squared_error")
#train model and make prediction (deliberately overfitting to illustrate problem)
model.fit(dataset, epochs=500)
prediction = model.predict(dataset)
minimalDataset.csv
and predictions: minimalDataset.csv
和预测:
input![]() |
output1![]() |
output2![]() |
prediction_output1![]() |
prediction_output2![]() |
|
---|---|---|---|---|---|
0 ![]() |
-1 ![]() |
1 ![]() |
-0.989956 ![]() |
-0.989964 ![]() |
|
1 ![]() |
2 ![]() |
0 ![]() |
1.834444 ![]() |
1.845085 ![]() |
|
2 ![]() |
0 ![]() |
2 ![]() |
0.640249 ![]() |
0.596099 ![]() |
|
3 ![]() |
1 ![]() |
-1 ![]() |
0.621426 ![]() |
0.646796 ![]() |
If I create two independent dense final layers the second parameter is learned accurately but I get two losses:如果我创建两个独立的密集最终层,第二个参数会被准确学习,但我会得到两个损失:
output_layer = (layers.Dense(1)(x), layers.Dense(1)(x))
Note: I want to use tf.data.Dataset because I build a 20k image/csv with it and do per-element transformations as preprocessing.注意:我想使用 tf.data.Dataset,因为我用它构建了一个 20k 图像/csv,并进行了每个元素的转换作为预处理。
tf.data.Dataset.from_tensor_slices() slices along the first dimension. tf.data.Dataset.from_tensor_slices() 沿第一个维度切片。 Because of this the input and output tensors need to be transposed:
因此,需要转置输入和 output 张量:
dataset = tf.data.Dataset.from_tensor_slices((tf.transpose(input), (tf.transpose([output1, output2])))).batch(4)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.