def __init__(self, max_sent_len, word_embed_dim, filter_num, filter_window_size, border_mode='valid', activation='relu', name='Convolution2d'): super(Convolution2d, self).__init__() self.init = initializations.get('uniform') self.activation = activations.get(activation) self.border_mode = border_mode self.W = self.init((filter_num, 1, filter_window_size, word_embed_dim), scale=0.01, name='W') self.b = shared_zeros((filter_num), name='b') self.params = [self.W, self.b] if self.border_mode == 'valid': self.ds = (max_sent_len - filter_window_size + 1, 1) elif self.border_mode == 'full': self.ds = (max_sent_len + filter_window_size - 1, 1) if name is not None: self.set_name(name)
def __init__(self): # self.node_embedding = Embedding(config.node_num, config.node_embed_dim, name='node_embed') self.query_embedding = Embedding(config.source_vocab_size, config.word_embed_dim, name='query_embed') if config.encoder == 'bilstm': self.query_encoder_lstm = BiLSTM(config.word_embed_dim, config.encoder_hidden_dim / 2, return_sequences=True, name='query_encoder_lstm') else: self.query_encoder_lstm = LSTM(config.word_embed_dim, config.encoder_hidden_dim, return_sequences=True, name='query_encoder_lstm') self.decoder_lstm = CondAttLSTM(config.rule_embed_dim + config.node_embed_dim + config.rule_embed_dim, config.decoder_hidden_dim, config.encoder_hidden_dim, config.attention_hidden_dim, name='decoder_lstm') self.src_ptr_net = PointerNet() self.terminal_gen_softmax = Dense(config.decoder_hidden_dim, 2, activation='softmax', name='terminal_gen_softmax') self.rule_embedding_W = initializations.get('normal')((config.rule_num, config.rule_embed_dim), name='rule_embedding_W', scale=0.1) self.rule_embedding_b = shared_zeros(config.rule_num, name='rule_embedding_b') self.node_embedding = initializations.get('normal')((config.node_num, config.node_embed_dim), name='node_embed', scale=0.1) self.vocab_embedding_W = initializations.get('normal')((config.target_vocab_size, config.rule_embed_dim), name='vocab_embedding_W', scale=0.1) self.vocab_embedding_b = shared_zeros(config.target_vocab_size, name='vocab_embedding_b') # decoder_hidden_dim -> action embed self.decoder_hidden_state_W_rule = Dense(config.decoder_hidden_dim, config.rule_embed_dim, name='decoder_hidden_state_W_rule') # decoder_hidden_dim -> action embed self.decoder_hidden_state_W_token= Dense(config.decoder_hidden_dim + config.encoder_hidden_dim, config.rule_embed_dim, name='decoder_hidden_state_W_token') # self.rule_encoder_lstm.params self.params = self.query_embedding.params + self.query_encoder_lstm.params + \ self.decoder_lstm.params + self.src_ptr_net.params + self.terminal_gen_softmax.params + \ [self.rule_embedding_W, self.rule_embedding_b, self.node_embedding, self.vocab_embedding_W, self.vocab_embedding_b] + \ self.decoder_hidden_state_W_rule.params + self.decoder_hidden_state_W_token.params self.srng = RandomStreams()
def __init__(self, input_dim, output_dim, init='uniform', name=None): super(Embedding, self).__init__() self.init = initializations.get(init) self.input_dim = input_dim self.output_dim = output_dim self.W = self.init((self.input_dim, self.output_dim), scale=0.1) self.params = [self.W] if name is not None: self.set_name(name)
def __init__(self, input_dim, output_dim, init='glorot_uniform', activation='tanh', name='Dense'): super(Dense, self).__init__() self.init = initializations.get(init) self.activation = activations.get(activation) self.input_dim = input_dim self.output_dim = output_dim self.input = T.matrix() self.W = self.init((self.input_dim, self.output_dim)) self.b = shared_zeros((self.output_dim)) self.params = [self.W, self.b] if name is not None: self.set_name(name)
def __init__(self, embed_size, unfixed_embed_size, embed_dim, init='uniform', name='HybridEmbedding'): super(HybridEmbedding, self).__init__() self.init = initializations.get(init) self.unfixed_embed_size = unfixed_embed_size self.W_unfixed = self.init((embed_size, embed_dim)) self.W_fixed = self.init((embed_size, embed_dim)) self.W_fixed.name = 'HybridEmbedding_fiexed_embed_matrix' # print W_fixed # for id, row in enumerate(self.W_fixed.get_value()): # if id >= 400: print '[word %d]' % id, row self.params = [self.W_unfixed] if name is not None: self.set_name(name)
def __init__(self, input_dim, output_dim, context_dim, att_hidden_dim, init='glorot_uniform', inner_init='orthogonal', forget_bias_init='one', activation='tanh', inner_activation='sigmoid', name='CondAttLSTM'): super(CondAttLSTM, self).__init__() self.output_dim = output_dim self.init = initializations.get(init) self.inner_init = initializations.get(inner_init) self.forget_bias_init = initializations.get(forget_bias_init) self.activation = activations.get(activation) self.inner_activation = activations.get(inner_activation) self.context_dim = context_dim self.input_dim = input_dim # regular LSTM layer self.W_i = self.init((input_dim, self.output_dim)) self.U_i = self.inner_init((self.output_dim, self.output_dim)) self.C_i = self.inner_init((self.context_dim, self.output_dim)) self.H_i = self.inner_init((self.output_dim, self.output_dim)) self.P_i = self.inner_init((self.output_dim, self.output_dim)) self.b_i = shared_zeros((self.output_dim)) self.W_f = self.init((input_dim, self.output_dim)) self.U_f = self.inner_init((self.output_dim, self.output_dim)) self.C_f = self.inner_init((self.context_dim, self.output_dim)) self.H_f = self.inner_init((self.output_dim, self.output_dim)) self.P_f = self.inner_init((self.output_dim, self.output_dim)) self.b_f = self.forget_bias_init((self.output_dim)) self.W_c = self.init((input_dim, self.output_dim)) self.U_c = self.inner_init((self.output_dim, self.output_dim)) self.C_c = self.inner_init((self.context_dim, self.output_dim)) self.H_c = self.inner_init((self.output_dim, self.output_dim)) self.P_c = self.inner_init((self.output_dim, self.output_dim)) self.b_c = shared_zeros((self.output_dim)) self.W_o = self.init((input_dim, self.output_dim)) self.U_o = self.inner_init((self.output_dim, self.output_dim)) self.C_o = self.inner_init((self.context_dim, self.output_dim)) self.H_o = self.inner_init((self.output_dim, self.output_dim)) self.P_o = self.inner_init((self.output_dim, self.output_dim)) self.b_o = shared_zeros((self.output_dim)) self.params = [ self.W_i, self.U_i, self.b_i, self.C_i, self.H_i, self.P_i, self.W_c, self.U_c, self.b_c, self.C_c, self.H_c, self.P_c, self.W_f, self.U_f, self.b_f, self.C_f, self.H_f, self.P_f, self.W_o, self.U_o, self.b_o, self.C_o, self.H_o, self.P_o, ] # attention layer self.att_ctx_W1 = self.init((context_dim, att_hidden_dim)) self.att_h_W1 = self.init((output_dim, att_hidden_dim)) self.att_b1 = shared_zeros((att_hidden_dim)) self.att_W2 = self.init((att_hidden_dim, 1)) self.att_b2 = shared_zeros((1)) self.params += [ self.att_ctx_W1, self.att_h_W1, self.att_b1, self.att_W2, self.att_b2 ] # attention over history self.hatt_h_W1 = self.init((output_dim, att_hidden_dim)) self.hatt_hist_W1 = self.init((output_dim, att_hidden_dim)) self.hatt_b1 = shared_zeros((att_hidden_dim)) self.hatt_W2 = self.init((att_hidden_dim, 1)) self.hatt_b2 = shared_zeros((1)) self.params += [ self.hatt_h_W1, self.hatt_hist_W1, self.hatt_b1, self.hatt_W2, self.hatt_b2 ] self.set_name(name)