多个类的神经网络不工作

2024-06-30 13:38:37 发布

您现在位置:Python中文网/ 问答频道 /正文

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

问题是,当我尝试为每个类训练一个NN时,我设置output_num_units=1,在train上,我通过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(所有六个字段),首先我会得到一个kstatifiedfold的错误,因为它似乎并不期望y有多行。如果设置eval_size=None,则会出现以下错误:

^{pr2}$

唯一有效的配置是设置一个以上的输出单元,并且只向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])

Tags: inputoutputdatasizelabelstype错误not
1条回答
网友
1楼 · 发布于 2024-06-30 13:38:37

然后我想在千层面中使用CNNs,但是没有让它以同样的方式工作,因为预测值总是0。。。建议您查看the MNIST example。我发现这一点更适合使用和扩展,因为随着时间的推移,旧的代码片段不能完全工作。我修改了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:

^{pr2}$

相关问题 更多 >