为什么我反数据的时候MSE会很高

问题描述 投票:0回答:0

我建立了一个回归模型来预测使用足底压力的力板。在这种情况下,我正在尝试使用 CNN 模型。我有 2 个不同的数据集,数据集 A(力板数据)和数据集 B(足底压力数据)。然后在训练数据之前,我首先使用 minmaxscaler() 对数据进行归一化。在培训期间,我取得了不错的成绩和较低的 MSE。然后我尝试测试模型并返回使用反函数归一化为原始数据的预测数据。我把数据取反后,再看MSE值,取反后的MSE值很高。尽管从预测结果来看我认为非常好。为什么这个MSE值突然这么高

数据如下所示:

测力台数据:[ 4.46733, 4.39629, -34.2351 , -4077.23 , -6206.81 , -874.539 ]

测力板数据形状:(15000,6)

SmartInsole 数据:[ 0. 0. 0. 13. 1. 0. 0. 0. 0. 0. 15. 92. 60. 0. 36. 0. 0. 0. 0. 0. 0. 62. 80. 58. 37. 0. 0. 0. 0. 40. 83. 72. 32. 22. 0. 0. 0. 0. 0. 0. 98. 108. 74. 56. 30. 17. 0. 0. 44. 121. 127. 83. 0. 0. 0. 0. 0. 3. 83. 64. 63. 63. 77. 70. 43. 55. 115. 138. 144. 137. 0. 0. 0. 0. 66. 107. 127. 146. 150. 52. 0. 0. 0. 129. 133. 18. 0. 0. 0.]

SmartInsole 数据形状:(15000,89)

这里是我的型号代码:

## Load Data
Insole = pd.read_csv('1225_Rwalk10min1_list.txt', header=None, low_memory=False)
SIData =  np.array(Insole)

df = pd.read_csv('1225_Rwalk10min.csv', low_memory=False)
columns = ['Fx','Fy','Fz','Mx','My','Mz']
selected_df = df[columns]
FPDatas = selected_df[:15000]

label = pd.read_csv('label.txt', header=None, low_memory=False)
labelData =  np.array(label).astype('float32')

SmartInsole = np.array(SIData[:15000]).astype('float32')
FPData = np.array(FPDatas).astype('float32')

Label = np.array(labelData[:15000]).astype('float32')

SIlabeled = np.concatenate((Label, SmartInsole), axis=1)
SIlabeled = np.array(SIlabeled).astype('float32')
## End Load Data

# Data Normalization
minInsole = SIlabeled.min()
maxInsole = SIlabeled.max()
xscale = (SIlabeled - minInsole) / ( maxInsole - minInsole )

FPmax = []
FPmin = []
yscale = []

for i in range(0,6):
    minFP = FPData[:,i].min()
    maxFP = FPData[:,i].max()
    FPmin.append(minFP)
    FPmax.append(maxFP)

FPmin = np.array(FPmin)
FPmax = np.array(FPmax)

for i in range(0,6):
  scale = (FPData[:,i] - FPmin[i]) / ( FPmax[i] - FPmin[i] )
  yscale.append(scale)
yscale = np.array(yscale)
yscale = yscale.transpose()

#End Data Normalization

# Spliting Data
sample_size = xscale.shape[0]
time_steps  = xscale.shape[1]
input_dimension = 1

train_data_reshaped = xscale.reshape(sample_size,time_steps,input_dimension)

X_train, X_test, y_train, y_test = train_test_split(train_data_reshaped, yscale, test_size=0.20, random_state=2)
print(X_train.shape,X_test.shape)
print(y_train.shape,y_test.shape)
#End Spliting Data

#Model Structure
model = Sequential(name="model_conv1D")

n_timesteps = train_data_reshaped.shape[1
n_features  = train_data_reshaped.shape[2]

model.add(Input(shape=(n_timesteps,n_features)))

model.add(Conv1D(filters=64, kernel_size=3, padding='same', activation='relu'))
model.add(Conv1D(filters=128, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(filters=256, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(500, activation='relu'))
model.add(Dense(6, activation='sigmoid'))

model.summary()
model.compile(loss='mse', optimizer=Adam(learning_rate=0.002), metrics=['mse'])

history = model.fit(X_train, y_train, batch_size=64, epochs=200,
                    validation_data=(X_test, y_test), verbose=2)
#End Model Structure

#Evaluate Model
model.evaluate(train_data_reshaped, yscale)
ypred = model.predict(train_data_reshaped)

plt.figure()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper right')
# plt.show()
plt.savefig('Loss Result.png')

print('MSE: ',mean_squared_error(yscale, ypred))
print('RMSE: ',math.sqrt(mean_squared_error(yscale, ypred)))
print('Coefficient of determination (r2 Score): ', r2_score(yscale, ypred))


#Inverse
y_inverse = []
y_pred_inverse = []


for i in range(0,6):
  Y_inver =  yscale[0:15000, i]*( FPmax[i] - FPmin[i] )+FPmin[i]
  Pred_inver = ypred[0:15000, i]*( FPmax[i] - FPmin[i] )+FPmin[i]
  y_inverse.append(Y_inver)
  y_pred_inverse.append(Pred_inver)
y_inverse = np.array(y_inverse)
y_inverse = y_inverse.transpose()
y_pred_inverse = np.array(y_pred_inverse)
y_pred_inverse = y_pred_inverse.transpose()


print('MSE: ',mean_squared_error(y_inverse, y_pred_inverse))
print('RMSE: ',math.sqrt(mean_squared_error(y_inverse, y_pred_inverse)))
print('Coefficient of determination (r2 Score): ', r2_score(y_inverse, y_pred_inverse))

x=[]
colors=['red','green','brown','teal','gray','black','maroon','orange','purple']
colors2=['green','red','orange','black','maroon','teal','blue','gray','brown']
x = np.arange(0,3000)*60/3000 
for i in range(0,6):
    plt.figure(figsize=(15,6))
    # plt.figure()
    plt.plot(x,y_inverse[0:3000,i],color='red')
    plt.plot(x,y_pred_inverse[0:3000,i], markerfacecolor='none',color='green')
    plt.title('CNN Regression (Training Data)')
    if i < 3:
      plt.ylabel('Force/'+columns[i])
    else:
      plt.ylabel('Moment/'+columns[i])
    plt.xlabel('Time(s)')
    plt.legend(['Real value', 'Predicted Value'], loc='best')
    plt.show()
#End Evaluate Model

模型损失:

mse 使用归一化数据:

MSE:  0.00033982666
RMSE:  0.018434387873554003
Coefficient of determination (r2 Score):  0.9934412267882915

反向数据后的mse:

MSE:  711726.3
RMSE:  843.6387334042931
Coefficient of determination (r2 Score):  0.9934412272949391

预测结果样本:

print("Real Value : ",y_inverse[51])
print("prediction Value : ",y_pred_inverse[51])
Real Value :  [    4.46733      4.39629    -34.235107 -4077.2305   -6206.8125
  -874.53906 ]
prediction Value :  [    6.6143274     5.6351166   -31.929413  -3412.164     -6177.2344
 -2047.6455   ]

如何让数据倒置时MSE值不变?

python tensorflow deep-learning conv-neural-network regression
© www.soinside.com 2019 - 2024. All rights reserved.