Search code examples
pythonmachine-learningtheanoneupy

NeuPy: Input shapes issues


I want to build a neural network using neupy. Therefore I consturcted the following architecture:

 network = layers.join(
                    layers.Input(10),

                    layers.Linear(500),
                    layers.Relu(),

                    layers.Linear(300),
                    layers.Relu(),

                    layers.Linear(10),
                    layers.Softmax(),
                )

My data is shaped as follwoing:

x_train.shape = (32589,10)
y_train.shape = (32589,1)

When I try to train this network using:

model.train(x_train, y_trian)

I get the follwoing error:

ValueError: Input dimension mis-match. (input[0].shape[1] = 10, input[1].shape[1] = 1)
Apply node that caused the error: Elemwise{sub,no_inplace}(SoftmaxWithBias.0, algo:network/var:network-output)
Toposort index: 26
Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)]
Inputs shapes: [(32589, 10), (32589, 1)]
Inputs strides: [(80, 8), (8, 8)]
Inputs values: ['not shown', 'not shown']
Outputs clients: [[Elemwise{Composite{((i0 * i1) / i2)}}(TensorConstant{(1, 1) of 2.0}, Elemwise{sub,no_inplace}.0, Elemwise{mul,no_inplace}.0), Elemwise{Sqr}[(0, 0)](Elemwise{sub,no_inplace}.0)]]

How do I have to edit my network to map this kind of data?

Thank you a lot!


Solution

  • Your architecture has 10 outputs instead of 1. I assume that your y_train function is a 0-1 class identifier. If so, than you need to change your structure to this:

    network = layers.join(
       layers.Input(10),
    
       layers.Linear(500),
       layers.Relu(),
    
       layers.Linear(300),
       layers.Relu(),
    
       layers.Linear(1),  # Single output
       layers.Sigmoid(),  # Sigmoid works better for 2-class classification
    )
    

    You can make it even simpler

    network = layers.join(
       layers.Input(10),
       layers.Relu(500),
       layers.Relu(300),
       layers.Sigmoid(1),
    )
    

    The reason why it works is because layers.Liner(10) > layers.Relu() is the same as layers.Relu(10). You can learn more in official documentation: http://neupy.com/docs/layers/basics.html#mutlilayer-perceptron-mlp