4
$\begingroup$

I trained a Multilayer Perceptron to predict a variable Y based on a set of predictors. Then I decided to test it on unseen data outside of the training range. I am aware of (some of) the implications of extrapolating machine learning models, and how ANN specifically can lead to crazy extrapolations. Nevertheless, my experiment requires this step, and I believe the shape the MLP produces when out of range is not necessarily the issue.

The issue, as seen in the partial dependence plot below, is that I would expect the extrapolation (red curve) to follow the down slope of the training curve (black curve). Instead, what we see is an almost identical curve, but translated to the right of the training curve. I would appreciate any insights on why this is happening, or any comments suggesting there is one or more flaws on my logic. Lastly, it would be interesting to hear thoughts on how to achieve this "extension" of the training curve on to the extrapolation curve.

Partial dependence plots

            def create_model():
                model = Sequential()
                model.add(Dense(200, input_dim=len(X_train.columns))) 
                model.add(Activation('relu'))
                model.add(Dropout(0.1))
    
                model.add(Dense(200))
                model.add(Activation('relu'))
                model.add(Dropout(0.1))
    
                model.add(Dense(200))
                model.add(Activation('relu'))
                model.add(Dropout(0.1))
                
                model.add(Dense(200))
                model.add(Activation('relu'))
                model.add(Dropout(0.1))
                
                model.add(Dense(1, activation='linear'))
                # compile the keras model
                model.compile(loss='mean_absolute_error', optimizer=tf.keras.optimizers.Adam(0.001), metrics=['mean_squared_error','mean_absolute_error'])
                return model
                    
            model_rf = Pipeline([
                ('scaler', StandardScaler()),
                ('estimator', KerasRegressor(model=create_model, epochs=200, batch_size= 1024, verbose=1))
$\endgroup$
12
  • 2
    $\begingroup$ Could you say a bit more about your data and problem? a) What kind of data is this? b) What are (roughly) the inputs and what is the output? c) By "extrapolation", do you mean "test set data"? Or are you literally trying to get a NN to be able to extend the line? If so, why not use some basic polynomial/spline approximation? Can you show the "correct" answer? $\endgroup$ Commented Jan 27, 2022 at 0:56
  • $\begingroup$ a) The data is in the format of a multiindex table, with the indices being time and spatial coords, and all variables are of continuous type. b) The inputs are different sets of temperature, and the output is crop development. c) By extrapolation, I refer to test set data that is 50% out of the training range (climate change). There is no correct answer, and the aim is the NN to map the interactions between temp and crop across time and space. However, there is some expert knowledge that supports the shapes seen on the black curves, and I wonder if the red ones shouldn't just "follow" them $\endgroup$
    – Henrique
    Commented Jan 27, 2022 at 9:03
  • 1
    $\begingroup$ c) So to be clear, you have some training data, then you are asking the question "I wonder what would happen if the inputs changed significantly?" , but you feel like you're unable to get something reasonable (as shown in the graph), is that correct? $\endgroup$ Commented Jan 27, 2022 at 16:11
  • 2
    $\begingroup$ Huh, strange. You're right in that there's no expectation of any sort for the completely extrapolated data. However, you would expect that the section of the testing data that overlaps the training data would look alike. Very strange. Are you sure there's no simple coding or plotting error going on? Additionally, when you re-run the model back on its training data, does it manage to give (near perfectly) the correct result? $\endgroup$ Commented Jan 27, 2022 at 17:49
  • 1
    $\begingroup$ The data consists in 7 features, each of 15000 rows. I added the model setup to the original post. The score is not great, 0.6 R2 test set, but when rerun on original data it's 0.9 R2. I believe there's no plotting error because when testing on a RF model, it overlaps beautifully (though RF cannot extrapolate). I ran an extra step training the ANN with one single feature, and then the extrapolation does work following the training curve. So it indicates the interaction between the variables has led to the shapes above. But once again, I cannot explain why that would be the case. $\endgroup$
    – Henrique
    Commented Jan 27, 2022 at 22:01

3 Answers 3

5
$\begingroup$

If a neural net is built with ReLu units, then its asymptotic behaviour is necessarily linear. No training can change this.

More generally, no machine learning with a finite training set can train asymptotic behaviour. So extrapolations always reflect a priori assumptions, not training.

$\endgroup$
2
  • $\begingroup$ Hi @chrishmorris, thanks for the reply. I'm not sure if I understood it well. If the model when predicting extrapolated data is reflecting the assumptions made during the training phase, shouldn't it be some sort of continuation of the training range, at least while within the calibration zone (the left tail of the red curve)? $\endgroup$
    – Henrique
    Commented Jan 26, 2022 at 22:42
  • 1
    $\begingroup$ Yes, but it can only be a linear continuation. A ReLu is non-linear only for ranges of data that cross its zero point, where the output switches from zero to linear. For sufficiently large values of the independent variables, all ReLus are bounded away from their zero point. Similar behaviour applies to any other neuron: the asymptotic behaviour is defined by the network architecture, not by training. $\endgroup$ Commented Jan 28, 2022 at 8:37
2
$\begingroup$

It seems you are using your fit outside of the expected range. You don't want to extrapolate. Ever. Period. End of story.

Anything extrapolated can not be trusted. The extrapolation results will depend on the coefficients you get when you fit and the functional form of the fit (your model). If your extrapolated data do not behave this way, you will get nonsense (or something unexplainable-- as you have seen)

Please see:

https://www.sciencedirect.com/science/article/pii/S2772415821000110

Section I shows how one can be tricked into thinking their training data cover their desired outcomes.

Section II gives an easy algorithm for people to use to quickly determine if their data point is extrapolated and how much of the possible outcome space is an extrapolation (based on training data).

Section III shows a great example of a great neural network fit, but what happens when you use it as a prediction and the inputs are out of range.

Section IV shows how much data you actually need to fit a deep neural network. I would focus on sections III and IV. Do not trust if out of range!

For those interested, the full citation:

Siegel, Adam. "A parallel algorithm for understanding design spaces and performing convex hull computations." Journal of Computational Mathematics and Data Science 2 (2022): 100021.

$\endgroup$
2
  • $\begingroup$ I would assume some famous researchers in the field (LeCun) would not agree to the first statement. Are you aware of Balestriero's Learning in High Dimension Always Amounts to Extrapolation paper? $\endgroup$
    – bluenote10
    Commented Jun 24, 2023 at 21:12
  • $\begingroup$ Yes, I am aware. The paper states that extrapolation occurs for high dimensional data sets, and that people do it. It offers an example of when extrapolation can work. You can not prove in general that something will work with a single example. You can however disprove with a single example, which is what Siegel's paper does. If you are unfamiliar with the topology of the problem (is it linear? quadratic? etc.) then do not extrapolate, since neural networks won't follow your functional form. If you can guarentee adherence, then sure, go for it. However, no reason to trust it in general. $\endgroup$ Commented Aug 11, 2023 at 22:18
1
$\begingroup$

After some weeks of testing, I think I finally figured out the solution for the issue above. And it is rather basic.

First, it's good to recall the two datasets used: the historical one, which was used for training and validation, and the future one, for tests purpose. Being the datasets timeseries, they were detrended. However, they were corrected by their mean values, as it is the standard approach in the field. Again, it is a basic thing that I did not realise before, but this leads to a significant bias between training data and tests data.

The solution was to adjust the detrending of the future dataset to have a mean value equal to the historical dataset. This way, with the mean values equal, the extrapolation became smooth and more similar to what I would have expected in the first place. Figure below illustrates the behaviour. enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.