numpy实现RNN原理实现

首先说明代码只是帮助理解,并未写出梯度下降部分,默认参数已经被固定,不影响理解。代码主要实现rnn原理,只使用numpy库,不可用于gpu加速。

import numpy as np


class rnn():

  def __init__(self, input_size, hidden_size, num_layers, bidirectional=false):
    self.input_size = input_size
    self.hidden_size = hidden_size
    self.num_layers = num_layers
    self.bidirectional = bidirectional

  def feed(self, x):
    '''

    :param x: [seq, batch_size, embedding]
    :return: out, hidden
    '''

    # x.shape [sep, batch, feature]
    # hidden.shape [hidden_size, batch]
    # whh0.shape [hidden_size, hidden_size] wih0.shape [hidden_size, feature]
    # whh1.shape [hidden_size, hidden_size] wih1.size [hidden_size, hidden_size]

    out = []
    x, hidden = np.array(x), [np.zeros((self.hidden_size, x.shape[1])) for i in range(self.num_layers)]
    wih = [np.random.random((self.hidden_size, self.hidden_size)) for i in range(1, self.num_layers)]
    wih.insert(0, np.random.random((self.hidden_size, x.shape[2])))
    whh = [np.random.random((self.hidden_size, self.hidden_size)) for i in range(self.num_layers)]

    time = x.shape[0]
    for i in range(time):
      hidden[0] = np.tanh((np.dot(wih[0], np.transpose(x[i, ...], (1, 0))) +
               np.dot(whh[0], hidden[0])
               ))

      for i in range(1, self.num_layers):
        hidden[i] = np.tanh((np.dot(wih[i], hidden[i-1]) +
                   np.dot(whh[i], hidden[i])
                   ))

      out.append(hidden[self.num_layers-1])

    return np.array(out), np.array(hidden)


def sigmoid(x):
  return 1.0/(1.0 + 1.0/np.exp(x))


if __name__ == '__main__':
  rnn = rnn(1, 5, 4)
  input = np.random.random((6, 2, 1))
  out, h = rnn.feed(input)
  print(f'seq is {input.shape[0]}, batch_size is {input.shape[1]} ', 'out.shape ', out.shape, ' h.shape ', h.shape)
  # print(sigmoid(np.random.random((2, 3))))
  #
  # element-wise multiplication
  # print(np.array([1, 2])*np.array([2, 1]))

到此这篇关于numpy实现rnn原理实现的文章就介绍到这了,更多相关numpy实现rnn内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐