python类softmax_cross_entropy()的实例源码

P4MAllCNNC.py 文件源码 项目:gconv_experiments 作者: tscohen 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def __call__(self, x, t, train=True, finetune=False):

        h = x
        h = F.dropout(h, ratio=0.2, train=train)
        h = self.l1(h, train, finetune)
        h = self.l2(h, train, finetune)
        h = self.l3(h, train, finetune)
        h = F.dropout(h, ratio=0.5, train=train)
        h = self.l4(h, train, finetune)
        h = self.l5(h, train, finetune)
        h = self.l6(h, train, finetune)
        h = F.dropout(h, ratio=0.5, train=train)
        h = self.l7(h, train, finetune)
        h = self.l8(h, train, finetune)
        h = self.l9(h, train, finetune)

        h = F.sum(h, axis=-1)
        h = F.sum(h, axis=-1)
        h = F.sum(h, axis=-1)
        h /= 8 * 8 * 8

        return F.softmax_cross_entropy(h, t), F.accuracy(h, t)
P4AllCNNC.py 文件源码 项目:gconv_experiments 作者: tscohen 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def __call__(self, x, t, train=True, finetune=False):

        h = x
        h = F.dropout(h, ratio=0.2, train=train)
        h = self.l1(h, train, finetune)
        h = self.l2(h, train, finetune)
        h = self.l3(h, train, finetune)
        h = F.dropout(h, ratio=0.5, train=train)
        h = self.l4(h, train, finetune)
        h = self.l5(h, train, finetune)
        h = self.l6(h, train, finetune)
        h = F.dropout(h, ratio=0.5, train=train)
        h = self.l7(h, train, finetune)
        h = self.l8(h, train, finetune)
        h = self.l9(h, train, finetune)

        h = F.sum(h, axis=-1)
        h = F.sum(h, axis=-1)
        h = F.sum(h, axis=-1)
        h /= 8 * 8 * 4

        return F.softmax_cross_entropy(h, t), F.accuracy(h, t)
P4MResNet.py 文件源码 项目:gconv_experiments 作者: tscohen 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def __call__(self, x, t, train=True, finetune=False):

        # First conv layer
        h = self[0](x)

        # Residual blocks
        for i in range(1, len(self) - 2):
            h = self[i](h, train, finetune)

        # BN, relu, pool, final layer
        h = self[-2](h)
        h = F.relu(h)
        n, nc, ns, nx, ny = h.data.shape
        h = F.reshape(h, (n, nc * ns, nx, ny))
        h = F.average_pooling_2d(h, ksize=h.data.shape[2:])
        h = self[-1](h)
        h = F.reshape(h, h.data.shape[:2])

        return F.softmax_cross_entropy(h, t), F.accuracy(h, t)
AllCNNC.py 文件源码 项目:gconv_experiments 作者: tscohen 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def __call__(self, x, t, train=True, finetune=False):

        h = x
        h = F.dropout(h, ratio=0.2, train=train)
        h = self.l1(h, train, finetune)
        h = self.l2(h, train, finetune)
        h = self.l3(h, train, finetune)
        h = F.dropout(h, ratio=0.5, train=train)
        h = self.l4(h, train, finetune)
        h = self.l5(h, train, finetune)
        h = self.l6(h, train, finetune)
        h = F.dropout(h, ratio=0.5, train=train)
        h = self.l7(h, train, finetune)
        h = self.l8(h, train, finetune)
        h = self.l9(h, train, finetune)

        h = F.sum(h, axis=-1)
        h = F.sum(h, axis=-1)
        h /= 8 * 8

        return F.softmax_cross_entropy(h, t), F.accuracy(h, t)
ResNet.py 文件源码 项目:gconv_experiments 作者: tscohen 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def __call__(self, x, t, train=True, finetune=False):

        h = x

        # First conv layer
        h = self[0](h)

        # Residual blocks
        for i in range(1, len(self) - 2):
            h = self[i](h, train, finetune)

        # BN, relu, pool, final layer
        h = self[-2](h)
        h = F.relu(h)
        h = F.average_pooling_2d(h, ksize=h.data.shape[2:])
        h = self[-1](h)
        h = F.reshape(h, h.data.shape[:2])

        return F.softmax_cross_entropy(h, t), F.accuracy(h, t)
Z2CNN.py 文件源码 项目:gconv_experiments 作者: tscohen 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
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)
chainer_model.py 文件源码 项目:biaffineparser 作者: chantera 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def compute_loss(self, y, t):
        arc_logits, label_logits = y
        true_arcs, true_labels = t.T

        b, l1, l2 = arc_logits.shape
        true_arcs = F.pad_sequence(true_arcs, padding=-1)
        if not self.model._cpu:
            true_arcs.to_gpu()
        arc_loss = F.softmax_cross_entropy(
            F.reshape(arc_logits, (b * l1, l2)),
            F.reshape(true_arcs, (b * l1,)),
            ignore_label=-1)

        b, l1, d = label_logits.shape
        true_labels = F.pad_sequence(true_labels, padding=-1)
        if not self.model._cpu:
            true_labels.to_gpu()
        label_loss = F.softmax_cross_entropy(
            F.reshape(label_logits, (b * l1, d)),
            F.reshape(true_labels, (b * l1,)),
            ignore_label=-1)

        loss = arc_loss + label_loss
        return loss
lstm_tagger.py 文件源码 项目:depccg 作者: masashi-y 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def __call__(self, xs):
        """
        xs [(w,s,p,y), ..., ]
        w: word, s: suffix, p: prefix, y: label
        """
        batchsize = len(xs)
        ws, ss, ps, ts = zip(*xs)
        ys = self.forward(ws, ss, ps)
        loss = reduce(lambda x, y: x + y,
            [F.softmax_cross_entropy(y, t) for y, t in zip(ys, ts)])

        acc = reduce(lambda x, y: x + y,
            [F.accuracy(y, t, ignore_label=IGNORE) for y, t in zip(ys, ts)])

        acc /= batchsize
        chainer.report({
            "loss": loss,
            "accuracy": acc
            }, self)
        return loss
ja_lstm_parser.py 文件源码 项目:depccg 作者: masashi-y 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def __call__(self, xs):
        batchsize = len(xs)
        ws, cs, ls, cat_ts, dep_ts = zip(*xs)
        cat_ys, dep_ys = self.forward(ws, cs, ls)

        cat_loss = reduce(lambda x, y: x + y,
            [F.softmax_cross_entropy(y, t) for y, t in zip(cat_ys, cat_ts)])
        cat_acc = reduce(lambda x, y: x + y,
            [F.accuracy(y, t, ignore_label=IGNORE) for y, t in zip(cat_ys, cat_ts)])

        dep_loss = reduce(lambda x, y: x + y,
            [F.softmax_cross_entropy(y, t) for y, t in zip(dep_ys, dep_ts)])
        dep_acc = reduce(lambda x, y: x + y,
            [F.accuracy(y, t, ignore_label=IGNORE) for y, t in zip(dep_ys, dep_ts)])

        cat_acc /= batchsize
        dep_acc /= batchsize
        chainer.report({
            "tagging_loss": cat_loss,
            "tagging_accuracy": cat_acc,
            "parsing_loss": dep_loss,
            "parsing_accuracy": dep_acc
            }, self)
        return cat_loss + dep_loss
lstm_tagger_old.py 文件源码 项目:depccg 作者: masashi-y 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __call__(self, xs):
        """
        xs [(w,s,p,y), ..., ]
        w: word, s: suffix, p: prefix, y: label
        """
        batchsize = len(xs)
        ws, ss, ps, ts = zip(*xs)
        ys = self.forward(ws, ss, ps)
        loss = reduce(lambda x, y: x + y,
            [F.softmax_cross_entropy(y, t) for y, t in zip(ys, ts)])

        acc = reduce(lambda x, y: x + y,
            [F.accuracy(y, t, ignore_label=IGNORE) for y, t in zip(ys, ts)])

        acc /= batchsize
        chainer.report({
            "loss": loss,
            "accuracy": acc
            }, self)
        return loss
tagger.py 文件源码 项目:depccg 作者: masashi-y 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def __call__(self, xs, ts):
        """
        Inputs:
            xs (tuple(Variable, Variable, Variable)):
                each of Variables is of dim (batchsize,)
            ts Variable:
                (batchsize)
        """
        words, suffixes, caps = xs[:,:7], xs[:, 7:14], xs[:, 14:]
        h_w = self.emb_word(words)
        h_c = self.emb_caps(caps)
        h_s = self.emb_suffix(suffixes)
        h = F.concat([h_w, h_c, h_s], 2)
        batchsize, ntokens, hidden = h.data.shape
        h = F.reshape(h, (batchsize, ntokens * hidden))
        ys = self.linear(h)

        loss = F.softmax_cross_entropy(ys, ts)
        acc = F.accuracy(ys, ts)

        chainer.report({
            "loss": loss,
            "accuracy": acc
            }, self)
        return loss
lstm_tagger_ph.py 文件源码 项目:depccg 作者: masashi-y 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def __call__(self, ws, ss, ps, ts):
        """
        xs [(w,s,p,y), ..., ]
        w: word, s: suffix, p: prefix, y: label
        """
        batchsize, length = ts.shape
        ys = self.forward(ws, ss, ps)[1:-1]
        ts = [F.squeeze(x, 0) for x in F.split_axis(F.transpose(ts), length, 0)]
        loss = reduce(lambda x, y: x + y,
            [F.softmax_cross_entropy(y, t) for y, t in zip(ys, ts)])

        acc = reduce(lambda x, y: x + y,
            [F.accuracy(y, t, ignore_label=IGNORE) for y, t in zip(ys, ts)])

        acc /= length
        chainer.report({
            "loss": loss,
            "accuracy": acc
            }, self)
        return loss
mdl_rgb_d.py 文件源码 项目:MultimodalDL 作者: masataka46 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def __call__(self, x, t):
        self.clear()
        h = F.max_pooling_2d(F.relu(
            F.local_response_normalization(self.conv1(x))), 3, stride=2)
        h = F.max_pooling_2d(F.relu(
            F.local_response_normalization(self.conv2(h))), 3, stride=2)
        h = F.relu(self.conv3(h))
        h = F.relu(self.conv4(h))
        h = F.max_pooling_2d(F.relu(self.conv5(h)), 3, stride=2)
        h = F.dropout(F.relu(self.fc6(h)), train=self.train)
        h = F.dropout(F.relu(self.fc7(h)), train=self.train)
        h = self.fc8(h)

        self.loss = F.softmax_cross_entropy(h, t)
        self.accuracy = F.accuracy(h, t)
        return self.loss
alexbn.py 文件源码 项目:chainer-deconv 作者: germanRos 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def __call__(self, x, t):
        self.clear()
        h = self.bn1(self.conv1(x), test=not self.train)
        h = F.max_pooling_2d(F.relu(h), 3, stride=2)
        h = self.bn2(self.conv2(h), test=not self.train)
        h = F.max_pooling_2d(F.relu(h), 3, stride=2)
        h = F.relu(self.conv3(h))
        h = F.relu(self.conv4(h))
        h = F.max_pooling_2d(F.relu(self.conv5(h)), 3, stride=2)
        h = F.dropout(F.relu(self.fc6(h)), train=self.train)
        h = F.dropout(F.relu(self.fc7(h)), train=self.train)
        h = self.fc8(h)

        self.loss = F.softmax_cross_entropy(h, t)
        self.accuracy = F.accuracy(h, t)
        return self.loss
seq2seq.py 文件源码 项目:TOHO_AI 作者: re53min 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def decode(self, sentences):
        # sentences = Variable(np.array([sentences], dtype=np.int32), volatile=False)
        loss = Variable(np.zeros((), dtype=np.float32))
        n_words = len(sentences)-1

        for word, t in zip(sentences, sentences[1:]):
            # print('??:{}, ??:{}'.format(word,t))
            word = Variable(np.array([[word]], dtype=np.int32))
            t = Variable(np.array([t], dtype=np.int32))
            decode0 = self.output_embed(word)
            decode1 = self.decode1(decode0)
            decode2 = self.decode2(decode1)
            z = self.output(decode2)

            loss += F.softmax_cross_entropy(z, t)

        return loss, n_words
base_network.py 文件源码 项目:ImageCaptioning 作者: rkuga 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def step(self,perm,batch_index, mode, epoch): 
            if mode =='train':
                data, label=self.read_batch(perm,batch_index,self.train_data)
            else:
                data, label=self.read_batch(perm,batch_index,self.test_data)

            data = Variable(cuda.to_gpu(data))
            yl = self.network(data)

            label=Variable(cuda.to_gpu(label))

            L_network = F.softmax_cross_entropy(yl, label)
            A_network = F.accuracy(yl, label)

            if mode=='train':
                self.o_network.zero_grads()
                L_network.backward()
                self.o_network.update()


            return {"prediction": yl.data.get(),
                    "current_loss": L_network.data.get(),
                    "current_accuracy": A_network.data.get(),
            }
segmentation.py 文件源码 项目:rnn-morpheme-analyzer 作者: mitaki28 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def loss(model, xs, ts, uss=None):
    model.reset_state()
    tags = model([Variable(
        np.array([x], dtype=np.int32)
    ) for x in xs])
    zss = []
    d = Variable(np.array(0, dtype=np.float32))
    for t, (y, zs) in zip(ts, tags):
        d += cf.sigmoid_cross_entropy(
            y, Variable(np.array([[t]], dtype=np.int32))
        )
        if t:
            zss.append(zs)
    if uss:
        assert len(uss) == len(zss)
        for us, zs in zip(uss, zss):
            for u, z in zip(us, zs):
                d += cf.softmax_cross_entropy(
                    z, Variable(np.array([u], dtype=np.int32))
                )
    return d
analyzer.py 文件源码 项目:rnn-morpheme-analyzer 作者: mitaki28 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def loss(model, xs, ts, uss=None):
    model.reset_state()
    tags = model([Variable(
        np.array([x], dtype=np.int32)
    ) for x in xs])
    zss = []
    d = Variable(np.array(0, dtype=np.float32))
    for t, (y, zs) in zip(ts, tags):
        d += cf.sigmoid_cross_entropy(
            y, Variable(np.array([[t]], dtype=np.int32))
        )
        if t:
            zss.append(zs)
    if uss:
        assert len(uss) == len(zss)
        for us, zs in zip(uss, zss):
            for u, z in zip(us, zs):
                d += cf.softmax_cross_entropy(
                    z, Variable(np.array([u], dtype=np.int32))
                )
    return d
pixelwise_softmax_classifier.py 文件源码 项目:chainercv 作者: chainer 项目源码 文件源码 阅读 164 收藏 0 点赞 0 评论 0
def __call__(self, x, t):
        """Computes the loss value for an image and label pair.

        Args:
            x (~chainer.Variable): A variable with a batch of images.
            t (~chainer.Variable): A variable with the ground truth
                image-wise label.

        Returns:
            ~chainer.Variable: Loss value.

        """
        self.y = self.predictor(x)
        self.loss = F.softmax_cross_entropy(
            self.y, t, class_weight=self.class_weight,
            ignore_label=self.ignore_label)

        reporter.report({'loss': self.loss}, self)
        return self.loss
encoder_decoder.py 文件源码 项目:workspace 作者: nojima 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def __call__(self, xs: List[Variable], ys: List[Variable]) -> Variable:
        batch_size = len(xs)
        xs = [x[::-1] for x in xs]

        eos = np.array([EOS], dtype=np.int32)
        ys_in = [F.concat((eos, y), axis=0) for y in ys]
        ys_out = [F.concat((y, eos), axis=0) for y in ys]

        embedded_xs = [self._embed_input(x) for x in xs]
        embedded_ys = [self._embed_output(y) for y in ys_in]

        hidden_states, cell_states, attentions = self._encoder(None, None, embedded_xs)
        _, _, embedded_outputs = self._decoder(hidden_states, cell_states, embedded_ys)

        loss = 0
        for embedded_output, y, attention in zip(embedded_outputs, ys_out, attentions):
            if self._use_attention:
                output = self._calculate_attention_layer_output(embedded_output, attention)
            else:
                output = self._extract_output(embedded_output)
            loss += F.softmax_cross_entropy(output, y)
        loss /= batch_size

        return loss
updater.py 文件源码 项目:Semantic-Segmentation-using-Adversarial-Networks 作者: oyam 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def _get_loss_gen(self):
        batchsize = self.y_fake.data.shape[0]
        L_mce = F.softmax_cross_entropy(self.pred_label_map, self.ground_truth, normalize=False)
        L_bce = F.softmax_cross_entropy(self.y_fake, Variable(self.xp.ones(batchsize, dtype=self.xp.int32), volatile=not self.gen.train))
        loss = L_mce + self.L_bce_weight * L_bce

        # log report
        label_true = chainer.cuda.to_cpu(self.ground_truth.data)
        label_pred = chainer.cuda.to_cpu(self.pred_label_map.data).argmax(axis=1)
        logs = []
        for i in six.moves.range(batchsize):
            acc, acc_cls, iu, fwavacc = utils.label_accuracy_score(
                label_true[i], label_pred[i], self.n_class)
            logs.append((acc, acc_cls, iu, fwavacc))
        log = np.array(logs).mean(axis=0)
        values = {
            'loss': loss,
            'accuracy': log[0],
            'accuracy_cls': log[1],
            'iu': log[2],
            'fwavacc': log[3],
        }
        chainer.report(values, self.gen)

        return loss
updater.py 文件源码 项目:Semantic-Segmentation-using-Adversarial-Networks 作者: oyam 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def calc_loss(self):
        batchsize = self.ground_truth.shape[0]
        self.loss = F.softmax_cross_entropy(self.pred_label_map, self.ground_truth, normalize=False)

        # log report
        label_true = chainer.cuda.to_cpu(self.ground_truth.data)
        label_pred = chainer.cuda.to_cpu(self.pred_label_map.data).argmax(axis=1)
        logs = []
        for i in six.moves.range(batchsize):
            acc, acc_cls, iu, fwavacc = utils.label_accuracy_score(
                label_true[i], label_pred[i], self.n_class)
            logs.append((acc, acc_cls, iu, fwavacc))
        log = np.array(logs).mean(axis=0)
        values = {
            'loss': self.loss,
            'accuracy': log[0],
            'accuracy_cls': log[1],
            'iu': log[2],
            'fwavacc': log[3],
        }
        chainer.report(values, self.model)
wavenet.py 文件源码 项目:wavenet 作者: musyoku 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def cross_entropy(self, raw_network_output, target_signal_data):
        if isinstance(target_signal_data, Variable):
            raise Exception("target_signal_data cannot be Variable")

        raw_network_output = self.to_variable(raw_network_output)
        target_width = target_signal_data.shape[1]
        batchsize = raw_network_output.data.shape[0]

        if raw_network_output.data.shape[3] != target_width:
            raise Exception("raw_network_output.width != target.width")

        # (batchsize * time_step,) <- (batchsize, time_step)
        target_signal_data = target_signal_data.reshape((-1,))
        target_signal = self.to_variable(target_signal_data)

        # (batchsize * time_step, channels) <- (batchsize, channels, 1, time_step)
        raw_network_output = F.transpose(raw_network_output, (0, 3, 2, 1))
        raw_network_output = F.reshape(raw_network_output, (batchsize * target_width, -1))

        loss = F.softmax_cross_entropy(raw_network_output, target_signal)
        return loss
train.py 文件源码 项目:chainer-image-caption 作者: dsanno 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def forward(net, image_batch, sentence_batch, train=True):
    images = xp.asarray(image_batch)
    n, sentence_length = sentence_batch.shape
    net.initialize(images)
    loss = 0
    acc = 0
    size = 0
    for i in range(sentence_length - 1):
        target = xp.where(xp.asarray(sentence_batch[:, i]) != eos, 1, 0).astype(np.float32)
        if (target == 0).all():
            break
        with chainer.using_config('train', train):
            with chainer.using_config('enable_backprop', train):
                x = xp.asarray(sentence_batch[:, i])
                t = xp.asarray(sentence_batch[:, i + 1])
                y = net(x)
                y_max_index = xp.argmax(y.data, axis=1)
                mask = target.reshape((len(target), 1)).repeat(y.data.shape[1], axis=1)
                y = y * mask
                loss += F.softmax_cross_entropy(y, t)
                acc += xp.sum((y_max_index == t) * target)
                size += xp.sum(target)
    return loss / size, float(acc) / size, float(size)
ewc_mnist.py 文件源码 项目:chainer-EWC 作者: okdshin 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __call__(self, *args):
        x = args[:-1]
        t = args[-1]
        self.y = None
        self.loss = None
        self.accuracy = None
        self.y = self.predictor(*x)
        self.loss = F.softmax_cross_entropy(self.y, t)

        if self.stored_variable_list is not None and \
                self.fisher_list is not None:  # i.e. Stored
            for i in range(len(self.variable_list)):
                self.loss += self.lam/2. * F.sum(
                        self.fisher_list[i] *
                        F.square(self.variable_list[i][1] -
                                 self.stored_variable_list[i]))
        reporter.report({'loss': self.loss}, self)
        if self.compute_accuracy:
            self.accuracy = F.accuracy(self.y, t)
            reporter.report({'accuracy': self.accuracy}, self)
        return self.loss
seq2seq_model.py 文件源码 项目:DSTC6-End-to-End-Conversation-Modeling 作者: dialogtekgeek 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def loss(self,es,x,y,t):
        """ Forward propagation and loss calculation
            Args:
                es (pair of ~chainer.Variable): encoder state 
                x (list of ~chainer.Variable): list of input sequences
                y (list of ~chainer.Variable): list of output sequences
                t (list of ~chainer.Variable): list of target sequences
                                   if t is None, it returns only states
            Return:
                es (pair of ~chainer.Variable(s)): encoder state
                ds (pair of ~chainer.Variable(s)): decoder state
                loss (~chainer.Variable) : cross-entropy loss
        """
        es,ey = self.encoder(es,x)
        ds,dy = self.decoder(es,y)
        if t is not None:
            loss = F.softmax_cross_entropy(dy,t)
            # avoid NaN gradients (See: https://github.com/pfnet/chainer/issues/2505)
            if chainer.config.train:
                loss += F.sum(F.concat(ey, axis=0)) * 0
            return es, ds, loss
        else: # if target is None, it only returns states
            return es, ds
seq2seq_model.py 文件源码 项目:DSTC6-End-to-End-Conversation-Modeling 作者: dialogtekgeek 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def loss(self,es,x,y,t):
        """ Forward propagation and loss calculation
            Args:
                es (pair of ~chainer.Variable): encoder state 
                x (list of ~chainer.Variable): list of input sequences
                y (list of ~chainer.Variable): list of output sequences
                t (list of ~chainer.Variable): list of target sequences
                                   if t is None, it returns only states
            Return:
                es (pair of ~chainer.Variable(s)): encoder state
                ds (pair of ~chainer.Variable(s)): decoder state
                loss (~chainer.Variable) : cross-entropy loss
        """
        es,ey = self.encoder(es,x)
        ds,dy = self.decoder(es,y)
        if t is not None:
            loss = F.softmax_cross_entropy(dy,t)
            # avoid NaN gradients (See: https://github.com/pfnet/chainer/issues/2505)
            if chainer.config.train:
                loss += F.sum(F.concat(ey, axis=0)) * 0
            return es, ds, loss
        else: # if target is None, it only returns states
            return es, ds
seq2seq_model.py 文件源码 项目:DSTC6-End-to-End-Conversation-Modeling 作者: dialogtekgeek 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def loss(self,es,x,y,t):
        """ Forward propagation and loss calculation
            Args:
                es (pair of ~chainer.Variable): encoder state 
                x (list of ~chainer.Variable): list of input sequences
                y (list of ~chainer.Variable): list of output sequences
                t (list of ~chainer.Variable): list of target sequences
                                   if t is None, it returns only states
            Return:
                es (pair of ~chainer.Variable(s)): encoder state
                ds (pair of ~chainer.Variable(s)): decoder state
                loss (~chainer.Variable) : cross-entropy loss
        """
        es,ey = self.encoder(es,x)
        ds,dy = self.decoder(es,y)
        if t is not None:
            loss = F.softmax_cross_entropy(dy,t)
            # avoid NaN gradients (See: https://github.com/pfnet/chainer/issues/2505)
            if chainer.config.train:
                loss += F.sum(F.concat(ey, axis=0)) * 0
            return es, ds, loss
        else: # if target is None, it only returns states
            return es, ds
seq2seq_model.py 文件源码 项目:DSTC6-End-to-End-Conversation-Modeling 作者: dialogtekgeek 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def loss(self,es,x,y,t):
        """ Forward propagation and loss calculation
            Args:
                es (pair of ~chainer.Variable): encoder state 
                x (list of ~chainer.Variable): list of input sequences
                y (list of ~chainer.Variable): list of output sequences
                t (list of ~chainer.Variable): list of target sequences
                                   if t is None, it returns only states
            Return:
                es (pair of ~chainer.Variable(s)): encoder state
                ds (pair of ~chainer.Variable(s)): decoder state
                loss (~chainer.Variable) : cross-entropy loss
        """
        es,ey = self.encoder(es,x)
        ds,dy = self.decoder(es,y)
        if t is not None:
            loss = F.softmax_cross_entropy(dy,t)
            # avoid NaN gradients (See: https://github.com/pfnet/chainer/issues/2505)
            if chainer.config.train:
                loss += F.sum(F.concat(ey, axis=0)) * 0
            return es, ds, loss
        else: # if target is None, it only returns states
            return es, ds
ResNet50.py 文件源码 项目:chainer-caption 作者: apple2373 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def __call__(self, x, t):
        self.clear()
        h = self.bn1(self.conv1(x), test=not self.train)
        h = F.max_pooling_2d(F.relu(h), 3, stride=2)
        h = self.res2(h, self.train)
        h = self.res3(h, self.train)
        h = self.res4(h, self.train)
        h = self.res5(h, self.train)
        h = F.average_pooling_2d(h, 7, stride=1)
        if t=="feature":
            return h
        h = self.fc(h)

        if self.train:
            self.loss = F.softmax_cross_entropy(h, t)
            self.accuracy = F.accuracy(h, t)
            return self.loss
        else:
            return h


问题


面经


文章

微信
公众号

扫码关注公众号