def __init__(self, num_input, num_hidden, input_layers=None, name="", go_backwards=False): """ MGU Layer from 周志华paper Takes as input sequence of inputs, returns sequence of outputs """ self.name = name self.num_input = num_input self.num_hidden = num_hidden if len(input_layers) >= 2: # axis=1 an row xiang jia self.X = T.concatenate( [input_layer.output() for input_layer in input_layers], axis=1) else: self.X = input_layers[0].output() self.h0 = theano.shared(floatX(np.zeros(num_hidden))) self.s0 = theano.shared(floatX(np.zeros(num_hidden))) self.go_backwards = go_backwards self.W_fx = random_weights((num_input, num_hidden), name=self.name + "W_fx") self.W_fh = random_weights((num_hidden, num_hidden), name=self.name + "W_fh") self.U_h = random_weights((num_input, num_hidden), name=self.name + "U_h") self.W_h = random_weights((num_hidden, num_hidden), name=self.name + "W_h") self.b_f = zeros(num_hidden, name=self.name + "b_f") self.b_h = zeros(num_hidden, name=self.name + "b_h") self.params = [self.W_fx, self.W_fh, self.b_f] self.output()
def __init__(self, num_input, num_hidden, input_layers=None, name="", go_backwards=False): self.name = name self.num_input = num_input self.num_hidden = num_hidden if len(input_layers) >= 2: self.X = T.concatenate( [input_layer.output() for input_layer in input_layers], axis=1) else: self.X = input_layers[0].output() self.h0 = theano.shared(floatX(np.zeros(num_hidden))) self.s0 = theano.shared(floatX(np.zeros(num_hidden))) self.go_backwards = go_backwards self.W_gx = random_weights((num_input, num_hidden), name=self.name + "W_gx") self.W_ix = random_weights((num_input, num_hidden), name=self.name + "W_ix") self.W_fx = random_weights((num_input, num_hidden), name=self.name + "W_fx") self.W_ox = random_weights((num_input, num_hidden), name=self.name + "W_ox") self.W_gh = random_weights((num_hidden, num_hidden), name=self.name + "W_gh") self.W_ih = random_weights((num_hidden, num_hidden), name=self.name + "W_ih") self.W_fh = random_weights((num_hidden, num_hidden), name=self.name + "W_fh") self.W_oh = random_weights((num_hidden, num_hidden), name=self.name + "W_oh") self.b_g = zeros(num_hidden, name=self.name + "b_g") self.b_i = zeros(num_hidden, name=self.name + "b_i") self.b_f = zeros(num_hidden, name=self.name + "b_f") self.b_o = zeros(num_hidden, name=self.name + "b_o") self.params = [ self.W_gx, self.W_ix, self.W_ox, self.W_fx, self.W_gh, self.W_ih, self.W_oh, self.W_fh, self.b_g, self.b_i, self.b_f, self.b_o, ] self.output()
def reset_state(self): self.h0 = theano.shared(floatX(np.zeros(self.num_hidden))) self.s0 = theano.shared(floatX(np.zeros(self.num_hidden)))