【发布时间】:2018-03-05 06:39:33
【问题描述】:
我一直在学习神经网络课程,但我并不真正理解为什么我从逻辑回归的准确度得分和两层神经网络(输入层和输出层)中得到不同的结果。输出层使用 sigmoid 激活函数。根据我的学习,我们可以使用神经网络中的 sigmoid 激活函数来计算概率。如果与逻辑回归试图完成的不同,这应该非常相似。然后从那里反向传播以使用梯度下降最小化误差。可能有一个简单的解释,但我不明白为什么准确度分数差异如此之大。在这个例子中,我没有使用任何训练或测试集,只是简单的数据来展示我不理解的内容。
我的逻辑回归准确率为 71.4%。在下面的示例中,我刚刚为“X”和结果“y”数组创建了数字。当结果等于“1”时,我故意将“X”的数字设置得更高,以便线性分类器可以具有一定的准确性。
import numpy as np
from sklearn.linear_model import LogisticRegression
X = np.array([[200, 100], [320, 90], [150, 60], [170, 20], [169, 75], [190, 65], [212, 132]])
y = np.array([[1], [1], [0], [0], [0], [0], [1]])
clf = LogisticRegression()
clf.fit(X,y)
clf.score(X,y) ##This results in a 71.4% accuracy score for logistic regression
但是,当我实现一个没有隐藏层的神经网络时,只对单节点输出层使用 sigmoid 激活函数(因此总共两层,输入层和输出层)。我的准确率在 42.9% 左右?为什么这与逻辑回归准确度得分显着不同?为什么这么低?
import keras
from keras.models import Sequential
from keras.utils.np_utils import to_categorical
from keras.layers import Dense, Dropout, Activation
model = Sequential()
#Create a neural network with 2 input nodes for the input layer and one node for the output layer. Using the sigmoid activation function
model.add(Dense(units=1, activation='sigmoid', input_dim=2))
model.summary()
model.compile(loss="binary_crossentropy", optimizer="adam", metrics = ['accuracy'])
model.fit(X,y, epochs=12)
model.evaluate(X,y) #The accuracy score will now show 42.9% for the neural network
【问题讨论】:
标签: python machine-learning neural-network keras logistic-regression