gru网络python代码
时间: 2023-10-12 17:14:57 浏览: 161
GRU神经网络 Python代码
4星 · 用户满意度95%
以下是一个简单的 GRU 网络的 Python 代码示例,用于序列到序列的预测任务:
```python
import numpy as np
# 定义激活函数
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# 定义 GRU 网络类
class GRUNetwork:
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.Wr = np.random.randn(hidden_size, input_size)
self.Wz = np.random.randn(hidden_size, input_size)
self.W = np.random.randn(hidden_size, input_size)
self.Ur = np.random.randn(hidden_size, hidden_size)
self.Uz = np.random.randn(hidden_size, hidden_size)
self.U = np.random.randn(hidden_size, hidden_size)
self.V = np.random.randn(output_size, hidden_size)
# 前向传播
def forward(self, x):
T = len(x)
h = np.zeros((T+1, self.hidden_size))
r = np.zeros((T, self.hidden_size))
z = np.zeros((T, self.hidden_size))
h[0] = np.zeros(self.hidden_size)
for t in range(T):
r[t] = sigmoid(np.dot(self.Wr, x[t]) + np.dot(self.Ur, h[t]))
z[t] = sigmoid(np.dot(self.Wz, x[t]) + np.dot(self.Uz, h[t]))
h[t+1] = (1 - z[t]) * h[t] + z[t] * np.tanh(np.dot(self.W, x[t]) + np.dot(self.U, r[t] * h[t]))
y = np.dot(self.V, h[1:].T)
return y
# 训练模型
def train(self, x_train, y_train, learning_rate=0.1, epochs=100):
for epoch in range(epochs):
for x, y_true in zip(x_train, y_train):
# 前向传播
y_pred = self.forward(x)
# 反向传播
dV = np.outer(y_pred - y_true, np.sum(h[1:], axis=0))
dh = np.zeros(self.hidden_size)
dW = np.zeros((self.hidden_size, self.input_size))
dU = np.zeros((self.hidden_size, self.hidden_size))
dWr = np.zeros((self.hidden_size, self.input_size))
dUr = np.zeros((self.hidden_size, self.hidden_size))
dWz = np.zeros((self.hidden_size, self.input_size))
dUz = np.zeros((self.hidden_size, self.hidden_size))
for t in reversed(range(len(x))):
dy = y_pred - y_true
dh = np.dot(self.V.T, dy) + dh
dz = dh * (np.tanh(np.dot(self.W, x[t]) + np.dot(self.U, r[t] * h[t])) - h[t])
dU = dz * r[t] * h[t] + dU
dW = dz * x[t] + dW
dr = dz * h[t] * (1 - r[t]) * r[t]
dUr = dr * h[t-1] + dUr
dWr = dr * x[t] + dWr
dh = dz * r[t] + np.dot(self.U.T, dr)
dz = dh * (1 - z[t]) * z[t]
dUz = dz * h[t-1] + dUz
dWz = dz * x[t] + dWz
self.V = self.V - learning_rate * dV
self.W = self.W - learning_rate * dW
self.U = self.U - learning_rate * dU
self.Wr = self.Wr - learning_rate * dWr
self.Ur = self.Ur - learning_rate * dUr
self.Wz = self.Wz - learning_rate * dWz
self.Uz = self.Uz - learning_rate * dUz
```
以上代码是一个基本的 GRU 网络实现。在训练模型时,我们可以使用反向传播来计算权重的梯度,并使用梯度下降来更新权重。
阅读全文