我尝试在此WildML - Implementing a Neural Network From Scratch教程中重现模型,但使用的是Keras。我尝试使用与教程相同的所有配置,但即使在调整隐藏层中的纪元数,批量大小,激活函数和单元数后,我仍然会进行线性分类:
这是我的代码:
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.utils.visualize_util import plot
from keras.utils.np_utils import to_categorical
import numpy as np
import matplotlib.pyplot as plt
import sklearn
from sklearn import datasets, linear_model
# Build model
model = Sequential()
model.add(Dense(input_dim=2, output_dim=3, activation="tanh", init="normal"))
model.add(Dense(output_dim=2, activation="softmax"))
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
# Train
np.random.seed(0)
X, y = sklearn.datasets.make_moons(200, noise=0.20)
y_binary = to_categorical(y)
model.fit(X, y_binary, nb_epoch=100)
# Helper function to plot a decision boundary.
# If you don't fully understand this function don't worry, it just generates the contour plot below.
def plot_decision_boundary(pred_func):
# Set min and max values and give it some padding
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
h = 0.01
# Generate a grid of points with distance h between them
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
# Predict the function value for the whole gid
Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# Plot the contour and training examples
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)
# Predict and plot
plot_decision_boundary(lambda x: model.predict_classes(x, batch_size=200))
plt.title("Decision Boundary for hidden layer size 3")
plt.show()
答案 0 :(得分:1)
我相信我弄明白了这个问题。如果我删除np.random.seed(0)
并训练2000个纪元,则输出并不总是线性的,偶尔会达到90%以上的准确率:
一定是np.random.seed(0)
导致参数播种效果不佳,而且由于我正在修理随机播种,我每次都会看到相同的图形。
答案 1 :(得分:0)
我想我已经解决了这个问题,但是我不知道为什么要解决它。如果将输出层的激活功能更改为'sigmoid'而不是'softmax',则系统将正常工作。
model = Sequential()
model.add(Dense(50, input_dim=2, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics= . ['accuracy'])
由此,我可以达到95%或更高的精度。如果将上面的代码留在 softmax 中,则线性分类器仍然存在。
。