我正在尝试创建一个可以使用 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 module has been moved to the theano.tensor.signal.pool module. "downsample module has been moved to the theano.tensor.signal.pool module.") [(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 theano.tensor.signal.pool module.,这似乎是良性的,只是库中的接口(interface)更改,更新 theano 版本到 0.8.0 应该修复它(sknn 在下面使用 lasagne 和 theano )
