pythonkerasneural-networkdeep-learningapproximation

Approximation of funtion with multi-dimensional output using a keras neural network


As part of a project for my studies I want to try and approximate a function f:R^m -> R^n using a Keras neural network (to which I am completely new). The network seems to be learning to some (indeed unsatisfactory) point. But the predictions of the network don't resemble the expected results in the slightest.

I have two numpy-arrays containing the training-data (the m-dimensional input for the function) and the training-labels (the n-dimensional expected output of the function). I use them for training my Keras model (see below), which seems to be learning on the provided data.

inputs = Input(shape=(m,))
hidden = Dense(100, activation='sigmoid')(inputs)
hidden = Dense(80, activation='sigmoid')(hidden)
outputs = Dense(n, activation='softmax')(hidden)

opti = tf.keras.optimizers.Adam(lr=0.001)

model = Model(inputs=inputs, outputs=outputs)
model.compile(optimizer=opti,
             loss='poisson',
             metrics=['accuracy'])

model.fit(training_data, training_labels, verbose = 2, batch_size=32, epochs=30)

When I call the evaluate-method on my model with a set of test-data and a set of test-labels, I get an apparent accuracy of more than 50%. However, when I use the predict method, the predictions of the network do not resemble the expected results in the slightest. For example, the first ten entries of the expected output are:

[0., 0.08193582, 0.13141066, 0.13495408, 0.16852582, 0.2154705 , 0.30517559, 0.32567417, 0.34073457, 0.37453226]

whereas the first ten entries of the predicted results are:

[3.09514281e-09, 2.20849714e-03, 3.84095078e-03, 4.99367528e-03,
6.06226595e-03, 7.18442770e-03, 8.96730460e-03, 1.03423093e-02, 1.16029680e-02, 1.31887039e-02]

Does this have something to do with the metrics I use? Could the results be normalized by Keras in some intransparent way? Have I just used the wrong kind of model for the problem I want to solve? What does 'accuracy' mean anyway? Thank you in advance for your help, I am new to neural networks and have been stuck with this issue for several days.


Solution

  • The problem is with this line:

    outputs = Dense(n, activation='softmax')(hidden)
    

    We use softmax activation only in a classification problem, where we need a probability distribution over the classes as an output of the network. And so softmax makes ensures that the output sums to one and non zero (which is true in your case). But I don't think the problem at hand for you is a classification task, you are just trying to predict ten continuous target varaibles, so use a linear activation function instead. So modify the above line to something like this

    outputs = Dense(n, activation='linear')(hidden)