Neural network with 'tanh' as activation and 'cross-entropy' as cost function did not work
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Neural networks have become indispensable tools in a range of machine learning and artificial intelligence applications. They owe their versatility largely to the flexibility of their architecture and the choice of various activation functions and cost functions. However, selecting unsuitable combinations can lead to poor model performance. Thus, understanding how each component affects the network is crucial.
Here, we explore why using the tanh
activation function with the cross-entropy
cost function can be suboptimal, providing a detailed technical breakdown of the associated issues.
Activation Functions: The Role of tanh
Overview of tanh
The tanh
activation function is expressed as:
The function maps input values into the range of (-1, 1). The symmetric nature of tanh
around the origin is often considered beneficial because it centers the data around zero, which can help with convergence in some scenarios by mitigating the effects of a biased gradient.
Problems with tanh
in Practice
- Vanishing Gradients: Like the sigmoid function,
tanhis susceptible to the vanishing gradient problem due to its asymptotic nature across large input magnitudes. The gradients tend to zero as inputs move to extreme positive or negative values, slowing down learning significantly. - Non-Zero Derivatives: Even though
tanhhas steeper gradients than its sigmoid counterpart around the central region, this is mitigated by its saturation at the edges of its range, making small gradients less effective for deep networks.
Cost Functions: Understanding Cross-Entropy
Overview of Cross-Entropy
Cross-entropy is widely used as a cost function in classification problems:
For binary classification:
Where: • is the actual class label. • is the predicted probability output by the network.
Issues with Using Cross-Entropy
and tanh
The cross-entropy cost function is commonly used in combination with the softmax
activation due to their complementary mathematical properties. When paired with tanh
, certain inefficiencies arise:
- Output Range Mismatch: While
softmaxoutputs predictions as probabilities between 0 and 1,tanhoutputs values from -1 to 1. Usingtanhrequires additional processing to convert the output into probabilities. - Gradient Jumping: Cross-entropy's steep penalty on incorrect predictions requires a smooth gradient that
tanhdoes not consistently provide, particularly for large or small valued inputs. - Instability: Neural networks using
tanhare more prone to divergent behavior when experiencing unseen or extreme inputs, primarily because of their symmetric mapping.
Key Factors and Recommendations
- **Alternatives to
tanh**: • ReLU (Rectified Linear Unit): Helps eliminate the vanishing gradient by offering a constant gradient for positive inputs. • Leaky ReLU: Improves upon ReLU by allowing a non-zero gradient for negative inputs. - Use
softmaxfor cross-entropy:softmaxnaturally provides an appropriate probabilistic output which pairs advantageously with cross-entropy's expectations of input. - Layer Normalization: Using techniques such as Batch Normalization to stabilize inputs during training.
- Weight Initialization: Implement strategies like He initialization to prevent issues stemming from symmetric activation functions.
| Key Component | Problems | Recommendations |
tanh | ||
| Vanishing gradients, output range (-1, 1) | Use ReLU or variants for stable gradients | |
Cross-Entropy | ||
Expected input not aligning with tanh | ||
Pair with softmax for natural workflow | ||
| Combined Issues | High instability in convergence, gradient damping | Ensure proper flow with aligned activations |
Conclusion
In sum, while tanh
and cross-entropy individually serve important roles in neural networks, they are best employed in situations where their characteristics align naturally with the operational requirements. Understanding and pairing compatible activation and cost functions can greatly enhance the effectiveness of neural network models, leading to optimal learning and performance outcomes.

