Code

Adam (Deep Learning Optimization Algorithm) Python Implementation

import numpy as np class Adam(): self.beta1 = 0.9 self.beta2 = 0.99 # Here is the M amd V matrices for the parameters and biases self.m_weights = np.zeros((input_dims, nodes)) self.v_weights = np.zeros((input_dims, nodes)) self.m_biases = np.zeros(nodes) self.v_biases = np.zeros(nodes) # need to track thecurrent time stamp self.curr_iter = 0 def get_steps(self, grad_weights, grad_biases, learning_rate): eps = 1e-8 # Just follows the formula and returns the update that have to be # subtracted from the parametes and biases self.m_weights = self.beta1 * self.m_weights + (1 - self.beta1) * grad_weights self.v_weights = self.beta2 * self.v_weights + (1 - self.beta2) * np.power(grad_weights, 2) self.m_biases = self.beta1 * self.m_biases + (1 - self.beta1) * grad_biases self.v_biases = self.beta2 * self.v_biases + (1 - self.beta2) * np.power(grad_biases, 2) self.curr_iter += 1 self.m_weights = self.m_weights / (1 - np.power(self.beta1, self.curr_iter)) self.v_weights = self.v_weights / (1 - np.power(self.beta2, self.curr_iter)) self.m_biases = self.m_biases / (1 - np.power(self.beta1, self.curr_iter)) self.v_biases = self.v_biases / (1 - np.power(self.beta2, self.curr_iter)) weights_step = np.multiply((learning_rate / (np.sqrt(self.v_weights) + eps)), self.m_weights) biases_step = np.multiply((learning_rate / (np.sqrt(self.v_biases) + eps)), self.m_biases) return weights_step, biases_step

0

2

Updated 2020-11-16

Tags

Data Science