We will take a very simple tabular example to get started
Take a non-linear function
$$ Z = 2X^2 - 3Y^2 +1 + \epsilon$$
# DL & Numerical Library
import numpy as np
import keras
# Visualisation
import matplotlib.pyplot as plt
%matplotlib inline
#!wget http://bit.do/vispy -O vis.py
x = np.arange(-1,1,0.01)
y = np.arange(-1,1,0.01)
x.shape, y.shape
x.min(), x.max()
X, Y = np.meshgrid(x, y)
c = np.ones((200,200))
e = np.random.rand(200,200)*0.1
X.shape
Z = 2*X*X - 3*Y*Y + c + e
import vis
vis.plot3d(X,Y,Z)
X.shape
X.reshape(-1).shape
input_xy = np.c_[X.reshape(-1), Y.reshape(-1)]
output_z = Z.reshape(-1)
input_xy.shape, output_z.shape
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
#model.add(Dense(units=10, input_dim=2, activation="linear"))
#model.add(Dense(units=10, input_dim=5, activation="linear"))
model.add(Dense(units=1, input_dim=2))
model.summary()
for layer in model.layers:
print(layer.input_shape, layer.output_shape)
model.compile(loss="mean_squared_error", optimizer="sgd", metrics=["mse"])
%%time
output = model.fit(input_xy, output_z, epochs=10, shuffle=True,
validation_split=0.2, verbose=0)
vis.metrics(output.history)
Z_pred = model.predict(input_xy).reshape(200,200)
vis.plot3d(X,Y,Z_pred)