经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 大数据/云/AI » 人工智能基础 » 查看文章
深度学习--RNN基础
来源:cnblogs  作者:林每天都要努力  时间:2023/4/26 8:51:15  对本文有异议

深度学习--RNN基础

? RNN(Recurrent Neutral Network,循环神经网络),主要应用于自然语言处理NLP。

RNN表示方法

1.编码

因为Pytorch中没有String类型数据,需要引入序列表示法(sequence representation)对文本进行表示。

? 表示方法:[seq_len:一句话的单词数,feature_len:每个单词的表示方法]

文本信息的表达方式:

  1. one-hot:多少个单词就有多少位编码。缺点:非常稀疏(sparse),维度太高,缺乏语意相关性(semantic similarity)
  2. word2vec
  1. import torch
  2. import torch.nn as nn
  3. word_to_ix = {"hello":0,"world":1}
  4. embeds = nn.Embedding(2,5) #2行5列 一共有2个单词,用5位的feature来表示
  5. lookup_tensor = torch.tensor([word_to_ix["hello"]],dtype=torch.long)
  6. hello_embed = embeds(lookup_tensor)
  7. print(hello_embed)
  8. #tensor([[-0.2169, 0.3653, 0.7812, -0.8420, -0.2815]],
  9. # grad_fn=<EmbeddingBackward0>)
  10. word_to_ix = {"hello":0,"world":1}
  11. embeds = nn.Embedding(2,5) #2行5列 一共有2个单词,用5位的feature来表示
  12. lookup_tensor = torch.tensor([word_to_ix["hello"]],dtype=torch.long)
  13. hello_embed = embeds(lookup_tensor)
  14. print(hello_embed)
  15. #tensor([[-0.2169, 0.3653, 0.7812, -0.8420, -0.2815]],
  16. # grad_fn=<EmbeddingBackward0>)
  1. glove
  1. from torchnlp.word_to_vector import GloVe
  2. vectors = GloVe()
  3. vector["hello"]

2. batch

两种引入方式:[word num, b, word vec] 或者 [b, word num, word vec ] 第一种常用

RNN原理

naive version

对每一个单词进行 x@w1+b1 操作,每个单词都有不同的参数

Weight sharing

共享参数,用同一个w和b

Consistent memory 持续记忆

每一个单词运算表示:x @ wxh +h @ whh

增加了一个h单元,相当于一个memory单元。

总结:

RNN的网络为yt = why*ht

ht=激活函数(Whh* ht-1+Wxh *xt) 常用的为tanh

模型的反向传播:BPTT(back propagation through time)

RNN层的使用方法

  1. run = nn.RNN(100,10) #word vec 单词的表示位数, memory 记忆节点
  2. run._parameters.keys()
  3. #odict_keys(['weight_ih_l0', 'weight_hh_l0', 'bias_ih_l0', 'bias_hh_l0'])
  4. run.weight_hh_l0.shape, run.weight_ih_l0.shape
  5. #(torch.Size([10, 10]), torch.Size([10, 100]))
  6. run.bias_hh_l0.shape, run.bias_ih_l0.shape
  7. #(torch.Size([10]), torch.Size([10]))

1.nn.RNN

nn.RNN(input_size:单词的表示方法维度,hidden_size:记忆的维度:,num_layers:默认是1)

前向传播,一步到位 out, ht = forward(x, h0)

? x:[一句话单词数,batch几句话,表示的维度]

? h0/ht:[层数,batch,记忆(参数)的维度]

? out:[一句话单词数,batch,参数的维度]

  1. import torch
  2. import torch.nn as nn
  3. run = nn.RNN(input_size=100, hidden_size=20, num_layers=1)
  4. print(run)
  5. #RNN(100, 20)
  6. x = torch.randn(10,3,100)
  7. h = torch.zeros(1,3,20)
  8. out,h1 = run(x,h)
  9. print(out.shape,h1.shape)
  10. #torch.Size([10, 3, 20]) torch.Size([1, 3, 20])

2. nn.RNNCell:只完成一个计算

nn.RNNCell(input_size:单词的表示方法维度,hidden_size:记忆的维度:,num_layers:默认是1)

前向传播:ht=rnncell(xt,ht_1)

? xt:[batch,word维度]

? ht_1/ht:[层数,batch,参数的维度]

  1. #RNNCell
  2. x = torch.randn(10,3,100)
  3. cell = nn.RNNCell(100,20)
  4. h1 = torch.zeros(3,20)
  5. #人为控制一句话的单词数
  6. for xt in x:
  7. print(xt)
  8. h1 = cell(xt,h1)
  9. print(h1.shape)
  10. #torch.Size([3, 20])

原文链接:https://www.cnblogs.com/ssl-study/p/17353488.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号