循环神经网络python实例
时间: 2023-09-13 21:07:47 浏览: 40
以下是一个基于Python的循环神经网络的简单示例:
```python
import numpy as np
# 定义循环神经网络
class RNN:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
# 初始化权重和偏置
self.Wxh = np.random.randn(hidden_size, input_size) * 0.01
self.Whh = np.random.randn(hidden_size, hidden_size) * 0.01
self.Why = np.random.randn(output_size, hidden_size) * 0.01
self.bh = np.zeros((hidden_size, 1))
self.by = np.zeros((output_size, 1))
# 前向传播
def forward(self, x):
h = np.zeros((self.hidden_size, 1))
self.hidden = {}
self.hidden[-1] = h
for t in range(len(x)):
h = np.tanh(np.dot(self.Wxh, x[t]) + np.dot(self.Whh, h) + self.bh)
self.hidden[t] = h
y = np.dot(self.Why, h) + self.by
return y, self.hidden
# 反向传播
def backward(self, x, y_true, y_pred, hidden):
dWxh = np.zeros_like(self.Wxh)
dWhh = np.zeros_like(self.Whh)
dWhy = np.zeros_like(self.Why)
dbh = np.zeros_like(self.bh)
dby = np.zeros_like(self.by)
dh_next = np.zeros_like(hidden[0])
for t in reversed(range(len(x))):
dy = np.copy(y_pred[t])
dy[y_true[t]] -= 1
dWhy += np.dot(dy, hidden[t].T)
dby += dy
dh = np.dot(self.Why.T, dy) + dh_next
dh_raw = (1 - hidden[t] ** 2) * dh
dbh += dh_raw
dWxh += np.dot(dh_raw, x[t].T)
dWhh += np.dot(dh_raw, hidden[t-1].T)
dh_next = np.dot(self.Whh.T, dh_raw)
for dparam in [dWxh, dWhh, dWhy, dbh, dby]:
np.clip(dparam, -5, 5, out=dparam)
return dWxh, dWhh, dWhy, dbh, dby
# 训练模型
def train(self, x, y_true, learning_rate):
y_pred, hidden = self.forward(x)
dWxh, dWhh, dWhy, dbh, dby = self.backward(x, y_true, y_pred, hidden)
self.Wxh -= learning_rate * dWxh
self.Whh -= learning_rate * dWhh
self.Why -= learning_rate * dWhy
self.bh -= learning_rate * dbh
self.by -= learning_rate * dby
# 设置超参数
input_size = 5
hidden_size = 4
output_size = 3
learning_rate = 0.1
epochs = 1000
# 创建模型
rnn = RNN(input_size, hidden_size, output_size)
# 训练模型
for i in range(epochs):
x = np.random.randn(input_size, 1)
y_true = np.random.randint(output_size)
y_pred, hidden = rnn.forward(x)
loss = -np.log(y_pred[y_true])
dWxh, dWhh, dWhy, dbh, dby = rnn.backward(x, y_true, y_pred, hidden)
rnn.Wxh -= learning_rate * dWxh
rnn.Whh -= learning_rate * dWhh
rnn.Why -= learning_rate * dWhy
rnn.bh -= learning_rate * dbh
rnn.by -= learning_rate * dby
if i % 100 == 99:
print('Epoch:', i+1, ' Loss:', loss)
```
这是一个非常简单的循环神经网络实例,其中包括前向传播、反向传播和训练模型。在这个示例中,我们使用随机数作为输入和目标输出,并训练模型来预测正确的输出。