A nonlinear mapping between linear operations lets a network build more complex functions than stacked linear operations alone. Nonlinear functions used this way are called activation functions. section 2.2.3
Two activation functions and their output ranges:
- Tanh maps inputs to [−1, 1]. section 2.2.3
- Sigmoid maps inputs to [0, 1]. section 2.2.3
Add tanh between linear layers
In a PyTorch sequential model with two nn.Linear layers, place nn.Tanh between them. This gives one activation between the two linear operations and leaves a linear layer at the output. section 2.2.3
activation_input = np.linspace(-2, 2, num=200)
tanh_activation = np.tanh(activation_input)
sigmoid_activation = np.exp(activation_input)/(np.exp(activation_input)+1)
sns.lineplot(x=activation_input, y=activation_input,
color=’black’, label="linear")
sns.lineplot(x=activation_input, y=tanh_activation,
color=’red’, label="tanh(x)")
ax = sns.lineplot(x=activation_input, y=sigmoid_activation,
color=’blue’, label="$\sigma(x)$)")
ax.set_xlabel(’Input value x’)
ax.set_ylabel(’Activation’)
[13]: Text(0, 0.5, 'Activation')
Compare the plotted tanh and sigmoid curves with a linear function. section 2.2.3






