当前位置: 首页 > article >正文

手写单层RNN网络,后续更新

文章目录

  • 1. 原理
  • 2. pytorch 源码,只是测试版,后续持续优化

1. 原理

根据如下公式,简单的手写实现单层的RNN神经网络,加强代码功能和对网络的理解能力
在这里插入图片描述

2. pytorch 源码,只是测试版,后续持续优化

import torch
import torch.nn as nn
import torch.nn.functional as F

torch.set_printoptions(precision=3, sci_mode=False)
torch.manual_seed(23435)

if __name__ == "__main__":
    run_code = 0
    input_size = 4
    hidden_size = 3
    num_layers = 1
    batch_first = True
    single_rnn = nn.RNN(input_size=input_size, hidden_size=hidden_size, num_layers=num_layers, batch_first=batch_first)
    print(single_rnn)
    for name in single_rnn.named_parameters():
        print(name)

    single_rnn_weight_ih_l0 = single_rnn.weight_ih_l0
    single_rnn_weight_hh_l0 = single_rnn.weight_hh_l0
    single_rnn_bias_ih_l0 = single_rnn.bias_ih_l0
    single_rnn_bias_hh_l0 = single_rnn.bias_hh_l0

    # print(f"single_rnn_weight_ih_l0=\n{single_rnn_weight_ih_l0}")
    # input --> batch_size,seq_len,feature_map
    in_batch_size = 1
    in_seq_len = 2
    in_feature_map = input_size
    input_matrix = torch.randn(in_batch_size, in_seq_len, in_feature_map)
    output_matrix, output_hn = single_rnn(input_matrix)
    print(f"output_matrix=\n{output_matrix}")
    print(f"output_hn=\n{output_hn}")
    test_output0 = input_matrix @ single_rnn_weight_ih_l0.T + single_rnn_bias_ih_l0
    ht_1 = torch.zeros_like(test_output0)
    print(f"ht_1=\n{ht_1}")
    print(f"ht_1.shape=\n{ht_1.shape}")
    test_output1 = ht_1 @ single_rnn_weight_hh_l0.T + single_rnn_bias_hh_l0
    test_output = torch.tanh(test_output1 + test_output0)
    ht_1[:,1, :] = test_output[:,0, :]
    test_output1 = ht_1 @ single_rnn_weight_hh_l0.T + single_rnn_bias_hh_l0
    test_output = torch.tanh(test_output1 + test_output0)
    print(f"test_output=\n{test_output}")
    print(f"test_output.shape=\n{test_output.shape}")
  • 结果:经计算,通过pytorch官方的API输出的结果和自定义的结果一致!!!
RNN(4, 3, batch_first=True)
('weight_ih_l0', Parameter containing:
tensor([[ 0.413,  0.044,  0.243,  0.171],
        [-0.093,  0.250, -0.499, -0.450],
        [-0.571,  0.220,  0.464, -0.154]], requires_grad=True))
('weight_hh_l0', Parameter containing:
tensor([[-0.403,  0.165, -0.244],
        [ 0.216, -0.511, -0.441],
        [ 0.133,  0.278, -0.211]], requires_grad=True))
('bias_ih_l0', Parameter containing:
tensor([ 0.115, -0.493,  0.555], requires_grad=True))
('bias_hh_l0', Parameter containing:
tensor([-0.309, -0.504,  0.311], requires_grad=True))
output_matrix=
tensor([[[ 0.243, -0.467, -0.554],
         [-0.013, -0.802, -0.490]]], grad_fn=<TransposeBackward1>)
output_hn=
tensor([[[-0.013, -0.802, -0.490]]], grad_fn=<StackBackward0>)
ht_1=
tensor([[[0., 0., 0.],
         [0., 0., 0.]]])
ht_1.shape=
torch.Size([1, 2, 3])
test_output=
tensor([[[ 0.243, -0.467, -0.554],
         [-0.013, -0.802, -0.490]]], grad_fn=<TanhBackward0>)
test_output.shape=
torch.Size([1, 2, 3])

http://www.kler.cn/a/529945.html

相关文章:

  • Oracle数据库高效管理与优化实践
  • C语言中的线程本地变量
  • 构建具身智能体的时空宇宙!GRUtopia:畅想城市规模下通用机器人的生活图景
  • 微信登录模块封装
  • 使用LLaMA-Factory对AI进行认知的微调
  • matlab快速入门(2)-- 数据处理与可视化
  • K8S集群架构及主机准备
  • SQL索引优化_提高系统响应速度的秘诀
  • Deepseek R1 本地化部署指南:跨平台实战
  • react redux监测值的变化
  • 硕成C语言1笔记
  • Linux - 进程间通信(3)
  • IOC三种实现方式的区别
  • Brooks MX Marathon Expressrm User Manual MX集成系统平台
  • 建表注意事项(2):表约束,主键自增,序列[oracle]
  • Lesson 127 A famous actress
  • 一维数组0-1背包问题理论基础
  • w189电商平台的设计与实现
  • 尝试ai生成figma设计
  • 系统URL整合系列视频二(界面原型)
  • 独立开发经验谈:如何借助 AI 辅助产品 UI 设计
  • C++中的拷贝构造器(Copy Constructor)
  • 稀疏进化训练:机器学习优化算法中的高效解决方案
  • 蓝桥杯备考:枚举算法之铺地毯
  • R语言绘制有向无环图(DAG)
  • 普通用户(非root) 安装libreoffice