<commit_before>from unittest import TestCase
import numpy as np

from chainer import cuda, Variable, FunctionSet
from chainer.functions import Linear, softmax_cross_entropy, accuracy
from chainer.optimizers import SGD

cuda.init()

class TestSGD(TestCase):
    UNIT_NUM = 10
    BATCH_SIZE = 32
    EPOCH = 100

    def _make_label(self, x):
        a = (np.dot(x, self.w) + self.b).reshape((TestSGD.BATCH_SIZE, ))
        t = np.empty_like(a).astype(np.int32)
        t[a>=0] = 0
        t[a< 0] = 1
        return t

    def setUp(self):
        self.model = FunctionSet(
            l = Linear(TestSGD.UNIT_NUM, 2)
        )
        self.optimizer = SGD(0.1)
        self.w      = np.random.uniform(-1, 1, (TestSGD.UNIT_NUM, 1)).astype(np.float32)
        self.b      = np.random.uniform(-1, 1, (1, )).astype(np.float32)
        self.x_test = np.random.uniform(-1, 1, (TestSGD.BATCH_SIZE, 10)).astype(np.float32)
        self.t_test = self._make_label(self.x_test)

    def setup_cpu(self):
        self.optimizer.setup(self.model.collect_parameters())

    def setup_gpu(self):
        model.to_gpu()
        optimizer.setup(model.collect_parameters())

    def test_train_linear_classifier(self):
        self.setup_cpu()
        for epoch in xrange(TestSGD.EPOCH):
            x_data = np.random.uniform(-1, 1, (TestSGD.BATCH_SIZE, TestSGD.UNIT_NUM)).astype(np.float32)
            x = Variable(x_data)
            t = Variable(self._make_label(x_data))

            self.optimizer.zero_grads()
            y = self.model.l(x)
            loss = softmax_cross_entropy(y, t)
            loss.backward()
            self.optimizer.update()

        x_test = Variable(self.x_test)
        t_test = Variable(self.t_test)

        y_test = self.model.l(x_test)
        acc = accuracy(y_test, t_test)
        self.assertGreater(acc.data, 0.8)<commit_msg>Support unittest of sgd for gpu<commit_after>from unittest import TestCase
import numpy as np

from chainer import cuda, Variable, FunctionSet
from chainer.functions import Linear, softmax_cross_entropy, accuracy
from chainer.optimizers import SGD

cuda.init()

class TestSGD(TestCase):
    UNIT_NUM = 10
    BATCH_SIZE = 32
    EPOCH = 100

    def _make_dataset(self, batch_size, unit_num, gpu):
        def _make_label(x):
            a = (np.dot(x, self.w) + self.b).reshape((TestSGD.BATCH_SIZE, ))
            t = np.empty_like(a).astype(np.int32)
            t[a>=0] = 0
            t[a< 0] = 1
            return t

        x_data = np.random.uniform(-1, 1, (batch_size, unit_num)).astype(np.float32)
        t_data = _make_label(x_data)
        if gpu:
            x_data = cuda.to_gpu(x_data)
            t_data = cuda.to_gpu(t_data)
        x = Variable(x_data)
        t = Variable(t_data)
        return x, t

    def setUp(self):
        self.model = FunctionSet(
            l = Linear(TestSGD.UNIT_NUM, 2)
        )
        self.optimizer = SGD(0.1)
        self.w      = np.random.uniform(-1, 1, (TestSGD.UNIT_NUM, 1)).astype(np.float32)
        self.b      = np.random.uniform(-1, 1, (1, )).astype(np.float32)

    def check_train_linear_classifier(self, model, optimizer, gpu):
        for epoch in xrange(TestSGD.EPOCH):
            x, t = self._make_dataset(TestSGD.BATCH_SIZE, TestSGD.UNIT_NUM, gpu)
            optimizer.zero_grads()
            y = model.l(x)
            loss = softmax_cross_entropy(y, t)
            loss.backward()
            optimizer.update()

        x_test, t_test = self._make_dataset(TestSGD.BATCH_SIZE, 10, gpu)
        y_test = model.l(x_test)
        acc = accuracy(y_test, t_test)
        self.assertGreater(cuda.to_cpu(acc.data), 0.8)

    def test_train_linear_classifier_cpu(self):
        self.optimizer.setup(self.model.collect_parameters())
        self.check_train_linear_classifier(self.model, self.optimizer, False)

    def test_train_linear_classifier_gpu(self):
        model = self.model
        optimizer = self.optimizer
        model.to_gpu()
        optimizer.setup(model.collect_parameters())
        self.check_train_linear_classifier(model, optimizer, True)
{'fraction_non_alphanumeric': 0.11652386780905753, 'fraction_numerical': 0.01762545899632803, 'mean_word_length': 2.405, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5545148', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1427, 'n_words': 264}