liveBook · Manning

Nonlinear mapping

As explained in Inside Deep Learning: Math, Algorithms, Models

A transformation inserted between linear steps to let a neural network represent functions that are not purely linear.

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:

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

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Defines tanh and sigmoid ranges and demonstrates inserting tanh in a PyTorch network, while deferring further function properties.
Examples
Uses activation plots and a trained model with two linear layers and an intervening tanh.
What you can do after
Apply nonlinear mappings in PyTorch by placing nn.Tanh between nn.Linear layers and training the resulting model.
How it connects
Connects activation functions with linear layers, tanh, sigmoid, and neural networks.

Also explained in

Compare how 7 books explain nonlinear mapping →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.