我们从Python开源项目中,提取了以下25个代码示例,用于说明如何使用chainer.functions.max()。
def __call__(self, x, t, train=True, finetune=False): h = self.l1(x, train, finetune) # h = F.dropout(h, self.dr, train) h = self.l2(h, train, finetune) h = plane_group_spatial_max_pooling(h, ksize=2, stride=2, pad=0, cover_all=True, use_cudnn=True) h = self.l3(h, train, finetune) # h = F.dropout(h, self.dr, train) h = self.l4(h, train, finetune) # h = F.dropout(h, self.dr, train) h = self.l5(h, train, finetune) # h = F.dropout(h, self.dr, train) h = self.l6(h, train, finetune) h = self.top(h) h = F.max(h, axis=-3, keepdims=False) h = F.max(h, axis=-1, keepdims=False) h = F.max(h, axis=-1, keepdims=False) return F.softmax_cross_entropy(h, t), F.accuracy(h, t)
def __call__(self, x, t, train=True, finetune=False): h = self.l1(x, train, finetune) h = F.dropout(h, self.dr, train) h = self.l2(h, train, finetune) h = F.max_pooling_2d(h, ksize=2, stride=2, pad=0, cover_all=True, use_cudnn=True) h = self.l3(h, train, finetune) h = F.dropout(h, self.dr, train) h = self.l4(h, train, finetune) h = F.dropout(h, self.dr, train) h = self.l5(h, train, finetune) h = F.dropout(h, self.dr, train) h = self.l6(h, train, finetune) h = F.dropout(h, self.dr, train) h = self.top(h) h = F.max(h, axis=-1, keepdims=False) h = F.max(h, axis=-1, keepdims=False) return F.softmax_cross_entropy(h, t), F.accuracy(h, t)
def avg_pool_max_pool(self, hs): num_output = len(hs[0]) houts = [] i = 0 shape = hs[0][i].shape h = F.dstack([F.reshape(h[i],(shape[0], -1)) for h in hs]) x = 1.0*F.sum(h,2)/h.shape[2] x = F.reshape(x, shape) houts.append(x) for i in range(1,num_output): shape = hs[0][i].shape h = F.dstack([F.reshape(h[i],(shape[0], -1)) for h in hs]) x = 1.0*F.max(h,2) x = F.reshape(x, shape) houts.append(x) return houts
def max_pool_avg_pool(self, hs): num_output = len(hs[0]) houts = [] i = 0 shape = hs[0][i].shape h = F.dstack([F.reshape(h[i],(shape[0], -1)) for h in hs]) x = 1.0*F.max(h,2) x = F.reshape(x, shape) houts.append(x) for i in range(1,num_output): shape = hs[0][i].shape h = F.dstack([F.reshape(h[i],(shape[0], -1)) for h in hs]) x = 1.0*F.sum(h,2)/h.shape[2] x = F.reshape(x, shape) houts.append(x) return houts
def __call__(self, chars): if not isinstance(chars, (tuple, list)): chars = [chars] char_ids, boundaries = self._create_sequence(chars) x = self.embed(self.xp.array(char_ids)) x = F.dropout(x, self._dropout) length, dim = x.shape C = self.conv(F.reshape(x, (1, 1, length, dim))) # C.shape -> (1, out_size, length, 1) C = F.split_axis(F.transpose(F.reshape(C, (self.out_size, length))), boundaries, axis=0) ys = F.max(F.pad_sequence( [matrix for i, matrix in enumerate(C) if i % 2 == 1], padding=-np.inf), axis=1) # max over time pooling # assert len(chars) == ys.shape[0] return ys
def calculate_score(self, h, pos, neg, pos_score=None, neg_score=None, multipos=False): #h_pro = self.act1(self.W_predict(h)) h_pro = h if multipos: # If multiple positive vectors are given, # max score is picked up. (other ones are not propagated) pos_scoreL = [F.batch_matmul(h_pro, pos_one, transa=True) for pos_one in pos] pos_score = F.max(F.concat(pos_scoreL, axis=1), axis=1, keepdims=True) else: pos_score = F.batch_matmul(h_pro, pos, transa=True) neg_score = F.batch_matmul(h_pro, neg, transa=True) return pos_score, neg_score
def weighted_cross_entropy(p,t,weight_arr,sec_arr,weigh_flag=True): print("p:{}".format(p.data.shape)) b = np.zeros(p.shape,dtype=np.float32) b[np.arange(p.shape[0]), t] = 1 soft_arr = F.softmax(p) log_arr = -F.log(soft_arr) xent = b*log_arr # # print("sec_arr:{}".format(sec_arr)) # print("xent_shape:{}".format(xent.data.shape)) xent = F.split_axis(xent,sec_arr,axis=0) print([xent_e.data.shape[0] for xent_e in xent]) x_sum = [F.reshape(F.sum(xent_e)/xent_e.data.shape[0],(1,1)) for xent_e in xent] # print("x_sum:{}".format([x_e.data for x_e in x_sum])) xent = F.concat(x_sum,axis=0) # # print("xent1:{}".format(xent.data)) xent = F.max(xent,axis=1)/p.shape[0] # print("xent2:{}".format(xent.data)) if not weigh_flag: return F.sum(xent) # print("wei_arr:{}".format(weight_arr)) # print("wei_arr:{}".format(weight_arr.data.shape)) print("xent3:{}".format(xent.data.shape)) wxent= F.matmul(weight_arr,xent,transa=True) wxent = F.sum(F.sum(wxent,axis=0),axis=0) print("wxent:{}".format(wxent.data)) return wxent
def __call__(self, x, t, train=True, finetune=False): h = self.l1(x, train, finetune) # h = F.dropout(h, self.dr, train) h = F.max(h, axis=-3, keepdims=False) h = self.l2(h, train, finetune) h = F.max(h, axis=-3, keepdims=False) h = F.max_pooling_2d(h, ksize=2, stride=2, pad=0) h = self.l3(h, train, finetune) h = F.max(h, axis=-3, keepdims=False) # h = F.dropout(h, self.dr, train) h = self.l4(h, train, finetune) h = F.max(h, axis=-3, keepdims=False) # h = F.dropout(h, self.dr, train) h = self.l5(h, train, finetune) h = F.max(h, axis=-3, keepdims=False) # h = F.dropout(h, self.dr, train) h = self.l6(h, train, finetune) h = F.max(h, axis=-3, keepdims=False) h = self.top(h) h = F.max(h, axis=-3, keepdims=False) h = F.max(h, axis=-1, keepdims=False) h = F.max(h, axis=-1, keepdims=False) return F.softmax_cross_entropy(h, t), F.accuracy(h, t)
def compute_vecs(self, word_ids, word_boundaries, phrase_num, char_vecs=None): word_ids = my_variable(word_ids, volatile=not self.train) word_embs = self.emb(word_ids) # total_len x dim word_embs_reshape = F.reshape(word_embs, (1, 1, -1, self.emb_dim)) if self.word_level_flag and char_vecs is not None: # print(char_vecs.data.shape) # print(word_embs.data.shape) word_embs = F.concat([word_embs, char_vecs], axis=1) # print(word_embs.data.shape) dim = self.emb_dim + self.add_dim word_embs_reshape = F.reshape(word_embs, (1, 1, -1, dim)) # 1 x 1 x total_len x dim # convolution word_emb_conv = self.conv(word_embs_reshape) # 1 x dim x total_len x 1 word_emb_conv_reshape = F.reshape(word_emb_conv, (self.hidden_dim, -1)) # max word_emb_conv_reshape = F.split_axis(word_emb_conv_reshape, word_boundaries, axis=1) embs = [F.max(word_emb_conv_word, axis=1) for i, word_emb_conv_word in enumerate(word_emb_conv_reshape) if i % 2 == 1] embs = F.concat(embs, axis=0) phrase_emb_conv = F.reshape(embs, (phrase_num, self.hidden_dim)) return phrase_emb_conv
def update(Q, target_Q, opt, samples, gamma=0.99, target_type='double_dqn'): xp = Q.xp s = np.ndarray(shape=(minibatch_size, STATE_LENGTH, FRAME_WIDTH, FRAME_HEIGHT), dtype=np.float32) a = np.asarray([sample[1] for sample in samples], dtype=np.int32) r = np.asarray([sample[2] for sample in samples], dtype=np.float32) done = np.asarray([sample[3] for sample in samples], dtype=np.float32) s_next = np.ndarray(shape=(minibatch_size, STATE_LENGTH, FRAME_WIDTH, FRAME_HEIGHT), dtype=np.float32) for i in xrange(minibatch_size): s[i] = samples[i][0] s_next[i] = samples[i][4] # to gpu if available s = xp.asarray(s) a = xp.asarray(a) r = xp.asarray(r) done = xp.asarray(done) s_next = xp.asarray(s_next) # Prediction: Q(s,a) y = F.select_item(Q(s), a) # Target: r + gamma * max Q_b (s',b) with chainer.no_backprop_mode(): if target_type == 'dqn': t = r + gamma * (1 - done) * F.max(target_Q(s_next), axis=1) elif target_type == 'double_dqn': t = r + gamma * (1 - done) * F.select_item( target_Q(s_next), F.argmax(Q(s_next), axis=1)) else: raise ValueError('Unsupported target_type: {}'.format(target_type)) loss = mean_clipped_loss(y, t) Q.cleargrads() loss.backward() opt.update()
def update(Q, target_Q, opt, samples, gamma=0.99, target_type='double_dqn'): xp = Q.xp s = np.ndarray(shape=(minibatch_size, STATE_LENGTH, FRAME_WIDTH, FRAME_HEIGHT), dtype=np.float32) a = np.asarray([sample[1] for sample in samples], dtype=np.int32) r = np.asarray([sample[2] for sample in samples], dtype=np.float32) done = np.asarray([sample[3] for sample in samples], dtype=np.float32) s_next = np.ndarray(shape=(minibatch_size, STATE_LENGTH, FRAME_WIDTH, FRAME_HEIGHT), dtype=np.float32) for i in xrange(minibatch_size): s[i] = samples[i][0] s_next[i] = samples[i][4] # to gpu if available s = xp.asarray(s) a = xp.asarray(a) r = xp.asarray(r) done = xp.asarray(done) s_next = xp.asarray(s_next) # Prediction: Q(s,a) y = F.select_item(Q(s), a) f0 = Q.conv1.data print f0.shape # Target: r + gamma * max Q_b (s',b) with chainer.no_backprop_mode(): if target_type == 'dqn': t = r + gamma * (1 - done) * F.max(target_Q(s_next), axis=1) elif target_type == 'double_dqn': t = r + gamma * (1 - done) * F.select_item( target_Q(s_next), F.argmax(Q(s_next), axis=1)) else: raise ValueError('Unsupported target_type: {}'.format(target_type)) loss = mean_clipped_loss(y, t) Q.cleargrads() loss.backward() opt.update()
def check_forward(self, x_data, axis=None, keepdims=False): x = chainer.Variable(x_data) y = functions.max(x, axis=axis, keepdims=keepdims) self.assertEqual(y.data.dtype, numpy.float32) y_expect = self.x.max(axis=axis, keepdims=keepdims) self.assertEqual(y.data.shape, y_expect.shape) gradient_check.assert_allclose(y_expect, y.data)
def test_backward_axis_cpu(self): for i in range(self.x.ndim): gy = numpy.ones_like(self.x.max(axis=i)) * self.gy self.check_backward(self.x, gy, axis=i)
def test_backward_negative_axis_cpu(self): gy = numpy.ones_like(self.x.max(axis=-1)) * self.gy self.check_backward(self.x, gy, axis=-1)
def test_backward_multi_axis_cpu(self): gy = numpy.ones_like(self.x.max(axis=(0, 1))) * self.gy self.check_backward(self.x, gy, axis=(0, 1))
def test_backward_multi_axis_invert_cpu(self): gy = numpy.ones_like(self.x.max(axis=(1, 0))) * self.gy self.check_backward(self.x, gy, axis=(1, 0))
def test_backward_negative_multi_axis_invert_cpu(self): gy = numpy.ones_like(self.x.max(axis=(-2, 0))) * self.gy self.check_backward(self.x, gy, axis=(-2, 0))
def test_pos_neg_duplicate_axis(self): with self.assertRaises(ValueError): self.x.max(axis=(1, -2))
def concat_max_pool(self, hs): num_output = len(hs[0]) houts = [] i = 0 x = F.concat([h[i] for h in hs],1) houts.append(x) for i in range(1,num_output): shape = hs[0][i].shape h = F.dstack([F.reshape(h[i],(shape[0], -1)) for h in hs]) x = 1.0*F.max(h,2) x = F.reshape(x, shape) houts.append(x) return houts
def max_pool(self, hs): num_output = len(hs[0]) houts = [] for i in range(num_output): shape = hs[0][i].shape h = F.dstack([F.reshape(h[i],(shape[0], -1)) for h in hs]) x = 1.0*F.max(h,2) x = F.reshape(x, shape) houts.append(x) return houts
def max_pool_concat(self, hs): num_output = len(hs[0]) houts = [] i = 0 x = F.max(F.dstack([h[i] for h in hs]),2) houts.append(x) for i in range(1,num_output): #x = 0 #for h in hs: # x = x + h[i] x = F.concat([h[i] for h in hs],1) houts.append(x) # Merged branch exit and main exit return houts
def __call__(self, chars): xp = self.xp if not isinstance(chars, (tuple, list)): chars = [chars] lengths = [len(_chars) for _chars in chars] n_words = len(lengths) pad_width = self._pad_width char_ids = F.PadSequence(length=max(lengths) + pad_width, padding=self._pad_id).forward(chars)[0] left_pads = xp.full((n_words, pad_width), self._pad_id, xp.int32) char_ids = xp.concatenate((left_pads, xp.array(char_ids)), axis=1) """note: cupy does not have `inf`.""" mask = xp.full(char_ids.shape, np.inf) for i, length in enumerate(lengths): mask[i, pad_width:pad_width + length] = 0 mask = xp.expand_dims(mask, axis=2) xs = self.embed(char_ids) xs = F.dropout(xs, self._dropout) C = self.conv(F.expand_dims(xs, axis=1)) C = F.transpose(F.squeeze(C, axis=3), (0, 2, 1)) """ assert C.shape == (n_words, pad_width + max(lengths) + pad_width, self.out_size) """ ys = F.max(C - mask, axis=1) return ys
def _pool_and_predict( self, indices_and_rois, h_cls_seg, h_locs, gt_roi_labels=None): # PSROI Pooling # shape: (n_rois, n_class*2, roi_size, roi_size) pool_cls_seg = _psroi_pooling_2d_yx( h_cls_seg, indices_and_rois, self.roi_size, self.roi_size, self.spatial_scale, group_size=self.group_size, output_dim=self.n_class*2) # shape: (n_rois, n_class, 2, roi_size, roi_size) pool_cls_seg = pool_cls_seg.reshape( (-1, self.n_class, 2, self.roi_size, self.roi_size)) # shape: (n_rois, 2*4, roi_size, roi_size) pool_locs = _psroi_pooling_2d_yx( h_locs, indices_and_rois, self.roi_size, self.roi_size, self.spatial_scale, group_size=self.group_size, output_dim=2*4) # Classfication # Group Max # shape: (n_rois, n_class, roi_size, roi_size) h_cls = pool_cls_seg.transpose((0, 1, 3, 4, 2)) h_cls = F.max(h_cls, axis=4) # Global pooling (vote) # shape: (n_rois, n_class) roi_cls_scores = _global_average_pooling_2d(h_cls) # Bbox Regression # shape: (n_rois, 2*4) roi_cls_locs = _global_average_pooling_2d(pool_locs) n_rois = roi_cls_locs.shape[0] roi_cls_locs = roi_cls_locs.reshape((n_rois, 2, 4)) # Mask Regression # shape: (n_rois, n_class, 2, roi_size, roi_size) # Group Pick by Score if gt_roi_labels is None: max_cls_idx = roi_cls_scores.array.argmax(axis=1) else: max_cls_idx = gt_roi_labels # shape: (n_rois, 2, roi_size, roi_size) roi_seg_scores = pool_cls_seg[np.arange(len(max_cls_idx)), max_cls_idx] return roi_seg_scores, roi_cls_locs, roi_cls_scores
def solve(self, docD, train=True): old2newD, e2sD = self.initialize_entities(docD["entities"], self.args.max_ent_id, train=train) e2dLD = dict((e, [s]) for (e, s) in e2sD.items()) sentences = self.reload_sentences(docD["sentences"], old2newD) for sent in sentences: i2sD = OrderedDict() e2iLD = defaultdict(list) for i, token in enumerate(sent): if token in e2sD: i2sD[i] = e2sD[token] e2iLD[token].append(i) if not i2sD: # skip sentences without any entities continue e2iLD = OrderedDict(e2iLD) concat_h_L = self.encode_context(sent, i2sD, e2iLD, train=train) for e, concat_h in zip(e2iLD.keys(), concat_h_L): e2dLD[e].append(F.tanh(self.W_hd(concat_h))) e2sD[e] = F.max(F.concat([e2sD[e], e2dLD[e][-1]], axis=0), axis=0, keepdims=True) EPS = sys.float_info.epsilon accum_loss_doc, TorFs, subTorFs = 0, 0, 0 for query, answer in zip(docD["queries"], docD["answers"]): query = self.reload_sentence(query, old2newD) answer = old2newD[int(answer)] i2sD = dict([(i, e2sD[token]) for i, token in enumerate(query) if token in e2sD]) u_Dq, q = self.encode_query(query, i2sD, train=train) eL, sL = zip(*list(e2sD.items())) pre_vL = [self.attention_history(e2dLD[e], q, train=train) for e in eL] v_eDq = self.W_dv(F.concat(pre_vL, axis=0)) answer_idx = eL.index(answer) p = self.predict_answer(u_Dq, v_eDq, [True if token in query else False for token in eL], train=train) + EPS t = chainer.Variable(self.xp.array([answer_idx]).astype(np.int32), volatile=not train) accum_loss_doc += F.softmax_cross_entropy(p, t) p_data = p.data[0, :] max_idx = self.xp.argmax(p_data) TorFs += (max_idx == answer_idx) if max_idx != answer_idx: for sub_ans in [k for k, e in enumerate(eL) if e in query]: p_data[sub_ans] = -10000000 subTorFs += (self.xp.argmax(p_data) == answer_idx) return accum_loss_doc, TorFs, subTorFs
def argmax(self, xs): indices = argsort_list_descent(xs) xs = permutate_list(xs, indices, inv=False) xs = F.transpose_sequence(xs) score, path = F.argmax_crf1d(self.cost, xs) path = F.transpose_sequence(path) path = permutate_list(path, indices, inv=True) score = F.permutate(score, indices, inv=True) return score, path # def argnmax(self, xs, n=10): # cost = cuda.to_cpu(self.cost.data) # xs = permutate_list(xs, argsort_list_descent(xs), inv=False) # xs = [cuda.to_cpu(x.data) for x in xs] # # scores = [] # paths = [] # # for _xs in xs: # alphas = [_xs[0]] # for x in _xs[1:]: # alpha = np.max(alphas[-1] + cost, axis=1) + x # alphas.append(alpha) # # _scores = [] # _paths = [] # _end = len(_xs) - 1 # buf = n # # c = queue.PriorityQueue() # q = queue.PriorityQueue() # x = _xs[_end] # for i in range(x.shape[0]): # q.put((-alphas[_end][i], -x[i], _end, # np.random.random(), np.array([i], np.int32))) # while not q.empty() and c.qsize() < n + buf: # beta, score, time, r, path = q.get() # if time == 0: # c.put((score, r, path)) # continue # t = time - 1 # x = _xs[t] # for i in range(x.shape[0]): # _trans = score - cost[i, path[-1]] # _beta = -alphas[t][i] + _trans # _score = _trans - x[i] # q.put((_beta, _score, t, # np.random.random(), np.append(path, i))) # while not c.empty() and len(_paths) < n: # score, r, path = c.get() # _scores.append(-score) # _paths.append(path[::-1]) # scores.append(_scores) # paths.append(_paths) # # return scores, paths