多个类的神经网络不起作用

时间:2015-07-13 19:11:26

标签: python theano nolearn lasagne

我正在尝试使用神经网络来解决分类问题。我有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。比它训练NN,但似乎不正确,因为它给了我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])

1 个答案:

答案 0 :(得分:1)

然后我想在烤宽面条中使用CNN,但没有以相同的方式使用它,因为预测总是0 ...建议你看看the MNIST example。我发现使用和扩展更好一些,因为旧的代码片段由于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)