[英]mlp classifier of scikit-neuralnetwork not working for xor
我正在嘗試創建一個可以使用 scikit-neuralnetwork 學習異或問題的神經網絡。 我的所有輸出都為 1
import sknn.mlp as mlp;
import numpy as np;
""" input layer """
ip_layer = mlp.Layer('Sigmoid', units=2);
hidden_layer = mlp.Layer('Tanh', units=3);
op_layer = mlp.Layer('Softmax', units=1);
nn = mlp.Classifier([ip_layer, hidden_layer, op_layer], n_iter=10000);
nn.fit(np.array([[0,0], [1,0], [0,1], [1,1]]), np.array([[0], [1], [1], [0]]));
print nn.predict(np.array([[0,0], [0,1], [1, 0], [1, 1]]))
它預測 [[1], [1], [1], [1]]。 還有另一個關於堆棧溢出本身的問題,它嘗試了類似的代碼,但我無法理解解決方案,並且它不允許我發表評論scikit-neuralnetwork mismatch error in dataset size
它給了我以下警告。 我不確定它是否相關。
/usr/local/lib/python2.7/dist-packages/theano/tensor/signal/downsample.py:6: UserWarning: downsample 模塊已移至 theano.tensor.signal.pool 模塊。 “下采樣模塊已移至 theano.tensor.signal.pool 模塊。”) [(4, 1)]
使用您的原始代碼,我得到AssertionError: Mismatch between dataset size and units in output layer.
我已經修改了你的代碼,輸出層的units=2
(這似乎是關鍵),並得到了[[0], [1], [1], [0]]
的正確預測輸出
import sknn.mlp as mlp;
import numpy as np;
ip_layer = mlp.Layer('Sigmoid', units=2)
hidden_layer = mlp.Layer('Tanh', units=3)
op_layer = mlp.Layer('Softmax', units=2) # <-- units=2, not 1
nn = mlp.Classifier(
[ip_layer, hidden_layer, op_layer],
n_iter=10000
)
x_train = np.array([[0,0],[1,0],[0,1],[1,1]])
y_train = np.array([[0],[1],[1],[0]])
nn.fit(x_train, y_train)
y_predict = nn.predict(x_train)
print 'y_predict is', y_predict
具有正確預測的輸出軌跡
x_train is [[0 0]
[1 0]
[0 1]
[1 1]]
y_predict is [[0]
[1]
[1]
[0]]
我的環境版本
Python 2.7.9
>>> np.__version__
'1.11.0'
>>> sknn.__version__
u'0.7'
>>> lasagne.__version__
'0.1'
>>> theano.__version__
'0.8.2'
Theano警告
至於警告UserWarning: downsample module has been moved to the theano.tensor.signal.pool module.
,這似乎是良性的,只是在庫接口更改,更新theano
版本0.8.0
應該修復它( sknn
用途lasagne
和theano
下)
鑒於您正在處理二元分類問題,輸出層應使用 Sigmoid 激活函數。
op_layer = mlp.Layer('Sigmoid', units=1);
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.