Skip to content
Advertisement

How to increase the number of decimals when predicting an attribute using LSTM on Python?

I have an LSTM model on Python (Keras) in which predicts floats with long decimals. However, the predicted value has fewer decimals than expected. For instance:

  • Input value: 41.39011366661221
  • Predicted value: 41.396626

Should I use something on my model? I’ve also tried to normalize the input. Unfortunately, I get the same number of output decimals.

Any clue?

Advertisement

Answer

Your input have long decimals, but the problem is related to the way Keras captures your data by defining internally their floating format.

To set the Keras floating numbers internally you could :

1/ set the dtype definition (dtype stands for dataset type)

An example on a constant with Keras with different dtype definitions :

import keras.backend as K
import tensorflow as tf

K.constant(1.2588723669415442556, dtype=tf.float16).numpy()
# 1.234
K.constant(1.2588723669415442556, dtype=tf.float32).numpy()
# 1.2588724
K.constant(1.2588723669415442556, dtype=tf.float64).numpy()
# 1.2588723669415443

2/ set float using set_floatx() function :

### set_floatx arguments : 'float16', 'float32', or 'float64'
K.set_floatx('float32')
K.constant(1.2588723669415442556).numpy()
# 1.2588724
K.set_floatx('float64')
K.constant(1.2588723669415442556).numpy()
# 1.2588723669415443
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement