当前位置:   article > 正文

勾八头歌之RNN

勾八头歌之RNN

一、RNN快速入门

1.学习单步的RNN:RNNCell

  1. # -*- coding: utf-8 -*-
  2. import tensorflow as tf
  3. # 参数 a 是 BasicRNNCell所含的神经元数, 参数 b 是 batch_size, 参数 c 是单个 input 的维数,shape = [ b , c ]
  4. def creatRNNCell(a,b,c):
  5. # 请在此添加代码 完成本关任务
  6. # ********** Begin *********#
  7. x1=tf.placeholder(tf.float32,[b,c])
  8. cell=tf.nn.rnn_cell.BasicRNNCell(num_units=a)
  9. h0=cell.zero_state(batch_size=b,dtype=tf.float32)
  10. output,h1=cell.__call__(x1,h0)
  11. print(cell.state_size)
  12. print(h1)
  13. # ********** End **********#

2.探幽入微LSTM

  1. # -*- coding: utf-8 -*-
  2. import tensorflow as tf
  3. # 参数 a 是 BasicLSTMCell所含的神经元数, 参数 b 是 batch_size, 参数 c 是单个 input 的维数,shape = [ b , c ]
  4. def creatLSTMCell(a,b,c):
  5. # 请在此添加代码 完成本关任务
  6. # ********** Begin *********#
  7. x1=tf.placeholder(tf.float32,[b,c])
  8. cell=tf.nn.rnn_cell.BasicLSTMCell(num_units=a)
  9. h0=cell.zero_state(batch_size=b,dtype=tf.float32)
  10. output,h1=cell.__call__(x1,h0)
  11. print(h1.h)
  12. print(h1.c)
  13. # ********** End **********#

3.进阶RNN:学习一次执行多步以及堆叠RNN

  1. # -*- coding: utf-8 -*-
  2. import tensorflow as tf
  3. import numpy as np
  4. # 参数 a 是RNN的层数, 参数 b 是每个BasicRNNCell包含的神经元数即state_size
  5. # 参数 c 是输入序列的批量大小即batch_size,参数 d 是时间序列的步长即time_steps,参数 e 是单个输入input的维数即input_size
  6. def MultiRNNCell_dynamic_call(a,b,c,d,e):
  7. # 用tf.nn.rnn_cell MultiRNNCell创建a层RNN,并调用tf.nn.dynamic_rnn
  8. # 请在此添加代码 完成本关任务
  9. # ********** Begin *********#
  10. cell = tf.nn.rnn_cell.MultiRNNCell([tf.nn.rnn_cell.BasicRNNCell(num_units=b) for _ in range(a)]) # a层RNN
  11. inputs = tf.placeholder(np.float32, shape=(c, d, e)) # a 是 batch_size,d 是time_steps, e 是input_size
  12. h0=cell.zero_state(batch_size=c,dtype=tf.float32)
  13. output, h1 = tf.nn.dynamic_rnn(cell, inputs, initial_state=h0)
  14. print(output)
  15. # ********** End **********#

二、RNN循环神经网络

1.Attention注意力机制(A  ABC  B  C  A)

2.Seq2Seq

  1. import numpy as np
  2. import torch
  3. import torch.nn as nn
  4. import torch.optim as optim
  5. from torch.autograd import Variable
  6. dtype = torch.FloatTensor
  7. char_list = [c for c in 'SEPabcdefghijklmnopqrstuvwxyz']
  8. char_dic = {n: i for i, n in enumerate(char_list)}
  9. seq_data = [['man', 'women'], ['black', 'white'], ['king', 'queen'], ['girl', 'boy'], ['up', 'down'], ['high', 'low']]
  10. seq_len = 8
  11. n_hidden = 128
  12. n_class = len(char_list)
  13. batch_size = len(seq_data)
  14. ##########Begin##########
  15. #对数据进行编码部分
  16. ##########End##########
  17. def make_batch(seq_data):
  18. batch_size = len(seq_data)
  19. input_batch, output_batch, target_batch = [], [], []
  20. for seq in seq_data:
  21. for i in range(2):
  22. seq[i] += 'P' * (seq_len - len(seq[i]))
  23. input = [char_dic[n] for n in seq[0]]
  24. output = [char_dic[n] for n in ('S' + seq[1])]
  25. target = [char_dic[n] for n in (seq[1] + 'E')]
  26. input_batch.append(np.eye(n_class)[input])
  27. output_batch.append(np.eye(n_class)[output])
  28. target_batch.append(target)
  29. return Variable(torch.Tensor(input_batch)), Variable(torch.Tensor(output_batch)), Variable(torch.LongTensor(target_batch))
  30. ##########Begin##########
  31. #模型类定义
  32. input_batch, output_batch, target_batch = make_batch(seq_data)
  33. class Seq2Seq(nn.Module):
  34. def __init__(self):
  35. super(Seq2Seq, self).__init__()
  36. self.encoder = nn.RNN(input_size=n_class, hidden_size=n_hidden)
  37. self.decoder = nn.RNN(input_size=n_class, hidden_size=n_hidden)
  38. self.fc = nn.Linear(n_hidden, n_class)
  39. def forward(self, enc_input, enc_hidden, dec_input):
  40. enc_input = enc_input.transpose(0, 1)
  41. dec_input = dec_input.transpose(0, 1)
  42. _, h_states = self.encoder(enc_input, enc_hidden)
  43. outputs, _ = self.decoder(dec_input, h_states)
  44. outputs = self.fc(outputs)
  45. return outputs
  46. ##########End##########
  47. model = Seq2Seq()
  48. criterion = nn.CrossEntropyLoss()
  49. optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
  50. ##########Begin##########
  51. #模型训练过程
  52. for epoch in range(5001):
  53. hidden = Variable(torch.zeros(1, batch_size, n_hidden))
  54. optimizer.zero_grad()
  55. outputs = model(input_batch, hidden, output_batch)
  56. outputs = outputs.transpose(0, 1)
  57. loss = 0
  58. for i in range(batch_size):
  59. loss += criterion(outputs[i], target_batch[i])
  60. loss.backward()
  61. optimizer.step()
  62. ##########End##########
  63. ##########Begin##########
  64. #模型验证过程函数
  65. def translated(word):
  66. input_batch, output_batch, _ = make_batch([[word, 'P' * len(word)]])
  67. hidden = Variable(torch.zeros(1, 1, n_hidden))
  68. outputs = model(input_batch, hidden, output_batch)
  69. predict = outputs.data.max(2, keepdim=True)[1]
  70. decode = [char_list[i] for i in predict]
  71. end = decode.index('P')
  72. translated = ''.join(decode[:end])
  73. print(translated)
  74. ##########End##########
  75. translated('highh')
  76. translated('kingh')

三、RNN和LSTM

1.循环神经网络简介

  1. import torch
  2. def rnn(input,state,params):
  3. """
  4. 循环神经网络的前向传播
  5. :param input: 输入,形状为 [ batch_size,num_inputs ]
  6. :param state: 上一时刻循环神经网络的状态,形状为 [ batch_size,num_hiddens ]
  7. :param params: 循环神经网络的所使用的权重以及偏置
  8. :return: 输出结果和此时刻网络的状态
  9. """
  10. W_xh,W_hh,b_h,W_hq,b_q = params
  11. """
  12. W_xh : 输入层到隐藏层的权重
  13. W_hh : 上一时刻状态隐藏层到当前时刻的权重
  14. b_h : 隐藏层偏置
  15. W_hq : 隐藏层到输出层的权重
  16. b_q : 输出层偏置
  17. """
  18. H = state
  19. # 输入层到隐藏层
  20. H = torch.matmul(input, W_xh) + torch.matmul(H, W_hh) + b_h
  21. H = torch.tanh(H)
  22. # 隐藏层到输出层
  23. Y = torch.matmul(H, W_hq) + b_q
  24. return Y,H
  25. def init_rnn_state(num_inputs,num_hiddens):
  26. """
  27. 循环神经网络的初始状态的初始化
  28. :param num_inputs: 输入层中神经元的个数
  29. :param num_hiddens: 隐藏层中神经元的个数
  30. :return: 循环神经网络初始状态
  31. """
  32. init_state = torch.zeros((num_inputs,num_hiddens),dtype=torch.float32)
  33. return init_state

2.长短时记忆网络

  1. import torch
  2. def lstm(X,state,params):
  3. """
  4. LSTM
  5. :param X: 输入
  6. :param state: 上一时刻的单元状态和输出
  7. :param params: LSTM 中所有的权值矩阵以及偏置
  8. :return: 当前时刻的单元状态和输出
  9. """
  10. W_xi, W_hi, b_i, W_xf, W_hf, b_f, W_xo, W_ho, b_o, W_xc, W_hc, b_c, W_hq, b_q = params
  11. """
  12. W_xi,W_hi,b_i : 输入门中计算i的权值矩阵和偏置
  13. W_xf,W_hf,b_f : 遗忘门的权值矩阵和偏置
  14. W_xo,W_ho,b_o : 输出门的权值矩阵和偏置
  15. W_xc,W_hc,b_c : 输入门中计算c_tilde的权值矩阵和偏置
  16. W_hq,b_q : 输出层的权值矩阵和偏置
  17. """
  18. #上一时刻的输出 H 和 单元状态 C。
  19. (H,C) = state
  20. # 遗忘门
  21. F = torch.matmul(X, W_xf) + torch.matmul(H, W_hf) + b_f
  22. F = torch.sigmoid(F)
  23. # 输入门
  24. I = torch.sigmoid(torch.matmul(X,W_xi)+torch.matmul(H,W_hi) + b_i)
  25. C_tilde = torch.tanh(torch.matmul(X, W_xc) + torch.matmul(H, W_hc) + b_c)
  26. C = F * C + I * C_tilde
  27. # 输出门
  28. O = torch.sigmoid(torch.matmul(X,W_xo)+torch.matmul(H,W_ho) + b_o)
  29. H = O * C.tanh()
  30. # 输出层
  31. Y = torch.matmul(H,W_hq) + b_q
  32. return Y,(H,C)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/479290
推荐阅读
相关标签
  

闽ICP备14008679号