Tensorflow-Keras:简单的曲线拟合任务表现不佳

时间:2019-12-09 07:05:44

标签: matlab regression tensorflow2.0

我正在尝试为一个玩具回归问题实现一个非常简单的多层MLP,该玩具回归问题具有一个变量(维度= 1)和一个目标(维度= 1)。这是一个零噪声的简单曲线拟合问题。


Matlab \深度学习工具箱

在具有100个神经元和双曲正切激活的单个隐藏层的MLP上使用levenberg-marquardt反向传播,我几乎不费吹灰之力就获得了相当不错的性能:

MSE = 7.18e-08

绘制预测和目标非常精确。

Python \ Tensorflow \ Keras

使用与Matlab中相同的网络设置,几乎无需培训。无论我多么努力地调整训练参数或切换优化器。

MSE = 0.12900154

在这种情况下,预测图是一条甚至无法跟随目标曲线的振荡的曲线。

使用RELU激活隐藏层可以获得更好的效果,但是我们仍然很遥远:

MSE = 0.0582045

这是我在Python中使用的代码:

#  IMPORT LIBRARIES
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras


#  IMPORT DATASET FROM CSV FILE, SHUFFLE TRAINING SET 
#  AND MAKE NUMPY ARRAY FOR TRAINING (DATA ARE ALREADY NORMALIZED)
dataset_path = "C:/Users/Rob/Desktop/Learning1.csv"
Learning_Dataset = pd.read_csv(dataset_path
                          , comment='\t',sep=","
                          ,skipinitialspace=False)
Learning_Dataset = Learning_Dataset.sample(frac = 1)  # SHUFFLING


test_dataset_path = "C:/Users/Rob/Desktop/Test1.csv"
Test_Dataset = pd.read_csv(test_dataset_path
                          , comment='\t',sep=","
                          ,skipinitialspace=False)


Learning_Target = Learning_Dataset.pop('Target')
Test_Target = Test_Dataset.pop('Target')

Learning_Dataset = np.array(Learning_Dataset,dtype = "float32")
Test_Dataset = np.array(Test_Dataset,dtype = "float32")
Learning_Target = np.array(Learning_Target,dtype = "float32")
Test_Target = np.array(Test_Target,dtype = "float32")





#  DEFINE SIMPLE MLP MODEL
inputs = tf.keras.layers.Input(shape=(1,))
x = tf.keras.layers.Dense(100, activation='relu')(inputs)
y = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(inputs=inputs, outputs=y)




#  TRAIN MODEL
opt = tf.keras.optimizers.RMSprop(learning_rate = 0.001,
                                  rho = 0.9,
                                  momentum = 0.0,
                                  epsilon = 1e-07,
                                  centered = False)
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=100)
model.compile(optimizer = opt,
              loss = 'mse',
              metrics = ['mse'])


model.fit(Learning_Dataset, 
          Learning_Target,  
          epochs=500, 
          validation_split = 0.2, 
          verbose=0, 
          callbacks=[early_stop], 
          shuffle = False, 
          batch_size = 100)




#  INFERENCE AND CHECK ACCURACY
Predictions = model.predict(Test_Dataset)
Predictions = Predictions.reshape(10000)

print(np.square(np.subtract(Test_Target,Predictions)).mean()) #  MSE

plt.plot(Test_Dataset,Test_Target,'o',Test_Dataset,Predictions,'o')
plt.legend(('Target','Model Prediction'))
plt.show()

我在做什么错了?

谢谢

0 个答案:

没有答案