# How does neural network training work, if there are A HUGE number of points that not differentiable?

When I first saw ReLu function, I would not guess it will work in neural network because there is a point that is not differentiable. But it seems works very well on modern neural network.

My question is that, if we have a neural network with ReLu everywhere, then there are HUGE number of points that is not differentiable, then how to update the weights when we do not have the gradient at those points?

• A finite number of points out of a uncountably infinite number doesn’t seem huge to me – kbrose Aug 16 '18 at 12:46
• @kbrose the number of points which can be represented in floating point arithmetics is finite. It’s true that the non-differentiability of ReLU is not a practical concern, but the reason is a bit different. – DeltaIV Aug 16 '18 at 18:48
• If “It’s true that the non-differentiability of ReLU is not a practical concern, but the reason is a bit different” then I don’t think I understand what is being said. I thought the question was asking about the practicalities of updating the weights via gradient descent when there are points where the gradient is not well defined. – kbrose Aug 17 '18 at 1:07
• @kbrose I think you understood the OP’s question correctly (though his opinion would be more than welcome). The point is that the set of possible values for the weights of a NN in practice is finite, thus you can’t use the finiteness of non-differentiable points to imply trainability of ReLU-based NNs. – DeltaIV Aug 17 '18 at 11:32

The simple and perhaps unsatisfying answer is that we arbitrarily choose a gradient at 0.

Typically deep learning libraries will choose to have a gradient of 0. We can see this using the python libraries of Keras (with tensorflow backend) and numpy:

import keras
import keras.backend as K
import numpy as np

# Define simple model with only ReLU activation.
inp = keras.layers.Input([1])
x = keras.layers.Activation('relu')(inp)
m = keras.models.Model(inp, x)
m.compile(loss='mse', optimizer='sgd')

# get the gradients of the input with respect to the loss.

# create a function we can use to retrieve the gradient
input_tensors = m.inputs + m.sample_weights + m.targets + [K.learning_phase()]

# Let's see the gradient of ReLU when passing in [-1, 0, 1, 2]
# as the input, with a target value of always 1
data_input = np.array([[-1.0, 0.0, 1.0, 2.0]]).T
sample_weights = [1.0] * 4 # just scales the gradient
target = np.ones([4, 1]) # The target is 1.0
training_phase = 1 # doesn't effect anything in this example
inputs = [data_input, sample_weights, target, training_phase]

X = data_input.flatten()

x = -1.00, loss = +4.00, (∂ loss)/(∂ x) = -0.00000000