Python的二进制输出神经网络



作为个人项目的一部分,我试图用我自己的数据修改Theano文档(多层感知器)中给出的示例代码。

到目前为止,我设法以所需的格式带来我自己的(文本)数据,我想构建一个二进制分类器。问题是,当我写输出的数量为1时,即

classifier = MLP(rng=rng, input=x, n_in=49, n_hidden=n_hidden, n_out=1)

我得到以下错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:UsersAsteriosAnacondalibsite-packagesspyderlibwidgetsexternalshell  sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "C:/Users/Asterios/Documents/Python/TripAdvisor/untitled4.py", line 603, in <module>
params = test_mlp()
File "C:/Users/Asterios/Documents/Python/TripAdvisor/untitled4.py", line 553, in test_mlp
minibatch_avg_cost = train_model(minibatch_index)
File "C:UsersAsteriosAnacondalibsite-packagestheano-0.6.0-py2.7.eggtheanocompilefunction_module.py", line 588, in __call__
self.fn.thunks[self.fn.position_of_error])
File "C:UsersAsteriosAnacondalibsite-packagestheano-0.6.0-py2.7.eggtheanocompilefunction_module.py", line 579, in __call__
outputs = self.fn()
ValueError: y_i value out of bounds
Apply node that caused the error: CrossentropySoftmaxArgmax1HotWithBias(Dot22.0, b, Elemwise{Cast{int32}}.0)
Inputs shapes: [(10L, 1L), (1L,), (10L,)]
Inputs strides: [(8L, 8L), (8L,), (4L,)]
Inputs types: [TensorType(float64, matrix), TensorType(float64, vector), TensorType(int32, vector)]
Use the Theano flag 'exception_verbosity=high' for a debugprint of this apply node.

我的训练数据的输出(在转换为共享类型之前)是这样的:

array([1, 1, 1, ..., 0, 0, 0], dtype=int64)

奇怪的是,如果我使用的输出神经元的数量大于1(例如n_out=2),代码运行没有任何错误,但当然现在有许多输出神经元没有实际意义。

请解释一下为什么二进制输出的代码似乎给我一个错误?我怎样才能使它工作?

谢谢!

在MLP教程中用作输出层的逻辑回归类不是"标准"逻辑回归,它只给出一个输出值并区分两个类,而是多项式逻辑回归(又名Softmax回归),它为每个类提供一个输出值,告诉输入属于它们的概率。所以,如果你有10个类,你也需要10个单位,显然所有输出单位的总和等于1,因为这是一个概率分布。

尽管使用了类名("LogistRegression"),但它在链接源代码中的教义使其真实意图('''Multi-class Logistic Regression Class [...]''')毫无疑问。

然而在你的问题中,你有两个类,你也需要2个输出单元,你的n_out的值必须是2而不是1。当然,对于两个类,一个输出的值总是1减去另一个输出的值。

还有,检查你是否真的需要int64而不是int32。