import numpy as np class LSTM: def __init__(self, input_dim, hidden_dim): self.input_dim = input_dim self.hidden_dim = hidden_dim self.Wf = np.random.randn(hidden_dim, input_dim + hidden_dim) * 0.01 self.Wi = np.random.randn(hidden_dim, input_dim + hidden_dim) * 0.01 self.Wc = np.random.randn(hidden_dim, input_dim + hidden_dim) * 0.01 self.Wo = np.random.randn(hidden_dim, input_dim + hidden_dim) * 0.01 self.bf = np.zeros((hidden_dim, 1)) self.bi = np.zeros((hidden_dim, 1)) self.bc = np.zeros((hidden_dim, 1)) self.bo = np.zeros((hidden_dim, 1)) self.h = np.zeros((hidden_dim, 1)) self.c = np.zeros((hidden_dim, 1)) def sigmoid(self, x): return 1 / (1 + np.exp(-x)) def tanh(self, x): return np.tanh(x) def forward(self, x_t): combined = np.vstack((self.h, x_t)) f_t = self.sigmoid(np.dot(self.Wf, combined) + self.bf) i_t = self.sigmoid(np.dot(self.Wi, combined) + self.bi) C_tilde_t = self.tanh(np.dot(self.Wc, combined) + self.bc) self.c = f_t * self.c + i_t * C_tilde_t o_t = self.sigmoid(np.dot(self.Wo, combined) + self.bo) self.h = o_t * self.tanh(self.c) return self.h def backward(self, x_t, h_t, y_t, learning_rate): # Your backward pass implementation here pass def train(self, X, y, num_epochs, learning_rate): for epoch in range(num_epochs): for i in range(len(X)): x_t = X[i] y_t = y[i] # Forward pass h_t = self.forward(x_t) # Calculate loss and perform backward pass loss = np.mean((h_t - y_t) ** 2) # Example loss self.backward(x_t, h_t, y_t, learning_rate) if i % 100 == 0: # Print loss every 100 samples print(f"Epoch {epoch}, Sample {i}, Loss: {loss}")