Theano
HiddenLayer
Activation Function
Neural Networks
Machine Learning

Theano HiddenLayer Activation Function

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Theano's HiddenLayer is a core component in building neural networks using the Theano library. Theano, although no longer under active development, laid the foundation for many modern deep learning frameworks such as TensorFlow and PyTorch. It's crucial to understand the workings of its components, such as the HiddenLayer, for historical context and for understanding some foundational aspects of deep learning.

Understanding HiddenLayer in Theano

A HiddenLayer is essentially a fully connected layer that consists of neurons, each receiving input, applying a linear transformation followed by a non-linear activation function. The linear transformation is typically parameterized by weights and biases that are learned during the training of the network. The essential purpose of this layer is to introduce non-linearity into the model, which enhances its ability to make complex predictions.

Components of HiddenLayer

  1. Weights (WW) and Biases (bb):
    Each neuron in the hidden layer has associated weights and a bias that help in determining the neuron's output.
    • Weights (WW): These are parameters associated with every input feature that gets adjusted during training. They're initialized using different strategies such as zero initialization, random initialization, Xavier initialization, etc.
    • Biases (bb): Bias is an additional parameter used to shift the activation function. It helps the model fit the data better by adjusting the output independent of the input.
  2. Activation Function (gg):
    The non-linear activation function applied to the weighted sum of inputs and bias. Activations like sigmoid, tanh, ReLU are commonly used. The choice of activation function impacts the learning and predictive power of the neural network significantly.
  3. Linear Transformation:
    The output of the linear transformation for a neuron is typically given by: z=Wx+bz = Wx + b where zz is the scalar input to the activation function, xx is the input vector, and WW and bb are the weights and bias respectively.
  4. Output (aa) from the Activation Function:
    The activated output is computed as: a=g(z)a = g(z) where gg is the chosen activation function.

Common Activation Functions

Sigmoid Function

  • Formula:
    g(z)=11+ezg(z) = \frac{1}{1 + e^{-z}}
  • Properties:
    The sigmoid function maps inputs to a range between 0 and 1, making it useful for binary classification.
  • Drawbacks:
    Susceptible to issues like vanishing gradients for large positive or negative inputs which leads to slow convergence during training.

Hyperbolic Tangent (Tanh)

  • Formula:
    g(z)=tanh(z)=ezezez+ezg(z) = \tanh(z) = \frac{e^{z} - e^{-z}}{e^{z} + e^{-z}}
  • Properties:
    Outputs values between -1 and 1, centered around zero, which generally leads to faster convergence compared to sigmoid.

Rectified Linear Unit (ReLU)

  • Formula:
    g(z)=max(0,z)g(z) = \max(0, z)
  • Properties:
    ReLU introduces non-linearity while maintaining a simple nature, promoting sparsity of activations and computational efficiency.
  • Drawbacks:
    It can suffer from the "dying ReLU" problem, where neurons can become inactive during training if they output zero for any input.

Example: Implementing a HiddenLayer in Theano


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.