为什么这段代码中的损失不等于训练数据中的均方误差?它应该相等,因为我设置了 alpha =0 ,因此没有正则化。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import mean_squared_error
#
i = 1 #difficult index
X_train = np.arange(-2,2,0.1/i).reshape(-1,1)
y_train = 1+ np.sin(i*np.pi*X_train/4)
fig = plt.figure(figsize=(8,8))
ax = fig.add_axes([0,0,1,1])
ax.plot(X_train,y_train,'b*-')
ax.set_xlabel('X_train')
ax.set_ylabel('y_train')
ax.set_title('Function')
nn = MLPRegressor(
hidden_layer_sizes=(1,), activation='tanh', solver='sgd', alpha=0.000, batch_size='auto',
learning_rate='constant', learning_rate_init=0.01, power_t=0.5, max_iter=1000, shuffle=True,
random_state=0, tol=0.0001, verbose=True, warm_start=False, momentum=0.0, nesterovs_momentum=False,
early_stopping=False, validation_fraction=0.1, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
nn = nn.fit(X_train, y_train)
predict_train=nn.predict(X_train)
print('MSE training : {:.3f}'.format(mean_squared_error(y_train, predict_train)))
当我运行这段代码时,我发现 loss = 0.02061828 并且训练中的 MSE(MSE 训练) = 0.041