1import numpy as np 2# 定义sigmoid函数 3def sigmoid(x): 4 return 1 / (1 + np.exp(-x)) 5class RNN: 6 def __init__(self, input_size, hidden_size, output_size): 7 # 设定超参数 8 self.input_size = input_size 9 self.hidden_size = hidden_size 10 self.output_size = output_size 11 12 # 初始化权重和偏置 13 self.Wxh = np.random.randn(hidden_size, input_size) * 0.01 # 输入到隐藏层的权重 14 self.Whh = np.random.randn(hidden_size, hidden_size) * 0.01 # 隐藏层到隐藏层的权重 15 self.Why = np.random.randn(output_size, hidden_size) * 0.01 # 隐藏层到输出层的权重 16 self.bh = np.zeros((hidden_size, 1)) # 隐藏层的偏置 17 self.by = np.zeros((output_size, 1)) # 输出层的偏置 18 19 def forward(self, inputs): 20 # 初始化隐藏状态和输出 21 self.h = np.zeros((self.hidden_size, 1)) 22 self.outputs = [] 23 for x in inputs: 24 # 更新隐藏状态 25 self.h = np.tanh(np.dot(self.Wxh, x) + np.dot(self.Whh, self.h) + self.bh) 26 # 计算输出 27 y = np.dot(self.Why, self.h) + self.by 28 # 应用sigmoid激活函数 29 output = sigmoid(y) 30 self.outputs.append(output) 31 return self.outputs 32 33 def backward(self, inputs, targets, learning_rate=0.1): 34 # 初始化梯度 35 dWxh = np.zeros_like(self.Wxh) 36 dWhh = np.zeros_like(self.Whh) 37 dWhy = np.zeros_like(self.Why) 38 dbh = np.zeros_like(self.bh) 39 dby = np.zeros_like(self.by) 40 dh_next = np.zeros_like(self.h) 41 42 for i in reversed(range(len(inputs))): 43 # 计算输出误差 44 dy = self.outputs[i] - targets[i] 45 # 计算输出层的梯度 46 dWhy += np.dot(dy, self.h.T) 47 dby += dy 48 # 计算隐藏层的误差 49 dh = np.dot(self.Why.T, dy) + dh_next 50 # 应用tanh的导数 51 dh_raw = (1 - self.h ** 2) * dh 52 # 计算隐藏层的梯度 53 dWxh += np.dot(dh_raw, inputs[i].T) 54 dWhh += np.dot(dh_raw, self.h.T) 55 dbh += dh_raw 56 # 更新dh_next 57 dh_next = np.dot(self.Whh.T, dh_raw) 58 59 for dparam in [dWxh, dWhh, dWhy, dbh, dby]: 60 np.clip(dparam, -5, 5, out=dparam) # 防止梯度爆炸 61 62 # 更新权重和偏置 63 self.Wxh -= learning_rate * dWxh 64 self.Whh -= learning_rate * dWhh 65 self.Why -= learning_rate * dWhy 66 self.bh -= learning_rate * dbh 67 self.by -= learning_rate * dby 68 69 70# 测试代码 71# 定义数据和标签 72inputs = [np.array([[1], [0], [1]]), np.array([[0], [1], [0]])] 73targets = [np.array([[1]]), np.array([[0]])] 74 75input_size = inputs[0].shape[0] 76hidden_size = 25 77output_size = targets[0].shape[0] 78 79# 创建RNN模型,并进行训练 80rnn = RNN(input_size, hidden_size, output_size) 81for epoch in range(1000): 82 outputs = rnn.forward(inputs) 83 loss = np.mean((np.array(outputs)-np.array(targets)) ** 2) 84 rnn.backward(inputs, targets) 85 if (epoch + 1) % 100 == 0: 86 print("次数:", epoch + 1, "误差:", loss) 87 88# 在新数据上进行预测 89new_input = np.array([[1], [1], [1]]) 90output = rnn.forward([new_input]) 91print("输入:", new_input.flatten()) 92print("输出:", output)
简单的GRU实例代码

风花雪月
2023-07-05
817 0 0
点赞
收藏
评论区
加载中...