简体   繁体   English

神经网络不想在随机场合使用相同的数据集学习

[英]Neural Network Doesn't wanna learn in random occasion using the same dataset

I have successfully created a supervised sequential model.我已经成功创建了一个有监督的序列模型。 The input is 4 dimensional with 1-dimensional output.输入是 4 维的,输出是 1 维的。 The dataset is scaled down using a minmaxscaler.使用 minmaxscaler 缩小数据集。 I have 5 hidden layers with 12 neurons at each layer.我有 5 个隐藏层,每层有 12 个神经元。 All the kernel initializers for input, hidden and output layer are he_normal.输入层、隐藏层和输出层的所有内核初始化器都是 he_normal。 Activation is Relu for all layers including input and output.对于包括输入和输出在内的所有层,激活都是 Relu。

I have 500 samples, batch size 128 data.我有 500 个样本,批量大小为 128 个数据。

Sometimes the loss function (MSE) is not improving, see below:有时损失函数 (MSE) 没有改善,见下文:

Epoch 1/2000
14/14 [==============================] - 0s 10ms/step - loss: 0.6348 - mape: 77.9929 - mse: 0.6348 - mae: 0.7799 - val_mse: 0.5862 - val_mae: 0.7431 - val_mape: 74.3140 - val_loss: 0.5862
Epoch 2/2000
14/14 [==============================] - 0s 2ms/step - loss: 0.6348 - mape: 77.9929 - mse: 0.6348 - mae: 0.7799 - val_mse: 0.5862 - val_mae: 0.7431 - val_mape: 74.3140 - val_loss: 0.5862
Epoch 3/2000
14/14 [==============================] - 0s 3ms/step - loss: 0.6348 - mape: 77.9929 - mse: 0.6348 - mae: 0.7799 - val_mse: 0.5862 - val_mae: 0.7431 - val_mape: 74.3140 - val_loss: 0.5862
Epoch 4/2000
14/14 [==============================] - 0s 2ms/step - loss: 0.6348 - mape: 77.9929 - mse: 0.6348 - mae: 0.7799 - val_mse: 0.5862 - val_mae: 0.7431 - val_mape: 74.3140 - val_loss: 0.5862
Epoch 5/2000
14/14 [==============================] - 0s 2ms/step - loss: 0.6348 - mape: 77.9929 - mse: 0.6348 - mae: 0.7799 - val_mse: 0.5862 - val_mae: 0.7431 - val_mape: 74.3140 - val_loss: 0.5862
Epoch 6/2000
14/14 [==============================] - 0s 3ms/step - loss: 0.6348 - mape: 77.9929 - mse: 0.6348 - mae: 0.7799 - val_mse: 0.5862 - val_mae: 0.7431 - val_mape: 74.3140 - val_loss: 0.5862
Epoch 7/2000
14/14 [==============================] - 0s 3ms/step - loss: 0.6348 - mape: 77.9929 - mse: 0.6348 - mae: 0.7799 - val_mse: 0.5862 - val_mae: 0.7431 - val_mape: 74.3140 - val_loss: 0.5862
Epoch 8/2000
14/14 [==============================] - 0s 2ms/step - loss: 0.6348 - mape: 77.9929 - mse: 0.6348 - mae: 0.7799 - val_mse: 0.5862 - val_mae: 0.7431 - val_mape: 74.3140 - val_loss: 0.5862

and then I stop the run ( ctrl+z ) and re-execute the command again and it becomes:然后我停止运行( ctrl+z )并再次重新执行命令,它变成:

Epoch 1/2000
14/14 [==============================] - 0s 10ms/step - loss: 0.2896 - mae: 0.4896 - mse: 0.2896 - mape: 48.9583 - val_mape: 49.4280 - val_loss: 0.2901 - val_mse: 0.2901 - val_mae: 0.4943
Epoch 2/2000
14/14 [==============================] - 0s 2ms/step - loss: 0.2425 - mae: 0.4380 - mse: 0.2425 - mape: 43.8044 - val_mape: 43.7562 - val_loss: 0.2393 - val_mse: 0.2393 - val_mae: 0.4376
Epoch 3/2000
14/14 [==============================] - 0s 2ms/step - loss: 0.2016 - mae: 0.3890 - mse: 0.2016 - mape: 38.9016 - val_mape: 38.2766 - val_loss: 0.1962 - val_mse: 0.1962 - val_mae: 0.3828
Epoch 4/2000
14/14 [==============================] - 0s 2ms/step - loss: 0.1680 - mae: 0.3478 - mse: 0.1680 - mape: 34.7794 - val_mape: 33.7354 - val_loss: 0.1616 - val_mse: 0.1616 - val_mae: 0.3374
Epoch 5/2000
14/14 [==============================] - 0s 2ms/step - loss: 0.1413 - mae: 0.3156 - mse: 0.1413 - mape: 31.5616 - val_mape: 30.1136 - val_loss: 0.1346 - val_mse: 0.1346 - val_mae: 0.3011
Epoch 6/2000
14/14 [==============================] - 0s 3ms/step - loss: 0.1207 - mae: 0.2890 - mse: 0.1207 - mape: 28.9029 - val_mape: 27.5114 - val_loss: 0.1143 - val_mse: 0.1143 - val_mae: 0.2751
Epoch 7/2000
14/14 [==============================] - 0s 2ms/step - loss: 0.1061 - mae: 0.2693 - mse: 0.1061 - mape: 26.9261 - val_mape: 25.3930 - val_loss: 0.0994 - val_mse: 0.0994 - val_mae: 0.2539
Epoch 8/2000
14/14 [==============================] - 0s 2ms/step - loss: 0.0957 - mae: 0.2548 - mse: 0.0957 - mape: 25.4783 - val_mape: 23.8949 - val_loss: 0.0892 - val_mse: 0.0892 - val_mae: 0.2389

I wonder, what happened during the first run?我想知道,第一次运行期间发生了什么? Why, with the same dataset, the model was working on the second run and it was not working on the 1st run?为什么使用相同的数据集,模型在第二次运行时可以工作,而在第一次运行时却不起作用? Is there any way that I can do programmatically to ensure that at each execution of the script the model will learn?有什么方法可以通过编程来确保模型在每次执行脚本时都能学习? I want to include the model in an adaptive sampling algorithm that executes the training phase more than once in one go.我想将模型包含在一次多次执行训练阶段的自适应采样算法中。

Any help will be very appreciated!任何帮助将不胜感激!

Cheers, PG干杯,PG

PS: Below is the code snippet, data sample, network architecture PS:下面是代码片段、数据样本、网络架构

Data Samples (4 inputs [Q_in,Tamb,T_in,H_drop] 1 output [eff_rcv])数据样本(4 个输入 [Q_in,Tamb,T_in,H_drop] 1 个输出 [eff_rcv])

Q_in,Tamb,T_in,H_drop,eff_rcv
609496059.800000,271.792985,807.218964,35.445493,0.870245
783459291.300000,314.275101,828.283384,31.161965,0.923094
391056216.100000,307.686423,816.411201,39.310067,0.748878
289120690.100000,292.437028,828.729067,29.114747,0.812813
508971245.000000,284.898844,812.819974,38.611096,0.817931
.
.

Code Snippet代码片段

#split the raw into input (X) and ouput(y)
X_raw = df[df.columns[0:inputdim]].to_numpy()

#Convert to 2D array
y_raw = df[df.columns[-1]].to_numpy()
y_raw = y_raw.reshape(-1,1)

#Import the scaler
mm = MinMaxScaler()

#Scaling the data
X_scaled = mm.fit_transform(X_raw)
y_scaled = mm.fit_transform(y_raw)

#Split into train test - 85% training 15% testing
Xtrain,Xtest,ytrain,ytest = train_test_split(X_scaled,y_scaled,test_size=0.15)

######################  BUILD MODEL ############################ 
inputdim = 4
outputdim = 1

#Number of neurons in each hidden layer < 2*input_dim
num_neurons = 3*inputdim

#Neural network architecture
network_layout = []
for i in range(5):
    network_layout.append(num_neurons)
#Building the neural network#
model = Sequential()

#Adding input layer and first hidden layer
model.add(Dense(network_layout[0],  
                    name = "Input",
                    input_dim=inputdim,
                    kernel_initializer=initializers.RandomNormal(),
                    bias_initializer=initializers.Zeros(),
                    use_bias=True,
                    activation=activation))

#Adding the rest of hidden layer
for numneurons in network_layout[1:]:
        model.add(Dense(numneurons,
                        kernel_initializer=initializers.RandomNormal(),
                        bias_initializer=initializers.Zeros(),
                        activation=activation))

#Adding the output layer
model.add(Dense(outputdim,
                    name="Output",
                    kernel_initializer=initializers.RandomNormal(),
                    bias_initializer=initializers.Zeros(),
                    activation="relu"))

backend.set_epsilon(1)

#Compiling the model
model.compile(optimizer=opt,loss='mse',metrics=['mse','mae','mape'])
model.summary()

#Training the model

history = model.fit(x=Xtrain,y=ytrain,validation_data=(Xtest,ytest),batch_size=batch_size,epochs=epochs)

You could set the random seed before training your model.您可以在训练模型之前设置随机种子。 That way, you can ensure that the outcome will be same with each run of the script.这样,您可以确保每次运行脚本时结果都相同。

tf.keras.backend.clear_session()
tf.random.set_seed(1)

Concerning why it was not improving in the first snipped: it is possible that you ran into a local minimum you couldnt get out of.关于为什么在第一次剪辑时没有改善:您可能遇到了无法摆脱的局部最小值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM