liveBook · Manning

Tanh

As explained in Optimization Algorithms: AI techniques for design, planning, and control problems

The hyperbolic tangent is an activation function that maps real-valued inputs to the interval from -1 to 1.

The hyperbolic tangent, or tanh, is a squashing activation function that maps real-valued neuron inputs to values between -1 and 1. It transforms the weighted input and bias before the next layer processes it. section 9.7

Using tanh in a hidden layer

A neural-network forward pass computes z1 = X.dot(W1) + b1, then applies a1 = np.tanh(z1). The resulting hidden-layer activations feed a later logits calculation for classification. section 9.7

def logits_function(p):
  
    W1 = p[0: n_inputs * n_hidden].reshape((n_inputs, n_hidden)) 
    b1 = p[n_inputs * n_hidden: (n_inputs + 1) * n_hidden].reshape((n_hidden,))  #1
    W2 = p[(n_inputs +1) * n_hidden: -n_classes].reshape((n_hidden, n_classes))  #2
    b2 = p[-n_classes:].reshape((n_classes,))                                    #3
  
    z1 = X.dot(W1) + b1                                                          #4
    a1 = np.tanh(z1)                                                             #4
    logits = a1.dot(W2) + b2                                                     #5
    return logits                                                                #5

Inspect the complete logits function, including weight reshaping, bias addition, tanh activation, and output-logit computation. section 9.7

How Optimization Algorithms: AI techniques for design, planning, and control problems covers it

How deep it goes
Defines tanh and demonstrates its NumPy placement in forward propagation, but does not derive its mathematical formula or training gradients.
Examples
Demonstrates tanh on the hidden layer of a NumPy classifier using the Penguins dataset.
What you can do after
Apply it via NumPy when implementing a neural-network hidden layer and inspect its effect within forward propagation.
How it connects
Connects tanh with activation functions, neurons, weights, biases, logits, softmax probabilities, cross-entropy loss, and particle swarm optimization.

Also explained in

Compare how 4 books explain tanh →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Optimization Algorithms: AI techniques for design, planning, and control problems. The section links point to the parts of the book it draws on.