python实现LSTM
时间: 2023-11-07 11:55:03 浏览: 79
以下是使用Python实现LSTM的基本代码:
```python
import numpy as np
# 定义LSTM类
class LSTM:
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.Wf = np.random.randn(hidden_size + input_size, hidden_size)
self.Wi = np.random.randn(hidden_size + input_size, hidden_size)
self.Wc = np.random.randn(hidden_size + input_size, hidden_size)
self.Wo = np.random.randn(hidden_size + input_size, hidden_size)
self.Wy = np.random.randn(hidden_size, output_size)
self.bf = np.zeros((1, hidden_size))
self.bi = np.zeros((1, hidden_size))
self.bc = np.zeros((1, hidden_size))
self.bo = np.zeros((1, hidden_size))
self.by = np.zeros((1, output_size))
# 定义sigmoid函数和tanh函数
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def tanh(self, x):
return np.tanh(x)
# 前向传播
def forward(self, X):
T = len(X)
self.h = np.zeros((T + 1, self.hidden_size))
self.f = np.zeros((T, self.hidden_size))
self.i = np.zeros((T, self.hidden_size))
self.c_bar = np.zeros((T, self.hidden_size))
self.c = np.zeros((T + 1, self.hidden_size))
self.o = np.zeros((T, self.hidden_size))
self.y = np.zeros((T, self.output_size))
for t in range(T):
# 将输入和上一时刻的隐藏状态连接起来
concat = np.hstack((self.h[t], X[t]))
# 计算遗忘门
self.f[t] = self.sigmoid(np.dot(concat, self.Wf) + self.bf)
# 计算输入门
self.i[t] = self.sigmoid(np.dot(concat, self.Wi) + self.bi)
# 计算候选状态
self.c_bar[t] = self.tanh(np.dot(concat, self.Wc) + self.bc)
# 计算当前状态
self.c[t + 1] = self.f[t] * self.c[t] + self.i[t] * self.c_bar[t]
# 计算输出门
self.o[t] = self.sigmoid(np.dot(concat, self.Wo) + self.bo)
# 计算输出
self.h[t + 1] = self.o[t] * self.tanh(self.c[t + 1])
self.y[t] = np.dot(self.h[t + 1], self.Wy) + self.by
return self.y
# 测试代码
input_size = 10
hidden_size = 20
output_size = 5
T = 50
X = [np.random.randn(input_size) for t in range(T)]
lstm = LSTM(input_size, hidden_size, output_size)
y = lstm.forward(X)
print(y.shape)
```
上述代码实现了一个基本的LSTM类,并在测试代码中使用随机数据进行了测试。
阅读全文