CNN_sentence icon indicating copy to clipboard operation
CNN_sentence copied to clipboard

Error when I have more than two classes

Open haoyangz opened this issue 9 years ago • 4 comments

Hi,

I was trying to change the code to use on classification task where there are more than two classes of outputs (positive and negative). I changed preprocess.py and it worked fine. But when I tried to run the conv_net_sentence.py , it reported following error:

Traceback (most recent call last):
  File "conv_net_sentence.py", line 327, in <module>
    dropout_rate=[0.5],img_w=wordim)
  File "conv_net_sentence.py", line 170, in train_conv_net
    cost_epoch = train_model(minibatch_index)
  File "/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.py", line 606, in __call__
    storage_map=self.fn.storage_map)
  File "/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.py", line 595, in __call__
    outputs = self.fn()
ValueError: y_i >= dx dimensions[1]
Apply node that caused the error: CrossentropySoftmax1HotWithBiasDx(Alloc.0, SoftmaxWithBias.0, Elemwise{Cast{int32}}.0)

According to here, it seems that it is because the code defines a model with binary outputs classes while my data had more than two classes. I am new to Theano and I am wondering where should I change the code to adopt it to more than two outputs?

Thanks

haoyangz avatar Jul 23 '15 05:07 haoyangz

The same error happens to me. After setting hidden_unites=[100,2] to [100,x], x is the number of classes, it works fine.

youngia avatar Sep 07 '15 01:09 youngia

Hello, I have the same problem. I have 4 classes and I've changed the method "build_data_cv" to create the data with 4 classes. In particular: datum = {"y":class_number, "text": orig_rev, "num_words": len(orig_rev.split()), "split": np.random.randint(0,cv)} I've changed hidden_units as you suggested, too. However, it still threw that error. Could you please help?

lenhhoxung86 avatar Mar 25 '16 11:03 lenhhoxung86

I applied the code to a dataset with 5 classes, changing hidden_units to [100,5] worked. But there's another issue that puzzled me: The model behaves quite differently depending on the output labels. Having '2,4,5,6,9' as labels, the model performs really badly and the training performance sometimes alternates between two values or has the exact same value for all epochs. So, I mapped my classes to labels '0,1,2,3,4', and the performance is much better and the strange behavior disappeared. Why is that?

mn73 avatar Jul 08 '16 11:07 mn73

@mn73 The code internally maps classes starting from 0 only. It is essential for you to start with 0 as for this code, it is the right approach.

HimaVarsha94 avatar Aug 18 '16 10:08 HimaVarsha94