2

Hey i train a neural network with keras. I have 14 categories. Everything works and there is no error. But when I look at the result I see that the last 3 trained categories are completely wrong. No testobject is connected to the 3 categories.

I already changed the order of the input but also the last 3 categories are not learned by the model.

I also used 2 different activation-functions in the last layer (sigmoid and softmax) and also 2 different optimizers (adam and sgd)

Is there a maximum number of categories?

Here is my code:

model = keras.models.Sequential()


model = Sequential()
model.add(Dense(units=50, input_dim = trainingsdaten.shape[1], 
                kernel_initializer='glorot_uniform', 
                bias_initializer='zeros',
                activation='tanh'))

model.add(Dense(units=50, input_dim = 50, 
                kernel_initializer='glorot_uniform', 
                bias_initializer='zeros',
                activation='tanh'))

model.add(Dense(output_dim = kategorien_train_one_hot.shape[1], input_dim=56, 
                kernel_initializer='glorot_uniform', 
                bias_initializer='zeros',
                activation='sigmoid'))

sgd_optimizer = keras.optimizers.SGD(lr=lr, decay = decay, momentum = momentum)

model.compile(optimizer = 'adam', 
              loss = 'categorical_crossentropy',
              metrics= ['accuracy'])

history = model.fit(trainingsdaten, kategorien_train_one_hot,
                    batch_size = batch_size, epochs=epochs,
                    verbose = verbose,
                    validation_split = validation_split)
Blümchen
  • 21
  • 1
  • 2
    Can you provide a statistical analysis of your training data? How many samples per class do you have? Generally, there is no limit to the classes (the [ImageNet](http://www.image-net.org/) dataset for example comes with over 1000 categories), and the order of classes does not matter. – dennlinger Apr 12 '19 at 07:51
  • Also, you don't have to provide the input and output shape for each layer. They are inferred by the previous one. – Eypros Apr 12 '19 at 07:59

2 Answers2

0

Maybe there is a small mistake that your third Dense layer has an input_dim = 56, but your second Dense layer has 50 nodes.

hhz
  • 143
  • 7
0

There is no maximum theoretically, but your results will be limited by the performance of your training machine and the amount of relevant data per class you can collect. If there are underrepresented classes through an unbalanced number of instances per class, your model will perform unsatisfactory.

In practise, it's almost always the available data per class that will limit your number of classes.

Nas
  • 17
  • 6