Consider this simple network that computes the following function:
The net looks like:
net = NetChain[{ElementwiseLayer[Ramp], AggregationLayer[Times, 1]}]
This works correctly as:
In[2]:= net[{4, 2}]
Out[2]= 8.
In[3]:= net[{4, -2}]
Out[3]= 0.
In[4]:= net[{-4, -2}]
Out[4]= 0.
However if try to evaluate the gradient in a given point, this returns a floating point error when either x or y are smaller than 0.
In[5]:= net[{4, -2}, NetPortGradient["Input"]]
During evaluation of In[5]:= NetGraph::netnan: A floating-point overflow, underflow, or division by zero occurred while evaluating the net.
Out[5]= $Failed
Why this happens? Is there a way to avoid this?
The error occurs also if we replace Ramp with
# - # &
but not with
# - # + 1&
PS: this is a very simplified example of the minimal network required to reproduce the error, extracted from a much more complicated loss function
Link to question on Stack Exchange [link]