I am trying to create a simple deep-learning based model to predict y=x**2
But looks like deep learning is not able to learn the general function outside the scope of its training set.
Intuitively I can think that neural network might not be able to fit y=x**2 as there is no multiplication involved between the inputs.
Please note I am not asking how to create a model to fit x**2
. I have already achieved that. I want to know the answers to following questions:
- Is my analysis correct?
- If the answer to 1 is yes, then isn't the prediction scope of deep learning very limited?
- Is there a better algorithm for predicting functions like y = x**2 both inside and outside the scope of training data?
Path to complete notebook: https://github.com/krishansubudhi/MyPracticeProjects/blob/master/KerasBasic-nonlinear.ipynb
training input:
x = np.random.random((10000,1))*1000-500
y = x**2
x_train= x
training code
def getSequentialModel():
model = Sequential()
model.add(layers.Dense(8, kernel_regularizer=regularizers.l2(0.001), activation='relu', input_shape = (1,)))
model.add(layers.Dense(1))
print(model.summary())
return model
def runmodel(model):
model.compile(optimizer=optimizers.rmsprop(lr=0.01),loss='mse')
from keras.callbacks import EarlyStopping
early_stopping_monitor = EarlyStopping(patience=5)
h = model.fit(x_train,y,validation_split=0.2,
epochs= 300,
batch_size=32,
verbose=False,
callbacks=[early_stopping_monitor])
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_18 (Dense) (None, 8) 16
_________________________________________________________________
dense_19 (Dense) (None, 1) 9
=================================================================
Total params: 25
Trainable params: 25
Non-trainable params: 0
_________________________________________________________________
Evaluation on random test set
Deep learning in this example is not good at predicting a simple non linear function. But good at predicting values in the sample space of training data.