def train(self, data, target, batch_size=100, epoch=5, test_size=0.3, report_interval_epoch=1):
        '''
        トレーニングをするメソッド
        '''
        dp = DataProcessor()
        dp.set_normalization_params(data)
        self.resource.save_normalization_params(dp.means, dp.stds)
        _data = dp.format_x(data)
        _target = dp.format_y(target)
        # データをトレーニング用とテスト用に分割
        train_x, test_x, train_y, test_y = train_test_split(_data, _target, test_size=test_size)

        optimizer = chainer.optimizers.Adam()
        optimizer.use_cleargrads()
        optimizer.setup(self.model)
        loss = lambda pred, teacher: softmax_cross_entropy.softmax_cross_entropy(pred, teacher)
        for x_batch, y_batch, epoch_end in dp.batch_iter(train_x, train_y, batch_size, epoch):
            predicted = self.model(x_batch)
            optimizer.update(loss, predicted, y_batch)
            if epoch_end:
                train_acc = accuracy.accuracy(predicted, y_batch)
                predicted_to_test = self.model(test_x)
                test_acc = accuracy.accuracy(predicted_to_test, test_y)
                print("train accuracy={}, test accuracy={}".format(train_acc.data, test_acc.data))
                self.resource.save_model(self.model)
Пример #2
0
    def __call__(self, *args):
        """Computes the loss value for an input and label pair.

        It also computes accuracy and stores it to the attribute.

        Args:
            args (list of ~chainer.Variable): Input minibatch.

        The all elements of ``args`` but last one are features and
        the last element corresponds to ground truth labels.
        It feeds features to the predictor and compare the result
        with ground truth labels.

        Returns:
            ~chainer.Variable: Loss value.

        """

        assert len(args) >= 2
        x = args[:-1]
        t = args[-1]
        self.y = None
        self.loss = None
        self.accuracy = None
        self.y = self.predictor(*x)
        self.loss = self.lossfun(self.y, t)
        if self.compute_accuracy:
            self.accuracy = accuracy.accuracy(self.y, t)
        return self.loss
    def update(self, output):
        y_pred, y = chainer_converter(output)

        correct = accuracy(y_pred, y)

        self._num_correct += correct.item() * y.shape[0]
        self._num_examples += y.shape[0]
    def __call__(self, x, *args):
        # x.shape = (n_batch, n_channels, h*w)
        dropout_ratio = 0.5

        h = x
        # Graph convolutional layers
        for f, p in self.graph_layers:
            h = p(F.relu(f(h)))

        # Fully connected layers
        for f in self.linear_layers:
            h = F.relu(F.dropout(f(h), dropout_ratio))

        # Linear classification layer
        h = self.cls_layer(h)

        if args:
            labels = args[0]
            loss = F.softmax_cross_entropy(h, labels)
            acc = accuracy.accuracy(h, labels)
            reporter.report({
                'loss': loss,
                'accuracy': acc},
                self)

            return loss

        return h
Пример #5
0
def test_pretrained_on_target(source_cnn, target, args):
    print(":: testing pretrained source CNN on target domain")

    if args.device >= 0:
        source_cnn.to_gpu()

    with chainer.using_config('train', False):
        _, target_test_iterator = data2iterator(target,
                                                args.batchsize,
                                                multiprocess=False)

        mean_accuracy = 0.0
        n_batches = 0

        for batch in target_test_iterator:
            batch, labels = chainer.dataset.concat_examples(batch,
                                                            device=args.device)
            encode = source_cnn.encoder(batch)
            classify = source_cnn.classifier(encode)
            acc = accuracy.accuracy(classify, labels)
            mean_accuracy += acc.data
            n_batches += 1
        mean_accuracy /= n_batches

        print(
            ":: classifier trained on only source, evaluated on target: accuracy {}%"
            .format(mean_accuracy))
Пример #6
0
    def __call__(self, *args):
        """Computes the loss value for an input and label pair.

        It also computes accuracy and stores it to the attribute.

        Args:
            args (list of ~chainer.Variable): Input minibatch.

        The all elements of ``args`` but last one are features and
        the last element corresponds to ground truth labels.
        It feeds features to the predictor and compare the result
        with ground truth labels.

        Returns:
            ~chainer.Variable: Loss value.

        """

        assert len(args) >= 2
        x = args[:-1]
        t = args[-1]
        self.y = None
        self.loss = None
        self.accuracy = None
        self.y = self.predictor(*x)
        self.loss = self.lossfun(self.y, t)
        if self.compute_accuracy:
            self.accuracy = accuracy.accuracy(self.y, t)
        return self.loss
Пример #7
0
 def __call__(self, x, t):
     self.clear()
     y = self.forward(x)
     self.loss = F.softmax_cross_entropy(y, t)
     reporter.report({'loss': self.loss}, self)
     self.accuracy = accuracy.accuracy(y, t)
     reporter.report({'accuracy': self.accuracy}, self)
     return self.loss
Пример #8
0
 def __call__(self,x1,x2,t):
     x1 = chainer.Variable(x1)
     x2 = chainer.Variable(x2)
     t  = chainer.Variable(t)
     y = self.predictor(x1,x2)
     loss = F.softmax_cross_entropy(y,t)
     acc = accuracy.accuracy(y,t)
     reporter.report({"accuracy": acc,"loss":loss}, self)
     return loss
Пример #9
0
    def __call__(self, x, t):
        encode = self.encoder(x)
        classify = self.classifier(encode)

        self.accuracy = accuracy.accuracy(classify, t)
        self.loss = F.softmax_cross_entropy(classify, t)

        reporter.report({"accuracy": self.accuracy, "loss": self.loss}, self)
        return self.loss
Пример #10
0
 def __call__(self, x, t, context=None):
     self.y = None
     self.loss = None
     self.accuracy = True
     self.y = self.predictor(x, context)
     self.loss = self.lossfun(self.y, t)
     if self.compute_accuracy:
         self.accuracy = accuracy.accuracy(self.y, t)
     return self.loss
Пример #11
0
 def __call__(self, x, t):
     self.clear()
     h = self.forward(x)
     #print h.data
     #print t.data
     to = pick_up_t(t.data)
     self.loss = F.softmax_cross_entropy(h, to)
     reporter.report({'loss': self.loss}, self)
     #self.loss = F.mean_squared_error(h, t)
     #print "Loss : " +str(self.loss.data)
     self.accuracy = accuracy.accuracy(h, to)
     reporter.report({'accuracy': self.accuracy}, self)
     return self.loss
Пример #12
0
 def autoencoder(self, x, t, j):
     h = x
     hb = None
     idx = 0
     for idx in range(j):
         if idx == j - 1 and self.pretrain:
             h = Variable(xp.asarray(h.data, dtype=xp.float32))
         hb = h
         if idx == self.size - 1: break
         h = F.sigmoid(self.__getitem__("l{0}".format(idx + 1))(h))
     if self.pretrain:
         d = F.sigmoid(self.__getitem__("b{0}".format(idx + 1))(h))
         self.loss = F.mean_squared_error(hb, d)
     else:
         self.y = self.__getitem__("l{0}".format(idx + 1))(h)
         self.accuracy = accuracy.accuracy(self.y, t)
         self.loss = F.softmax_cross_entropy(self.y, t)
     return self.loss
Пример #13
0
    def __call__(self, x, t):
        """Computes the loss value for an input and label pair.

        It also computes accuracy and stores it to the attribute.

        Args:
            x (~chainer.Variable): Input minibatch.
            t (~chainer.Variable): Corresponding groundtruth labels.

        Returns:
            ~chainer.Variable: Loss value.

        """
        self.y = self.predictor(x)
        self.loss = self.lossfun(self.y, t)
        if self.compute_accuracy:
            self.accuracy = accuracy.accuracy(self.y, t)
        return self.loss
Пример #14
0
    def __call__(self, x, t):
        """Computes the loss value for an input and label pair.

        It also computes accuracy and stores it to the attribute.

        Args:
            x (~chainer.Variable): Input minibatch.
            t (~chainer.Variable): Corresponding groundtruth labels.

        Returns:
            ~chainer.Variable: Loss value.

        """
        self.y = self.predictor(x)
        self.loss = self.lossfun(self.y, t)
        if self.compute_accuracy:
            self.accuracy = accuracy.accuracy(self.y, t)
        return self.loss
def aca(evaluator, device=None):
    iterator = evaluator.get_iterator()
    target = evaluator.get_target()

    if hasattr(iterator, 'reset'):
        iterator.reset()
        it = iterator
    else:
        it = copy.copy(iterator)

    acc_lst = []
    for batch in it:
        in_arrays = convert.concat_examples(batch, device)
        if isinstance(in_arrays, tuple):
            in_vars = tuple(
                variable.Variable(x, volatile='on') for x in in_arrays)
            y = target(in_vars[0]).data
            t = in_vars[1].data
            acc_lst.append(cuda.to_cpu(accuracy.accuracy(y, t).data))

    return np.mean(acc_lst)
Пример #16
0
    def forward(self, x, t):
        xp = cuda.get_array_module(x)
        y = self.predictor(x)
        log_softmax = F.log_softmax(y)
        # SelectItem is not supported by onnx-chainer.
        # TODO(hamaji): Support it?
        # log_prob = F.select_item(log_softmax, t)

        batch_size = chainer.Variable(xp.array(t.size, xp.float32),
                                      name='batch_size')
        self.extra_inputs = [batch_size]
        # TODO(hamaji): Currently, F.sum with axis=1 cannot be
        # backpropped properly.
        # log_prob = F.sum(log_softmax * t, axis=1)
        # return -F.sum(log_prob, axis=0) / self.batch_size
        log_prob = F.sum(log_softmax * t, axis=(0, 1))
        loss = -log_prob / batch_size
        reporter.report({'loss': loss}, self)
        if self.compute_accuracy:
            acc = accuracy.accuracy(y, xp.argmax(t, axis=1))
            reporter.report({'accuracy': acc}, self)
        loss.name = 'loss'
        return loss
Пример #17
0
 def calc_accuracy(self, y, t):
     return accuracy.accuracy(y, t)
Пример #18
0
 def __call__(self, x, t):
     reporter_module.report({
         'accuracy': accuracy.accuracy(x, t),
     })
Пример #19
0
 def __call__(self, x, t):
     y = self.predictor(x)
     loss = F.softmax_cross_entropy(x, t)
     acc = accuracy.accuracy(x, t)
     reporter.report({"accuracy": acc, "loss": loss}, self)
     return loss