Search code examples
pythonkerasneural-networksequential

Neural Network generating negative values


I have a data set X that is composed of mean and standard deviation, this is repeated 5 times, so 10 columns in the array

And Y is composed of ranges:

Example:

  • 0 to 20
  • 20 to 40
  • 40 to 60
  • 60 to 80
  • 80 to 100

To transform the values ​​into 0 and 1, each element was divided by the highest occurrence of its column, this applies to X and Y

Objective: To make X and Y generate the next 60 values

X = blocks of 60 values

Y = The next 30 values ​​of each block

Problem: For some reason I get negative values, it looks like my neural network is failing

X:

[[0.573 0.699 0.412 0.224 0.696 0.512 0.326 0.314 0.79  0.685]
 [0.456 0.251 0.629 0.523 0.344 0.286 0.8   0.699 0.721 1.   ]
 ...
 [0.229 0.148 0.683 0.624 0.222 0.146 0.687 0.732 0.296 0.184]
 [0.646 0.627 0.204 0.152 0.542 0.632 0.36  0.224 0.291 0.215]]

Y:

[[1.    0.5   0.    0.    0.   ]
 [1.    0.5   0.    0.    0.   ]
 ...
 [1.    0.5   0.    0.    0.   ]
 [1.    0.5   0.    0.    0.   ]]

Script:

model = keras.Sequential(
    [
        layers.Dense(10, activation="sigmoid", name="hidden-input"),
        layers.Dense(5, name="output"),
    ]
)

model.compile(optimizer = 'Adam', loss = 'mse', metrics = ['mae'])

model.fit(X, Y, epochs = 20, batch_size = 10)
print(model.summary())

y = model.predict(X)

Summary:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
hidden-input (Dense)         multiple                  110       
_________________________________________________________________
output (Dense)               multiple                  55        
=================================================================
Total params: 165
Trainable params: 165
Non-trainable params: 0

Train:

Epoch 1/20
48/48 [==============================] - 0s 2ms/sample - loss: 0.3500 - mean_absolute_error: 0.4904
...
Epoch 20/20
48/48 [==============================] - 0s 178us/sample - loss: 0.0283 - mean_absolute_error: 0.1172

Output:

[[ 8.6036199e-01  4.6452054e-01  1.3958054e-02 -2.3673278e-01 3.2733783e-02]
 [ 9.7470945e-01  4.6182287e-01  6.4254209e-02 -2.0704785e-01 -2.0927802e-02]
  ...
 [ 7.7844203e-01  4.5801651e-01 -2.5306268e-02 -2.8805625e-01 4.5798883e-02]]

Solution

  • You are getting negative output because you didn't specify an activation on the last layer this means by default it's set to None which is linear activation function so the probability of getting negative output is not zero.

    In the first layer you chose sigmoid as an activation function so whatever the input is it will produce a positive values, then the negative numbers came from the weights connecting the first layer to the second layer, they should have negative weights, and with the suitable input it can produce negative numbers.