Skip to main content
 首页 » 编程设计

python之超过一类的神经网络不起作用

2025年05月04日12tintown

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

问题是,当我尝试为每个类(class)训练一个神经网络时,我设置了 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]) 

请您参考如下方法:

然后我想在烤宽面条中使用 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)