def check_value_check(self): if self.valid: # Check if it throws nothing functions.hstack(self.xs) else: with self.assertRaises(type_check.InvalidType): functions.hstack(self.xs)
def _construct_relation_embedding(trig_embedding, pair, entities_ids, triggers_ids, bilstm_i, structures_above_threshold=None): relation = pair[0] action = pair[1] if action == const.ACTION_NONE: action_embedding = Variable(self.xp.zeros((self.action_dim), dtype=self.xp.float32)) else: action_embedding = self.embed_action(self.xp.array([action]).astype("i")) if relation[1] == const.NONE_ROLE_TYPE: role = 0 type_id = 0 role_type_embedding = None try: role_type_embedding = self.embed_roletype(self.xp.array([role]).astype("i")) except: print("debug") type_embedding = self.embed_enttype(self.xp.array([type_id]).astype("i")) mention = [0] # TODO: how to represent this better? mention_ids = _get_word_ids(instance, mention) mention_embedding = _represent_mentions(mention_ids, bilstm_i) flattened_type_embedding = F.flatten(type_embedding) flattened_mention_embedding = F.flatten(mention_embedding) type_and_argument_embedding = F.hstack([flattened_type_embedding, flattened_mention_embedding]) arg_embedding = F.reshape(type_and_argument_embedding, (1, self.len_type_and_arg)) relation_embedding = [] a = F.flatten(trig_embedding) b = F.flatten(role_type_embedding) c = F.flatten(arg_embedding) d = F.flatten(action_embedding) z = F.hstack([a, b, c, d]) emb = F.reshape(z, (1, self.len_relation + self.action_dim)) relation_embedding.append(emb) else: role = relation[0] arg = relation[1] role_type_embedding = self.embed_roletype(self.xp.array([role]).astype("i")) is_trigger = arg in triggers_ids if is_trigger: arg_embedding = _represent_type_and_argument(triggers_ids, const.IDS_TRIGGERS_IDX, bilstm_i, arg, structures_above_threshold) else: arg_embedding = _represent_type_and_argument(entities_ids, const.IDS_ENTITIES_IDX, bilstm_i, arg) relation_embedding = [] if len(arg_embedding) != 0: for i in range(len(arg_embedding)): a = F.flatten(trig_embedding) b = F.flatten(role_type_embedding) c = F.flatten(arg_embedding[i]) d = F.flatten(action_embedding) z = F.hstack([a, b, c, d]) emb = F.reshape(z, (1, self.len_relation + self.action_dim)) relation_embedding.append(emb) return relation_embedding
def _construct_relation_embedding(trig_embedding, relation, batch_i, bilstm_i, structures_above_threshold=None): role = relation[0][0] arg = relation[0][1] io = relation[1] role_type_embedding = self.embed_roletype( np.array([role]).astype("i")) triggers = batch_i[const.IDS_TRIGGERS_IDX] is_trigger = arg in triggers if is_trigger: arg_embedding = _represent_type_and_argument( batch_i, const.IDS_TRIGGERS_IDX, bilstm_i, arg, structures_above_threshold) else: arg_embedding = _represent_type_and_argument( batch_i, const.IDS_ENTITIES_IDX, bilstm_i, arg) io_embedding = self.embed_io(np.array([io]).astype('i')) relation_embedding = [] if len(arg_embedding) != 0: for i in range(len(arg_embedding)): a = F.flatten(trig_embedding) b = F.flatten(role_type_embedding) c = F.flatten(arg_embedding[i]) d = F.flatten(io_embedding) z = F.hstack([a, b, c, d]) emb = F.reshape(z, (1, self.len_relation)) relation_embedding.append(emb) return relation_embedding
def __call__(self, x): y = [] for tt in range(x.shape[1]): y.append(self[1](self[0](x[:, tt, :]))) y = F.hstack(y) y = F.reshape(y, (x.shape[0], x.shape[1], self.n_out)) return y
def greedy_actions(self): actions = [] for branch in self.branches: actions.append(branch.q_values.array.argmax(axis=1).reshape(-1, 1)) return F.hstack(actions)
def max(self): chosen_q_values = [] for branch in self.branches: chosen_q_values.append(branch.max.reshape(-1, 1)) return F.hstack(chosen_q_values)
def update_core(self): batch = self._iterators['main'].next() optimizer = self._optimizers['main'] words = [self.xp.array(x['words']).astype('i') for x in batch] chars = [ self.xp.array(y, dtype=self.xp.int32) for x in batch for y in x['chars'] ] tags = self.xp.vstack( [self.xp.array(x['tags']).astype('i') for x in batch]) optimizer.target.cleargrads() # Init index to keep track of words index_start = self.xp.arange(F.hstack(words).shape[0]) index_end = index_start + 1 index = self.xp.column_stack((index_start, index_end)) # Nest level + 1 max_depth = len(batch[0]['tags'][0]) batch_loss = 0 for depth in range(max_depth): accuracy, loss, next, index, _, words, chars = optimizer.target( chars, words, tags[:, depth], index, True) batch_loss += loss if not next: break batch_loss.backward() optimizer.update()
def predict(data_iter, model, mode): for batch in data_iter: raw_words = [x['str_words'] for x in batch] words = [model.xp.array(x['words']).astype('i') for x in batch] chars = [ model.xp.array(y).astype('i') for x in batch for y in x['chars'] ] tags = model.xp.vstack( [model.xp.array(x['tags']).astype('i') for x in batch]) # Init index to keep track of words index_start = model.xp.arange(F.hstack(words).shape[0]) index_end = index_start + 1 index = model.xp.column_stack((index_start, index_end)) # Maximum number of hidden layers = maximum nested level + 1 max_depth = len(batch[0]['tags'][0]) sentence_len = np.array([x.shape[0] for x in words]) section = np.cumsum(sentence_len[:-1]) predicts_depths = model.xp.empty( (0, int(model.xp.sum(sentence_len)))).astype('i') for depth in range(max_depth): next, index, extend_predicts, words, chars = model.predict( chars, words, tags[:, depth], index, mode) predicts_depths = model.xp.vstack( (predicts_depths, extend_predicts)) if not next: break predicts_depths = model.xp.split(predicts_depths, section, axis=1) ts_depths = model.xp.split(model.xp.transpose(tags), section, axis=1) yield ts_depths, predicts_depths, raw_words
def _represent_type_and_argument(arg_ids, type_index, bilstm_i, type_label, structures_above_threshold=None): embedding_list = [] if structures_above_threshold is not None: embedding_list = structures_above_threshold[type_label] else: defn = arg_ids[type_label] type_id = defn[const.IDS_ARG_TYPE] type_embedding = None if type_index == const.IDS_TRIGGERS_IDX: type_embedding = self.embed_trigtype( self.xp.array([type_id]).astype("i")) elif type_index == const.IDS_ENTITIES_IDX: type_embedding = self.embed_enttype( self.xp.array([type_id]).astype("i")) mention = defn[const.IDS_ARG_MENTION] mention_ids = _get_word_ids(instance, mention) mention_embedding = _represent_mentions(mention_ids, bilstm_i) flattened_type_embedding = F.flatten(type_embedding) flattened_mention_embedding = F.flatten(mention_embedding) type_and_argument_embedding = F.hstack( [flattened_type_embedding, flattened_mention_embedding]) reshaped_type_and_argument_embedding = F.reshape( type_and_argument_embedding, (1, self.len_type_and_arg)) embedding_list.append(reshaped_type_and_argument_embedding) return embedding_list
def char_encode(self, char_inputs, **kwargs): if self.char_dim is None: return char_features = [] char_embs = [] for char_input in char_inputs: # TODO (himkt) remove this hacky workaround # if asarray is not provided, # sometimes char_input.shape be 0-d array char_input = self.xp.asarray(char_input, dtype=self.xp.int32) char_emb = self.embed_char(char_input) char_embs.append(char_emb) batch_size = len(char_embs) shape = [2, batch_size, self.char_hidden_dim] h_0, c_0 = self.create_init_state(shape) hs, _, _ = self.char_level_bilstm(h_0, c_0, char_embs) _, batch_size, _ = hs.shape hs = hs.transpose([1, 0, 2]) hs = hs.reshape(batch_size, -1) char_features.append(hs) # final timestep for each sequence return F.hstack(char_features)
def recognize(self, h, recog_args): ''' :param h: :param recog_args: :return: ''' logging.info('input lengths: ' + str(h.shape[0])) # initialization c = None z = None att_w = None y_seq = [] self.att.reset() # reset pre-computation of h # preprate sos y = self.xp.full(1, self.sos, 'i') maxlen = int(recog_args.maxlenratio * h.shape[0]) minlen = int(recog_args.minlenratio * h.shape[0]) logging.info('max output length: ' + str(maxlen)) logging.info('min output length: ' + str(minlen)) for i in six.moves.range(minlen, maxlen): ey = self.embed(y) # utt list (1) x zdim att_c, att_w = self.att([h], z, att_w) ey = F.hstack((ey, att_c)) # utt(1) x (zdim + hdim) c, z = self.decoder(c, z, ey) y = self.xp.argmax(self.output(z).data, axis=1).astype('i') y_seq.append(y) # terminate decoding if y == self.eos: break return y_seq
def __call__(self, input_vector, h_in): batch_size = input_vector.shape[0] if self.min_batch_size is None: self.min_batch_size = batch_size else: assert (batch_size <= self.min_batch_size) self.max_batch_size = batch_size if self.h is None: h_prev = var_fzeros((batch_size, self.n_units_of_hidden_layer)) else: h_prev = self.h[0:batch_size] if h_in is None: h_in = var_fzeros((batch_size, self.n_units_of_hidden_layer)) else: assert (batch_size == h_in.shape[0]) v = F.hstack([input_vector, h_prev, h_in]) i = F.sigmoid(self.linear_i(v)) f = F.sigmoid(self.linear_f(v)) s = f * self.state + i * F.tanh(self.linear_s(v)) o = F.sigmoid(self.linear_o(v)) h = o * F.tanh(s) self.state = s self.h = h return h
def _represent_type_and_argument(batch_i, type_index, bilstm_i, type_label, structures_above_threshold=None): def _get_word_ids(xsi, mention): word_ind = [] sentence_ids = xsi[const.IDS_SENTENCE_INFO_IDX][ const.IDS_SENTENCE_IDX] for i in mention: if i in sentence_ids: ind = sentence_ids.index(i) word_ind.append(ind) return word_ind embedding_list = [] if structures_above_threshold is not None: embedding_list = structures_above_threshold[type_label] else: defn = batch_i[type_index][type_label] type_id = defn[const.IDS_ARG_TYPE] type_embedding = None if type_index == const.IDS_TRIGGERS_IDX: if self.REPLACE_TYPE: trig_word = self.id2triggertype[type_id] new_arg = Util.extract_category( trig_word, self.GENERALISATION, const.TYPE_GENERALISATION) assert new_arg != '', "ERROR: new_arg is '' " type_id = self.trigger_type2id[new_arg] type_embedding = self.embed_trigtype( np.array([type_id]).astype("i")) elif type_index == const.IDS_ENTITIES_IDX: if self.REPLACE_TYPE: ent_word = self.id2entitytype[type_id] new_arg = Util.extract_category( ent_word, self.GENERALISATION, const.TYPE_GENERALISATION) assert new_arg != '', "ERROR: new_arg is '' " type_id = self.entity_type2id[new_arg] type_embedding = self.embed_argtype( np.array([type_id]).astype("i")) mention = defn[const.IDS_ARG_MENTION] mention_ids = _get_word_ids(batch_i, mention) mention_embedding = _represent_mentions(mention_ids, bilstm_i) flattened_type_embedding = F.flatten(type_embedding) flattened_mention_embedding = F.flatten(mention_embedding) type_and_argument_embedding = F.hstack( [flattened_type_embedding, flattened_mention_embedding]) reshaped_type_and_argument_embedding = F.reshape( type_and_argument_embedding, (1, self.len_type_and_arg)) embedding_list.append(reshaped_type_and_argument_embedding) return embedding_list
def word_encode(self, word_sentence): word_features = [] if self.word_dim is not None: wemb = self.embed_word(word_sentence) word_features.append(wemb) return F.hstack(word_features)
def evaluate(self): iterator = self._iterators['main'] target = self._targets['main'] it = copy.copy(iterator) summary = reporter.DictSummary() ys_final, ts_final, raw_xs = [], [], [] for batch in it: # Read batch data and sort sentences in descending order for CRF layer observation = {} raw_words = [x['str_words'] for x in batch] words = [self.xp.array(x['words']).astype('i') for x in batch] chars = [ self.xp.array(y, dtype=self.xp.int32) for x in batch for y in x['chars'] ] tags = self.xp.vstack( [self.xp.array(x['tags']).astype('i') for x in batch]) # Init index to keep track of words index_start = self.xp.arange(F.hstack(words).shape[0]) index_end = index_start + 1 index = self.xp.column_stack((index_start, index_end)) # Nest level + 1 max_depth = len(batch[0]['tags'][0]) sentence_len = xp.array([x.shape[0] for x in words]) section = xp.cumsum(sentence_len[:-1]) # Init predicts_depths = self.xp.empty( (0, self.xp.sum(sentence_len))).astype('i') with reporter.report_scope(observation): for depth in range(max_depth): accuracy, loss, next, index, extend_predicts, words, chars = target( chars, words, tags[:, depth], index, False) predicts_depths = self.xp.vstack( (predicts_depths, extend_predicts)) if not next: break summary.add(observation) predicts_depths = self.xp.split(predicts_depths, section, axis=1) ts_depths = self.xp.split(self.xp.transpose(tags), section, axis=1) ys_final.extend(predicts_depths) ts_final.extend(ts_depths) raw_xs.extend(raw_words) fmeasure = summary.compute_mean() fmeasure['dev/main/fscore'] = evaluate(target, ys_final, ts_final, raw_xs) return fmeasure
def w_hstack(self): return F.hstack([ F.flatten(self.w1.W), F.flatten(self.w1.b), F.flatten(self.w2.W), F.flatten(self.w2.b), F.flatten(self.w3.W), F.flatten(self.w3.b) ])
def mu_hstack(self): return F.hstack([ F.flatten(self.mu1.W), F.flatten(self.mu1.b), F.flatten(self.mu2.W), F.flatten(self.mu2.b), F.flatten(self.mu3.W), F.flatten(self.mu3.b) ])
def evaluate_actions(self, actions): branch_q_values = [] for i, branch in enumerate(self.branches): branch_actions = actions[:, i] branch_q_values.append(branch.evaluate_actions( branch_actions).reshape(-1, 1)) return F.hstack(branch_q_values)
def sigma_hstack(self): return F.log(1 + F.exp( F.hstack([ F.flatten(self.pho1.W), F.flatten(self.pho1.b), F.flatten(self.pho2.W), F.flatten(self.pho2.b), F.flatten(self.pho3.W), F.flatten(self.pho3.b) ])))
def __call__(self, xs): """ Args: xs (list or tuple): batch-length list of Variable, each with shape ( Returns: """ h_fw, c_fw = self.fw(xs) h_bw, c_bw = self.bw([x[::-1] for x in xs]) h = [ F.hstack((hi_fw, hi_bw[::-1])) for hi_fw, hi_bw in zip(h_fw, h_bw) ] c = [ F.hstack((ci_fw, ci_bw[::-1])) for ci_fw, ci_bw in zip(c_fw, c_bw) ] return h, c
def __call__(self, x_p, x_v, ious=None): g_l, g_v = self.gate(x_p, x_v) h_l = F.tanh(self.bn_l0(self.l_phr(x_p))) h_v = F.tanh(self.bn_v0(self.l_img(x_v))) h = g_l * h_l + g_v * h_v if ious is not None: h = F.hstack([h, ious]) h = F.relu(self.bn_1(self.l_1(h))) h = self.cls(h) h = F.flatten(h) return h
def get_gaussian_for_attention(self, bboxes): resize_shapes = [self.global_shapes for _ in range(len(bboxes))] xp = cuda.get_array_module(self.x_var.data) gaussians = [] for resize_shape, bbox in zip(resize_shapes, bboxes): G = self.get_gaussian(resize_shape, bbox) G = G / (xp.max(G.data) + 1e-15) gaussians.append( F.hstack( [G, Variable(xp.ones(self.local_num, dtype=xp.float32))])) gaussians = F.stack(gaussians, axis=0) return gaussians
def extract(self, x): batch_size = x.shape[0] x = F.reshape(x, self.chain((batch_size, 1), self.shapes[0])) mask = F.broadcast_to(self.mask, x.shape) h = F.hstack((x, mask)) for layer_idx, num_layers in enumerate(self.encoder_layers): for rep_idx in range(num_layers): conv = self.__getattribute__("conv_{}_{}".format( layer_idx, rep_idx)) h = conv(h) h = self.linear_extract(h) return h
def calculate_all_attentions(self, hs, ys): """Calculate all of attentions. Args: hs (list of chainer.Variable | N-dimensional array): Input variable from encoder. ys (list of chainer.Variable | N-dimensional array): Input variable of decoder. Returns: chainer.Variable: List of attention weights. """ # prepare input and output word sequences with sos/eos IDs eos = self.xp.array([self.eos], "i") sos = self.xp.array([self.sos], "i") ys_in = [F.concat([sos, y], axis=0) for y in ys] ys_out = [F.concat([y, eos], axis=0) for y in ys] # padding for ys with -1 # pys: utt x olen pad_ys_in = F.pad_sequence(ys_in, padding=self.eos) pad_ys_out = F.pad_sequence(ys_out, padding=-1) # get length info olength = pad_ys_out.shape[1] # initialization c_list = [None] # list of cell state of each layer z_list = [None] # list of hidden state of each layer for _ in six.moves.range(1, self.dlayers): c_list.append(None) z_list.append(None) att_w = None att_ws = [] self.att.reset() # reset pre-computation of h # pre-computation of embedding eys = self.embed(pad_ys_in) # utt x olen x zdim eys = F.separate(eys, axis=1) # loop for an output sequence for i in six.moves.range(olength): att_c, att_w = self.att(hs, z_list[0], att_w) ey = F.hstack((eys[i], att_c)) # utt x (zdim + hdim) z_list, c_list = self.rnn_forward(ey, z_list, c_list, z_list, c_list) att_ws.append(att_w) # for debugging att_ws = F.stack(att_ws, axis=1) att_ws.to_cpu() return att_ws.data
def forward(self, xin, targets): """Compute total loss to train.""" vctx, vq, va, supps = xin # (B, Cs, C), (B, Q), (B, A), (B, I) rvctx, rvq, rva, rsupps = self.predictor.vrules # (R, Ls, L), (R, Q), (R, A), (R, I) # --------------------------- # Compute main loss predictions = self.predictor(xin) # (B, V) mainloss = F.softmax_cross_entropy(predictions, targets) # () acc = F.accuracy(predictions, targets) # () # --------------------------- # Compute aux losses vmaploss = F.sum(self.predictor.log['vmap'][0]) # () uattloss = F.stack(self.predictor.log['raw_uni_cands_att'], 1) # (B, I, Cs) uattloss = F.softmax_cross_entropy( F.reshape(uattloss, (-1, vctx.shape[1])), supps.flatten()) # () # --- oattloss = F.stack(self.predictor.log['raw_orig_cands_att'], 1) # (B, I, Cs) oattloss = F.softmax_cross_entropy( F.reshape(oattloss, (-1, vctx.shape[1])), supps.flatten()) # () # --- battloss = F.stack(self.predictor.log['raw_body_att'], 1) # (R, I, Ls) riters = min(rsupps.shape[-1], supps.shape[-1]) battloss = F.softmax_cross_entropy( F.reshape(battloss[:, :riters], (-1, rvctx.shape[1])), rsupps[:, :riters].flatten()) # () # --- rpredloss = F.softmax_cross_entropy(self.predictor.log['rpred'][0], rva[:, 0]) # () opred = self.predictor.log['opred'][0] # (B, V) opredloss = F.softmax_cross_entropy(opred, va[:, 0]) # () oacc = F.accuracy(opred, va[:, 0]) # () # --- uniloss = F.hstack(self.predictor.log['uniloss']) # (I+1,) uniloss = F.mean(uniloss) # () # --- C.report( { 'loss': mainloss, 'vmap': vmaploss, 'uatt': uattloss, 'oatt': oattloss, 'batt': battloss, 'rpred': rpredloss, 'opred': opredloss, 'uni': uniloss, 'oacc': oacc, 'acc': acc }, self) return self.uniparam * (mainloss + 0.1 * vmaploss + STRONG * (uattloss + battloss) + rpredloss + uniloss) + STRONG * oattloss + opredloss # ()
def __call__(self, x, wr=0.0): #h_m = FX.wsa(self.conv1(x), wr=wr) #h_s = x[:, :, 1:-1, 1:-1] h = FX.wsa(self.conv1s(x), wr=wr) #h = F.hstack((h_m, h_s)) h = F.max_pooling_2d(h, 4, stride=2) h_m = FX.wsa(self.conv2(h), wr=wr) h_s = FX.wsa(self.conv2s(h), wr=wr) h = F.hstack((h_m, h_s)) h = F.max_pooling_2d(h, 4, stride=2) return h
def __call__(self, x, wr=0.0): h_xl = FX.wsa(self.conv1xl(x), wr=wr) h_l = FX.wsa(self.conv1(x), wr=wr) h_m = x[:, :, 1:-1, 1:-1] h_m = FX.wsa(self.conv1m(h_m), wr=wr) h_s = x[:, :, 2:-2, 2:-2] h_s = FX.wsa(self.conv1s(h_s), wr=wr) h_xs = x[:, :, 3:-3, 3:-3] h_xs = FX.wsa(self.conv1xs(h_xs), wr=wr) h = F.hstack((h_xl, h_l, h_m, h_s, h_xs)) h = F.max_pooling_2d(h, 3, stride=2) h_m = FX.wsa(self.conv2(h), wr=wr) h_s = FX.wsa(self.conv2s(h), wr=wr) h = F.hstack((h_m, h_s)) h = F.max_pooling_2d(h, 3, stride=2) h_m = self.conv3(h) h_s = self.conv3s(h) h = F.hstack((h_m, h_s)) h = F.max_pooling_2d(h, 3, stride=2) return h
def forward(self, hs_flatten, pairs, ckeys, lengths): features = [] if self.use_sim: features.extend( self._feature_sim(hs_flatten, pairs, ckeys, lengths)) if self.use_repl: features.extend( self._feature_repl(hs_flatten, pairs, ckeys, lengths)) if not (self.use_sim or self.use_repl): features.extend( self._forward_spans(hs_flatten, pairs, ckeys, lengths)) fs = F.hstack(features) return fs
def __call__(self, input_vector, h_in): batch_size = input_vector.shape[0] if h_in is None: h_in = var_fzeros((batch_size, self.n_units_of_hidden_layer)) else: assert (h_in.shape[0] >= batch_size) h_in = h_in[0:batch_size] v = F.hstack([input_vector, h_in]) h = F.tanh(self.l1(v)) return h
def compute_accuracy(self, ys, ts): ts_tags, ts_actions = ts.T accuracy = [] if self._y_cache['tagger'] is not None: tag_accuracy = self._layers['tagger'] \ .compute_accuracy(self._y_cache['tagger'], ts_tags) self._record('tag_accuracy', tag_accuracy) accuracy.append(tag_accuracy) if self._y_cache['parser'] is not None: action_accuracy = self._layers['parser'] \ .compute_accuracy(self._y_cache['parser'], ts_actions) self._record('action_accuracy', action_accuracy) accuracy.append(action_accuracy) return F.mean(F.hstack(accuracy))
def forward(self, inputs, device): y = functions.hstack(inputs) return y,
def check_forward(self, xs_data): xs = [chainer.Variable(x) for x in xs_data] y = functions.hstack(xs) expect = numpy.hstack(self.xs) testing.assert_allclose(y.data, expect)
def func(*xs): y = functions.hstack(xs) return y * y
def func(*xs): return functions.hstack(xs)