Implementing a Simple RNN

Medium
Machine Learning

Write a Python function that implements a simple Recurrent Neural Network (RNN) cell. The function should process a sequence of input vectors and produce the final hidden state. Use the tanh activation function for the hidden state updates. The function should take as inputs the sequence of input vectors, the initial hidden state, the weight matrices for input-to-hidden and hidden-to-hidden connections, and the bias vector. The function should return the final hidden state after processing the entire sequence, rounded to four decimal places.

Examples

Example 1:
Input: input_sequence = [[1.0], [2.0], [3.0]] initial_hidden_state = [0.0] Wx = [[0.5]] # Input to hidden weights Wh = [[0.8]] # Hidden to hidden weights b = [0.0] # Bias
Output: final_hidden_state = [0.9759]
Explanation: The RNN processes each input in the sequence, updating the hidden state at each step using the tanh activation function.

Starter Code

import numpy as np

def rnn_forward(input_sequence: list[list[float]], initial_hidden_state: list[float], Wx: list[list[float]], Wh: list[list[float]], b: list[float]) -> list[float]:
	# Your code here
	return final_hidden_state
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews