SKlearn的神经网络实现无法始终找到最优权重,且最佳着陆点为acc = 0.975,但并非总是如此(看似取决于具体情况)
在如此简单的设置中如何保证最佳精度? 为什么Keras版本甚至没有关闭?
我是神经网络的新手,并且对稀疏模型(即,参数最少,精度最高)感兴趣。带有lbfgs解算器的Sklearn有时似乎可以达到最佳权重
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_moons, make_circles, make_classification
from sklearn.neural_network import MLPClassifier
X, y =make_moons(n_samples=200, noise=0.1, random_state=0)
X = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.4, random_state=42)
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
#sklearn
nn=MLPClassifier(hidden_layer_sizes=(2,),
alpha=alpha,
max_iter=100000000,
solver='lbfgs',
activation='tanh',
verbose=False).fit(X_train, y_train)
print(nn.score(X_test, y_test))
# Keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
model = Sequential()
model.add(Dense(2, input_dim=2, activation='tanh'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])
model.fit(X_train, y_train,
epochs=900,
batch_size=200,
validation_data=(X_test, y_test),
verbose=0)
print(model.evaluate(X_test, y_test)[1])
我获得0.975精度的sklearn模型的权重是
print(nn.coefs_)
array([[-0.66491063, -5.99816399],
[ 0.13907145, -3.82369965]]),
array([[-16.53339218],
[ 9.14208312]])]
print(nn.intercepts_)
[array([0.04063317, 0.01169239]), array([0.40667715])]