Search code examples
neural-networkloss-functiondata-fitting

Fitting a surface with keras


hello everyone!

I am a newbie in machine learning, and I decided to start off with fitting a 3D function z= -x^2 + y^2. First, I created a grid and evaluated the function at each point:

dataset=[(x,y,-x**2+y**2) for x  in range(-50,50) for y in range(-50,50)]
coord=dataset[:,0:2] 
z=dataset[:,2:]

The model architecture:

opt= tf.keras.optimizers.Adam(learning_rate=0.01)
model = tf.keras.models.Sequential([
  Dense(256, activation='relu',input_dim=2), 
  Dense(128, activation='relu'),
  Dense(64, activation='relu'),   
  Dense(10,activation='relu'), 
  Dense(1)
])
model.compile(loss='mae', optimizer=opt)
history=model.fit(coord, z, epochs=30,batch_size=15, verbose=1)

At this point I tried to tweak the architecture, tried different loss functions, optimizers and batch sizes. But my loss function, however, does not seem to improve. It follows a good trend, but it gets stuck at pretty high values.

The loss plot

What would you do to get closer to zero? Thank you!


Solution

  • You should definitely use more than 30 epochs. I recommend about 300-600 epochs. This could be what is causing your problem.