2

我正在尝试使用神经网络来解决分类问题。我有 6 个可能的类,并且相同的输入可能在多个类中。

问题是,当我尝试为每个类训练一个 NN 时,我设置 output_num_units = 1 并且在训练时,我传递了 y 的第一列,y[:,0]。我得到以下输出和错误:

## Layer information

#  name      size
---  ------  ------
  0  input       32
  1  dense0      32
  2  output       1

IndexError: index 1 is out of bounds for axis 1 with size 1
Apply node that caused the error: CrossentropyCategorical1Hot(Elemwise{Composite{scalar_sigmoid((i0 + i1))}}[(0, 0)].0, y_batch)
Inputs types: [TensorType(float32, matrix), TensorType(int32, vector)]
Inputs shapes: [(128, 1), (128,)]
Inputs strides: [(4, 4), (4,)]
Inputs values: ['not shown', 'not shown']

如果我尝试使用output_num_units=num_class(6) 和完整的 y(所有六个字段),首先我会收到 KStratifiedFold 的错误,因为它似乎不希望 y 有多行。如果我设置eval_size=None,则会收到以下错误:

TypeError: ('Bad input argument to theano function with name "/usr/local/lib/python2.7/site-packages/nolearn-0.6a0.dev0-py2.7.egg/nolearn/lasagne/base.py:311"  
at index 1(0-based)', 'Wrong number of dimensions: expected 1, got 2 with shape (128, 6).')

唯一有效的配置是设置多个输出单元并且只将一列传递给 y。比它训练神经网络,但它似乎不正确,因为它给了我 2 个输出层,我只有一个 Y 可以比较。

我究竟做错了什么?为什么我不能只使用一个输出?我应该将我的 y 类从 6 列的向量转换为只有一列的向量吗?

我使用以下代码(摘录):

# load data
data,labels = prepare_data_train('../input/train/subj1_series1_data.csv')

# X_train (119496, 32) <type 'numpy.ndarray'>
X_train = data_preprocess_train(data)
#print X_train.shape, type(X_train)

# y (119496, 6) <type 'numpy.ndarray'>
y = labels.values.astype(np.int32)
print y.shape, type(y)

# net config
num_features = X_train.shape[1]
num_classes = labels.shape[1]

# train neural net
layers0 = [('input', InputLayer),
           ('dense0', DenseLayer),
           ('output', DenseLayer)]

net1 = NeuralNet(
    layers=layers0,

    # layer parameters:
    input_shape=(None, num_features),  # 32 input
    dense0_num_units = 32,  # number of units in hidden layer
    output_nonlinearity=sigmoid,  # sigmoid function as it has only one class
    output_num_units=2 ,  # if I try 1, it does not work

    # optimization method:
    update=nesterov_momentum,
    update_learning_rate=0.01,
    update_momentum=0.9,

    max_epochs=50,  # we want to train this many epochs
    verbose=1,
    eval_size=0.2
    )


net1.fit(X_train,  y[:,0])
4

1 回答 1

1

然后我想在 Lasagne 中使用 CNN,但没有让它以相同的方式工作,因为预测总是 0... 建议您查看MNIST 示例。我发现一个更好的使用和扩展,因为旧的代码片段由于 API 随时间的变化而不能完全工作。我已经修改了 MNIST 示例,我的目标向量具有标签 0 或 1,并以这种方式为 NN 创建输出层:

# Finally, we'll add the fully-connected output layer, of 2 softmax units:
l_out = lasagne.layers.DenseLayer(
        l_hid2_drop, num_units=2,
        nonlinearity=lasagne.nonlinearities.softmax)

对于 CNN:

    layer = lasagne.layers.DenseLayer(
        lasagne.layers.dropout(layer, p=.5),
        num_units=2,
        nonlinearity=lasagne.nonlinearities.softmax)
于 2015-07-21T07:38:53.153 回答