"""
"""
import numpy as np
def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum(axis=0)
def sigmoid(x):
return 1 / (1 + np.exp(-x))
class LSTM:
def __init__(self, epochs=20, n_a=16, alpha=0.01, batch_size=32):
"""
:param epochs: 迭代次数
:param n_a: 隐藏层节点数
:param alpha: 梯度下降参数
:param batch_size: 每个batch大小
"""
self.epochs = epochs
self.n_a = n_a
self.alpha = alpha
self.parameters = {}
self.loss = 0.0
self.n_x = 2
self.n_y = 2
self.m = batch_size
def initialize_parameters(self, n_a, n_x, n_y):
"""
:param n_a: 每个cell输出a的维度 RNN单元的单元数
:param n_x: 每个cell输入xi的维度
:param n_y: 每个cell输出yi的维度
"""
np.random.seed(1)
Wf = np.random.randn(n_a, n_a + n_x) * 0.01 # randn函数返回一个或一组样本,具有标准正态分布。(50,50+27)
bf = np.zeros((n_a, 1)) # (50,1)
Wi = np.random.randn(n_a, n_a + n_x) * 0.01 # (50,50+27)=(50,77)【h(t-1),x(t)】=(50+27,1)=(77,1)
bi = np.zeros((n_a, 1)) # (50,1)
Wc = np.random.randn(n_a, n_a + n_x) * 0.01 # (50,50+27) 【h(t-1),x(t)】=(50+27,1)=(77,1)
bc = np.zeros((n_a, 1)) # (50,1)
Wo = np.random.randn(n_a, n_a + n_x) * 0.01 # (50,50+27) 【h(t-1),x(t)】=(50+27,1)=(77,1)
bo = np.zeros((n_a, 1)) # (50,1)
# Wo*【h(t-1),x(t)】+bo=(50,1)
Wy = np.random.randn(n_y, n_a) * 0.01 # (27,50) h=(50,1)
by = np.zeros((n_y, 1)) # (27,1)
# Wy*h+by=(27,1)
self.parameters = {
"Wf": Wf,
"bf": bf,
"Wi": Wi,
"bi": bi,
"Wc": Wc,
"bc": bc,
"Wo": Wo,
"bo": bo,
"Wy": Wy,
"by": by,
}
self.n_x = n_x # 27
self.n_y = n_y # 27
def lstm_cell_forward(self, xt, a_prev, c_prev):
"""
Implement a single forward step of the LSTM-cell as described in Figure (4)
如图(4)所示,执行LSTM单元的单个前进步骤
Arguments:
xt -- your input data at timestep "t", numpy array of shape (n_x, m). 时间步 “t”处输入的数据,形状的numpy数组(n_x,m)
a_prev -- Hidden state at timestep "t-1", numpy array of shape (n_a, m) 时间步“t-1”处的隐藏状态,形状的numpy数组(n_a,m)
c_prev -- Memory state at timestep "t-1", numpy array of shape (n_a, m) 时间步“t-1”处的记忆状态,形状的numpy数组(n_a,m)
parameters -- python dictionary containing: python字典包含:
Wf -- Weight matrix of the forget gate, numpy array of shape (n_a, n_a + n_x)遗忘门的权重矩阵,形状的numpy数组
bf -- Bias of the forget gate, numpy array of shape (n_a, 1) 遗忘门的偏差,形状的numpy数组
Wi -- Weight matrix of the update gate, numpy array of shape (n_a, n_a + n_x)更新门的权重矩阵,形状的numpy数组
bi -- Bias of the update gate, numpy array of shape (n_a, 1) 更新门的偏差,形状的numpy数组
Wc -- Weight matrix of the first "tanh", numpy array of shape (n_a, n_a + n_x)
第一个“tanh”的权重矩阵,形状的numpy数组
bc -- Bias of the first "tanh", numpy array of shape (n_a, 1)
第一个“tanh”的偏置,形状的numpy数组
Wo -- Weight matrix of the output gate, numpy array of shape (n_a, n_a + n_x)输出门的权重矩阵,形状的numpy数组
bo -- Bias of the output gate, numpy array of shape (n_a, 1) 输出门的偏置,形状的numpy数组
Wy -- Weight matrix relating the hidden-state to the output, numpy array of shape (n_y, n_a)
将隐藏状态与输出关联的权重矩阵,形状的numpy数组
by -- Bias relating the hidden-state to the output, numpy array of shape (n_y, 1)
将隐藏状态与输出关联的偏差,numpy形状数组
Returns:
a_next -- next hidden state, of shape (n_a, m)下一个隐藏状态
c_next -- next memory state, of shape (n_a, m)下一个记忆状态
yt_pred -- prediction at timestep "t", numpy array of shape (n_y, m)时间步“t”的预测
cache -- tuple of values needed for the backward pass, contains (a_next, c_next, a_prev, c_prev, xt, parameters)向后传递所需的值的元组
Note: ft/it/ot stand for the forget/update/output gates, cct stands for the candidate value (c tilde),cct 代表候选值
c stands for the memory value c代表记忆值
"""
# Retrieve parameters from "parameters" 从“参数”中检索参数
Wf = self.parameters["Wf"]
bf = self.parameters["bf"]
Wi = self.parameters["Wi"]
bi = self.parameters["bi"]
Wc = self.parameters["Wc"]
bc = self.parameters["bc"]
Wo = self.parameters["Wo"]
bo = self.parameters["bo"]
Wy = self.parameters["Wy"]
by = self.parameters["by"]
# Retrieve dimensions from shapes of xt and Wy 从xt和Wy形状检索尺寸
n_x, m = xt.shape # 时间步 “t”处输入的数据 27 1
n_y, n_a = Wy.shape # 27 50
# Concatenate a_prev and xt (≈3 lines) 连接a_prev和xt
concat = np.zeros((n_a + n_x, m)) # (77,1)
concat[: n_a, :] = a_prev
concat[n_a:, :] = xt
# Compute values for ft, it, cct, c_next, ot, a_next using the formulas given figure (4) (≈6 lines)
# 使用图(4)中给出的公式计算ft、it、cct、c_next、ot、a_next 的值
ft = sigmoid(np.dot(Wf, concat) + bf)
it = sigmoid(np.dot(Wi, concat) + bi)
cct = np.tanh(np.dot(Wc, concat) + bc)
c_next = np.multiply(ft, c_prev) + np.multiply(it, cct) # ct
ot = sigmoid(np.dot(Wo, concat) + bo)
a_next = np.multiply(ot, np.tanh(c_next)) # ht
# Compute prediction of the LSTM cell (≈1 line)LSTM单元的计算预测
yt_pred = softmax(np.dot(Wy, a_next) + by)
# store values needed for backward propagation in cache 在缓存中存储反向传播所需的值
cache = (a_next, c_next, a_prev, c_prev, ft, it, cct, ot, xt)
return a_next, c_next, yt_pred, cache
def lstm_forward(self, x, a0):
"""
Implement the forward propagation of the recurrent neural network using an LSTM-cell described in Figure (3).
使用图(3)所示的LSTM单元实现递归神经网络的正向传播
Arguments:
x -- Input data for every time-step, of shape (n_x, m, T_x). 输入每个时间步的数据(27,1,12) T_x:单词的长度+1
a0 -- Initial hidden state, of shape (n_a, m) 初始隐藏状态(50,1)
parameters -- python dictionary containing: python字典包含:
Wf -- Weight matrix of the forget gate, numpy array of shape (n_a, n_a + n_x)
bf -- Bias of the forget gate, numpy array of shape (n_a, 1)
Wi -- Weight matrix of the update gate, numpy array of shape (n_a, n_a + n_x)
bi -- Bias of the update gate, numpy array of shape (n_a, 1)
Wc -- Weight matrix of the first "tanh", numpy array of shape
numpy复现算法lstm算法内含数据集以及教程
版权申诉
54 浏览量
2022-10-16
22:04:43
上传
评论
收藏 931KB ZIP 举报
小码蚁.
- 粉丝: 2534
- 资源: 4146
最新资源
- 基于ARM的指令调度和延迟分支
- 基于Vue和TypeScript的极简聊天应用设计源码 - HasChat
- 基于Vue2全家桶和Zcool数据的图片收集网站设计源码 - cool-picture
- 基于C和C++的二维绘制工具设计源码 - DrawPro
- Object.defineProperty 的 IE 补丁object-defineproperty-ie-master.zip
- 整卷预览.mhtml
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见的
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见的
- 基于Javascript的结婚请帖设计源码 - Invitation
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0