def seq_rnn_embed(exs, birnn, init_state=None, return_sequences: bool = False): """Embed given sequences using rnn.""" # exs.shape == (..., S, E) seqs = F.reshape(exs, (-1, ) + exs.shape[-2:]) # (X, S, E) toembed = F.separate(seqs, 0) # X x [(S1, E), (S2, E), ...] hs, ys = birnn(init_state, toembed) # (2, X, E), X x [(S1, 2*E), (S2, 2*E), ...] if return_sequences: ys = F.stack(ys) # (X, S, 2*E) ys = F.reshape(ys, exs.shape[:-1] + (-1, )) # (..., S, 2*E) return ys hs = F.moveaxis(hs, 0, -2) # (X, 2, E) hs = F.reshape(hs, exs.shape[:-2] + (-1, )) # (..., 2*E) return hs
def differentiate(self, x, enable_double_backprop): """Calculate derivative of the output data w.r.t. input data. Args: x (~chainer.Variable): Input data which has the shape ``(n_sample, n_input)``. enable_double_backprop (bool): Passed to :func:`chainer.grad` to determine whether to create more deep calculation graph or not. """ dy = [chainer.grad([output_node], [x], enable_double_backprop=enable_double_backprop)[0] for output_node in F.moveaxis(self.results['y'], 0, -1)] dy = F.stack(dy, axis=1) self.results['dy'] = dy
def wsd_with_tc(self, sent, trf_encoded_matrix, labels): ### WSD ### if self.model_type == "TRF-Multi" or self.model_type == "TRF-Delay-Multi": y_wsd = self.wsd_only(trf_encoded_matrix, labels) elif self.model_type == "TRF-Sequential": y_wsd, task_type = self.wsd_model(sent, None, None, True) ## 読み込みsequential y_wsd_soft = F.softmax(y_wsd) ## 予測結果にSoftmaxをかける argmax_wsd = F.argmax(y_wsd_soft, axis=1) ## 最大のインデクス値を取ってくる cond = chainer.Variable( self.xp.array([ True if i != "<PAD>" else False for i in list(chain(*labels)) ])) ## 語義のラベルがついていない単語は無視するための条件 pad_array = chainer.Variable( -1 * self.xp.ones(argmax_wsd.shape, dtype=argmax_wsd.dtype)) pad_array_argmax_wsd = F.where(cond, argmax_wsd, pad_array) sense_label_embed = F.embed_id(x=pad_array_argmax_wsd, W=self.xp.array( self.lookup_table_sense_fixed), ignore_label=-1) ## 固定. sense_label_embed = sense_label_embed.reshape( trf_encoded_matrix.shape[0], trf_encoded_matrix.shape[-1], -1) origin_shape = sense_label_embed.shape sense_label_embed = F.moveaxis(sense_label_embed, 1, 2) ## 置き換え ## cond_reshape = cond.reshape(cond.shape[0], -1) cond_reshape = F.broadcast_to( cond_reshape, (cond_reshape.shape[0], trf_encoded_matrix.shape[1])) cond_reshape = cond_reshape.reshape(origin_shape) cond_reshape = F.swapaxes(cond_reshape, 1, 2) replaced_trf_matrix = F.where(cond_reshape, sense_label_embed, trf_encoded_matrix) ### WSDの予測をTCに組み入れる ### tc = replaced_trf_matrix ## 置換後の文書行列 ### TC ### tc_features = F.sum(tc, axis=2) ## TC特徴 y_tc = self.fc2(tc_features) ### TCの予測結果 return (y_tc, y_wsd) if (self.model_type == "TRF-Multi") or ( self.model_type == "TRF-Delay-Multi") else y_tc
def forward(self, inputs, device): x, = inputs y = functions.moveaxis(x, self.source, self.destination) return y,
def check_type_error(self, x): with self.assertRaises(TypeError): functions.moveaxis(x, self.source, self.destination)
def check_type_error(self, x): with self.assertRaises(type_check.InvalidType): functions.moveaxis(x, self.source, self.destination)
def epahser_n_sixaevom(_T_T, flags, dims): permutation = flatten([[flag.nonzero()[0].tolist()] for flag in flags]) T_T = F.reshape(_T_T, dims + [1] * (max(permutation) - len(dims) + 1)) return F.moveaxis(T_T, range(len(permutation)), permutation)
def moveaxis_n_reshape(T, flags): permutation = flatten([[flag.nonzero()[0].tolist()] for flag in flags]) T_T = F.moveaxis(T, permutation, range(len(permutation))) new_shape = [merged_dim(T.shape, flag) for flag in flags] return F.reshape(T_T, new_shape)
def f(x): y = functions.moveaxis(x, self.source, self.destination) return y * y
def check_forward(self, x_data): x = chainer.Variable(x_data) y = functions.moveaxis(x, self.source, self.destination) expect = numpy.moveaxis(self.x, self.source, self.destination) testing.assert_allclose(y.data, expect)
def check_type_error(self, x): with self.assertRaises(ValueError): functions.moveaxis(x, self.source, self.destination)
def f(x): return functions.moveaxis(x, self.source, self.destination)
def check_forward(self, x_data): x = chainer.Variable(x_data) y = functions.moveaxis(x, self.source, self.destination) expect = _moveaxis(self.x, self.source, self.destination) testing.assert_allclose(y.data, expect)