def test_double_backward_gpu(self):
     self.check_double_backward(
         cuda.to_gpu(self.x1),
         cuda.to_gpu(self.x2),
         cuda.to_gpu(self.gy),
         cuda.to_gpu(self.ggx1),
         cuda.to_gpu(self.ggx2))
    def forward(self, state, action, Reward, state_dash, episode_end):
        num_of_batch = state.shape[0]
        s = Variable(state)
        s_dash = Variable(state_dash)

        Q = self.Q_func(s)  # Get Q-value
        # Generate Target Signals
        tmp2 = self.Q_func(s_dash)
        tmp2 = list(map(np.argmax, tmp2.data.get()))  # argmaxQ(s',a)
        tmp = self.Q_func_target(s_dash)  # Q'(s',*)
        tmp = list(tmp.data.get())
        # select Q'(s',*) due to argmaxQ(s',a)
        res1 = []
        for i in range(num_of_batch):
            res1.append(tmp[i][tmp2[i]])

        #max_Q_dash = np.asanyarray(tmp, dtype=np.float32)
        max_Q_dash = np.asanyarray(res1, dtype=np.float32)
        target = np.asanyarray(Q.data.get(), dtype=np.float32)
        for i in xrange(num_of_batch):
            if not episode_end[i][0]:
                tmp_ = np.sign(Reward[i]) + self.gamma * max_Q_dash[i]
            else:
                tmp_ = np.sign(Reward[i])

            action_index = self.action_to_index(action[i])
            target[i, action_index] = tmp_
        # TD-error clipping
        td = Variable(cuda.to_gpu(target)) - Q  # TD error
        td_tmp = td.data + 1000.0 * (abs(td.data) <= 1)  # Avoid zero division
        td_clip = td * (abs(td.data) <= 1) + td/abs(td_tmp) * (abs(td.data) > 1)

        zero_val = Variable(cuda.to_gpu(np.zeros((self.replay_size, self.num_of_actions), dtype=np.float32)))
        loss = F.mean_squared_error(td_clip, zero_val)
        return loss, Q
 def test_sqnorm_array_multi_gpu(self):
     x0 = cuda.to_gpu(self.x, device=0)
     x1 = cuda.to_gpu(self.x, device=1)
     a0 = cuda.to_gpu(self.a, device=0)
     a1 = cuda.to_gpu(self.a, device=1)
     self.assertAlmostEqual(optimizer._sum_sqnorm(
         [self.x, self.a, x0, a0, x1, a1]), 8.75 * 3)
Exemple #4
0
 def test_double_backward_gpu(self):
     x1 = cuda.to_gpu(self.x1)
     x2 = cuda.to_gpu(self.x2)
     gy = cuda.to_gpu(self.gy)
     ggx1 = cuda.to_gpu(self.ggx1)
     ggx2 = cuda.to_gpu(self.ggx2)
     self.check_double_backward(x1, x2, gy, ggx1, ggx2)
    def experienceReplay(self, time):

        if self.initial_exploration < time:
            # Pick up replay_size number of samples from the Data
            if time < self.data_size:  # during the first sweep of the History Data
                replay_index = np.random.randint(0, time, (self.replay_size, 1))
            else:
                replay_index = np.random.randint(0, self.data_size, (self.replay_size, 1))

            s_replay = np.ndarray(shape=(self.replay_size, 4, 84, 84), dtype=np.float32)
            a_replay = np.ndarray(shape=(self.replay_size, 1), dtype=np.uint8)
            r_replay = np.ndarray(shape=(self.replay_size, 1), dtype=np.float32)
            s_dash_replay = np.ndarray(shape=(self.replay_size, 4, 84, 84), dtype=np.float32)
            episode_end_replay = np.ndarray(shape=(self.replay_size, 1), dtype=np.bool)
            for i in xrange(self.replay_size):
                s_replay[i] = np.asarray(self.D[0][replay_index[i]], dtype=np.float32)
                a_replay[i] = self.D[1][replay_index[i]]
                r_replay[i] = self.D[2][replay_index[i]]
                s_dash_replay[i] = np.array(self.D[3][replay_index[i]], dtype=np.float32)
                episode_end_replay[i] = self.D[4][replay_index[i]]

            s_replay = cuda.to_gpu(s_replay)
            s_dash_replay = cuda.to_gpu(s_dash_replay)

            # Gradient-based update
            self.optimizer.zero_grads()
            loss, _ = self.forward(s_replay, a_replay, r_replay, s_dash_replay, episode_end_replay)
            loss.backward()
            self.optimizer.update()
 def test_forward_gpu(self):
     self.check_forward(
         cuda.to_gpu(self.x),
         cuda.to_gpu(self.offset),
         cuda.to_gpu(self.W),
         cuda.to_gpu(self.b),
         self.stride, self.pad)
Exemple #7
0
def generate_sample(state, primetext):
    prev_char = np.array([0], dtype=np.int32)
    if args.gpu >= 0:
        prev_char = cuda.to_gpu(prev_char)

    if len(primetext) > 0:
        for i in primetext:
            sys.stdout.write(i)
            prev_char = np.ones((1,)).astype(np.int32) * vocab[i]
            if args.gpu >= 0:
                prev_char = cuda.to_gpu(prev_char)
            state, prob = model.predict(prev_char, state)

    for i in xrange(args.length):
        state, prob = model.predict(prev_char, state)
        if args.sample > 0:
            probability = cuda.to_cpu(prob.data)[0].astype(np.float64)
            probability /= np.sum(probability)
            index = np.random.choice(range(len(probability)), p=probability)
        else:
            index = np.argmax(cuda.to_cpu(prob.data))
        sys.stdout.write(ivocab[index].encode("utf-8"))
        prev_char = np.array([index], dtype=np.int32)
        if args.gpu >= 0:
            prev_char = cuda.to_gpu(prev_char)

    return state
Exemple #8
0
def train_epoch(train_data, train_labels, model, optimizer, batchsize, transformations, silent, gpu=0, finetune=False):

    N = train_data.shape[0]
    pbar = ProgressBar(0, N)
    perm = np.random.permutation(N)
    sum_accuracy = 0
    sum_loss = 0

    for i in range(0, N, batchsize):
        x_batch = train_data[perm[i : i + batchsize]]
        y_batch = train_labels[perm[i : i + batchsize]]

        if transformations is not None:
            if "rotation" == transformations:
                x_batch = rotate_transform_batch(x_batch, rotation=2 * np.pi)

        if gpu >= 0:
            x_batch = cuda.to_gpu(x_batch.astype(np.float32))
            y_batch = cuda.to_gpu(y_batch.astype(np.int32))

        optimizer.zero_grads()
        x = Variable(x_batch)
        t = Variable(y_batch)

        loss, acc = model(x, t, train=True, finetune=finetune)
        if not finetune:
            loss.backward()
            optimizer.update()

        sum_loss += float(cuda.to_cpu(loss.data)) * y_batch.size
        sum_accuracy += float(cuda.to_cpu(acc.data)) * y_batch.size
        if not silent:
            pbar.update(i + y_batch.size)

    return sum_loss, sum_accuracy
Exemple #9
0
def train(train_dl, N, model, optimizer, trans, args, input_q, data_q):
    pbar = ProgressBar(N)
    perm = np.random.permutation(N)
    sum_loss = 0

    # putting all data
    for i in range(0, N, args.batchsize):
        x_batch = train_dl[perm[i:i + args.batchsize]]
        input_q.put(x_batch)

    # training
    for i in range(0, N, args.batchsize):
        input_data, label = data_q.get()

        if args.gpu >= 0:
            input_data = cuda.to_gpu(input_data.astype(np.float32))
            label = cuda.to_gpu(label.astype(np.float32))

        optimizer.zero_grads()
        loss, pred = model.forward(input_data, label, train=True)
        loss.backward()
        optimizer.update()

        sum_loss += float(cuda.to_cpu(loss.data)) * batchsize
        pbar.update(i + args.batchsize if (i + args.batchsize) < N else N)

    return sum_loss
def forward(x_data, y_data, train=True):
    x_0 = chainer.Variable(cuda.to_gpu(x_data, 0), volatile=not train)
    x_1 = chainer.Variable(cuda.to_gpu(x_data, 1), volatile=not train)
    t = chainer.Variable(cuda.to_gpu(y_data, 0), volatile=not train)

    h1_0 = F.dropout(F.relu(model.gpu0.l1(x_0)),  train=train)
    h1_1 = F.dropout(F.relu(model.gpu1.l1(x_1)),  train=train)

    h2_0 = F.dropout(F.relu(model.gpu0.l2(h1_0)), train=train)
    h2_1 = F.dropout(F.relu(model.gpu1.l2(h1_1)), train=train)

    h3_0 = F.dropout(F.relu(model.gpu0.l3(h2_0)), train=train)
    h3_1 = F.dropout(F.relu(model.gpu1.l3(h2_1)), train=train)

    # Synchronize
    h3_0 += F.copy(h3_1, 0)
    h3_1 = F.copy(h3_0, 1)

    h4_0 = F.dropout(F.relu(model.gpu0.l4(h3_0)), train=train)
    h4_1 = F.dropout(F.relu(model.gpu1.l4(h3_1)), train=train)

    h5_0 = F.dropout(F.relu(model.gpu0.l5(h4_0)),  train=train)
    h5_1 = F.dropout(F.relu(model.gpu1.l5(h4_1)),  train=train)

    h6_0 = F.relu(model.gpu0.l6(h5_0))
    h6_1 = F.relu(model.gpu1.l6(h5_1))

    # Synchronize
    y = h6_0 + F.copy(h6_1, 0)
    return F.softmax_cross_entropy(y, t), F.accuracy(y, t)
Exemple #11
0
    def get_loss(self, state, action, reward, state_prime, episode_end):
        s = Variable(cuda.to_gpu(state))
        s_dash = Variable(cuda.to_gpu(state_prime))

        q = self.model.q_function(s)  # Get Q-value

        # Generate Target Signals
        tmp = self.model_target.q_function(s_dash)  # Q(s',*)
        tmp = list(map(np.max, tmp.data))  # max_a Q(s',a)
        max_q_prime = np.asanyarray(tmp, dtype=np.float32)
        target = np.asanyarray(copy.deepcopy(q.data.get()), dtype=np.float32)

        for i in range(self.replay_size):
            if episode_end[i][0] is True:
                tmp_ = np.sign(reward[i])
            else:
                #  The sign of reward is used as the reward of DQN!
                tmp_ = np.sign(reward[i]) + self.gamma * max_q_prime[i]

            target[i, action[i]] = tmp_

        # TD-error clipping
        td = Variable(cuda.to_gpu(target)) - q  # TD error
        td_tmp = td.data + 1000.0 * (abs(td.data) <= 1)  # Avoid zero division
        td_clip = td * (abs(td.data) <= 1) + td/abs(td_tmp) * (abs(td.data) > 1)

        zero_val = Variable(cuda.to_gpu(np.zeros((self.replay_size, self.n_act), dtype=np.float32)))
        loss = F.mean_squared_error(td_clip, zero_val)
        return loss, q
Exemple #12
0
 def test_forward_gpu_volatile(self):
     self.check_forward(cuda.to_gpu(self.hx),
                        cuda.to_gpu(self.cx),
                        [cuda.to_gpu(x) for x in self.xs],
                        [[cuda.to_gpu(w) for w in ws] for ws in self.ws],
                        [[cuda.to_gpu(b) for b in bs] for bs in self.bs],
                        True)
    def fit(self, train_x, train_y, transpose=True):
        self.create_model(len(train_x[0]), 10)
        optimizer = optimizers.Adam()
        optimizer.setup(self.model.collect_parameters())

        N = len(train_x)

        for epoch in xrange(self.epochs):
            if self.varbose:
                print epoch + 1
            sum_loss = 0.0
            perm = np.random.permutation(N)
            for i in xrange(0, N, self.batch_size):
                x_batch = train_x[perm[i : i + self.batch_size]]
                y_batch = train_y[perm[i : i + self.batch_size]]

                if self.cuda:
                    x_batch = cuda.to_gpu(x_batch)
                    y_batch = cuda.to_gpu(y_batch)

                optimizer.zero_grads()
                y, loss = self.forward(x_batch, y_batch)
                loss.backward()
                optimizer.update()

                if self.varbose:
                    sum_loss += float(cuda.to_cpu(loss.data)) * self.batch_size
            if self.varbose:
                print sum_loss / N
def _gather(link, target):
    size, num = size_num_grads(link)

    ptrs = numpy.empty(num, dtype=numpy.uint64)
    dtypes = numpy.empty(num, dtype=numpy.int8)
    info = numpy.empty(num + 1, dtype=numpy.int32)
    info[0] = 0
    i = 0
    for _, param in sorted(link.namedparams()):
        if param.size == 0:
            continue
        ptrs[i] = 0  # NULL pointer
        d = getattr(param, target)
        if d is not None:
            ptrs[i] = d.data.ptr
        dtypes[i] = 0  # fp32
        if param.dtype == numpy.float16:
            dtypes[i] = 1  # fp16
        info[i + 1] = info[i] + param.size
        i += 1
    info[0] = num

    ptrs = cuda.to_gpu(ptrs)
    dtypes = cuda.to_gpu(dtypes)
    info = cuda.to_gpu(info)

    return _memcpy_gather()(ptrs, dtypes, info, size=size)
def _scatter(link, array, target):
    size, num = size_num_grads(link)

    ptrs = numpy.zeros(num, dtype=numpy.uint64)
    dtypes = numpy.zeros(num, dtype=numpy.int8)
    info = numpy.zeros(num + 1, dtype=numpy.int32)
    info[0] = 0
    i = 0
    for _, param in sorted(link.namedparams()):
        if param.size == 0:
            continue
        ptrs[i] = 0  # NULL pointer
        d = getattr(param, target)
        if d is None:
            d = cuda.cupy.zeros(param.shape, dtype=param.dtype)
            setattr(param, target, d)
        ptrs[i] = d.data.ptr
        dtypes[i] = 0  # fp32
        if param.dtype == numpy.float16:
            dtypes[i] = 1  # fp16
        info[i + 1] = info[i] + param.size
        i += 1
    if i != num:
        raise()
    info[0] = num

    ptrs = cuda.to_gpu(ptrs)
    dtypes = cuda.to_gpu(dtypes)
    info = cuda.to_gpu(info)

    return _memcpy_scatter()(ptrs, dtypes, info, array, size=size)
 def test_proposal_creator_gpu(self):
     self.check_proposal_creator(
         self.proposal_creator,
         cuda.to_gpu(self.bbox_d),
         cuda.to_gpu(self.score),
         cuda.to_gpu(self.anchor), self.img_size,
         scale=1.)
Exemple #17
0
 def predict(self, x_data, y_data, gpu=-1):
     if gpu >= 0:
         x_data = cuda.to_gpu(x_data)
         y_data = cuda.to_gpu(y_data)
     x, t = Variable(x_data), Variable(y_data)
     y = self.__forward(x)
     return F.softmax(y) #, F.accuracy(y, t)
Exemple #18
0
def train_loop():
    while True:
        while data_q.empty():
            time.sleep(0.1)
        inp = data_q.get()
        if inp == 'end':  # quit
            res_q.put('end')
            break
        elif inp == 'train':  # restart training
            res_q.put('train')
            train = True
            continue
        elif inp == 'val':  # start validation
            res_q.put('val')
            pickle.dump(model, open('model', 'wb'), -1)
            train = False
            continue

        x, y = inp
        if args.gpu >= 0:
            x = cuda.to_gpu(x)
            y = cuda.to_gpu(y)

        if train:
            optimizer.zero_grads()
            loss, accuracy = model.forward(x, y)
            loss.backward()
            optimizer.update()
        else:
            loss, accuracy = model.forward(x, y, train=False)

        res_q.put((float(cuda.to_cpu(loss.data)),
                   float(cuda.to_cpu(accuracy.data))))
        del loss, accuracy, x, y
Exemple #19
0
    def forward(self, state, action, Reward, state_dash, episode_end):
        num_of_batch = state.shape[0]
        s = Variable(state)
        s_dash = Variable(state_dash)

        Q = self.model.Q_func(s,train=True)  # Get Q-value

        # Generate Target Signals
        tmp = self.model_target.Q_func(s_dash,train=self.targetFlag)  # Q(s',*)
        tmp = list(map(np.max, tmp.data.get()))  # max_a Q(s',a)
        max_Q_dash = np.asanyarray(tmp, dtype=np.float32)
        target = np.asanyarray(Q.data.get(), dtype=np.float32)

        for i in xrange(num_of_batch):
            if not episode_end[i][0]:
                tmp_ = np.sign(Reward[i]) + self.gamma * max_Q_dash[i]
            else:
                tmp_ = np.sign(Reward[i])
            #print action
            action_index = self.action_to_index(action[i])
            target[i, action_index] = tmp_

        # TD-error clipping
        td = Variable(cuda.to_gpu(target,self.gpu_id)) - Q  # TD error
        td_tmp = td.data + 1000.0 * (abs(td.data) <= 1)  # Avoid zero division
        td_clip = td * (abs(td.data) <= 1) + td/abs(td_tmp) * (abs(td.data) > 1)

        zero_val = Variable(cuda.to_gpu(np.zeros((self.replay_size, self.num_of_actions), dtype=np.float32),self.gpu_id))
        loss = F.mean_squared_error(td_clip, zero_val)
        return loss, Q
Exemple #20
0
 def check_forward_gpu(self, use_cudnn):
     with chainer.using_config('use_cudnn', use_cudnn):
         self.check_forward(
             cuda.to_gpu(self.hx),
             [cuda.to_gpu(x) for x in self.xs],
             [[cuda.to_gpu(w) for w in ws] for ws in self.ws],
             [[cuda.to_gpu(b) for b in bs] for bs in self.bs])
Exemple #21
0
    def __call__(self, x, test=False, finetune=False):
        """Invokes the forward propagation of BatchNormalization.

        BatchNormalization accepts additional arguments, which controls three
        different running mode.

        Args:
            x (Variable): Input variable.
            test (bool): If ``True``, BatchNormalization runs in testing mode;
                it normalizes the input using pre-computed statistics.
            finetune (bool): If ``finetune`` is ``True`` and ``test`` is
                ``False``, BatchNormalization runs in fine-tuning mode; it
                accumulates the input array to compute population statistics
                for normalization, and normalizes the input using batch
                statistics.

        If ``test`` is ``False``, then BatchNormalization runs in training
        mode; it computes moving averages of mean and variance for evaluation
        during training, and normalizes the input using batch statistics.

        """
        if hasattr(self, 'gamma'):
            gamma = self.gamma
        else:
            gamma_ = self.xp.ones(self.avg_mean.shape, dtype=x.dtype)
            if self.device is None:
                gamma = variable.Variable(gamma_, volatile='auto')
            else:
                gamma = \
                        variable.Variable(cuda.to_gpu(gamma_, self.device), volatile='auto')
        if hasattr(self, 'beta'):
            beta = self.beta
        else:
            beta_ = self.xp.zeros(self.avg_mean.shape, dtype=x.dtype)
            if self.device is None:
                beta = variable.Variable(beta_, volatile='auto')
            else:
                beta = variable.Variable(cuda.to_gpu(beta_, self.device), volatile='auto')

        if not test:
            if finetune:
                self.N += 1
                decay = 1. - 1. / self.N
            else:
                decay = self.decay

            func = batch_normalization.BatchNormalizationFunction(
                self.eps, self.avg_mean, self.avg_var, True, decay,
                self.use_cudnn)
            ret = func(x, gamma, beta)

            self.avg_mean[:] = func.running_mean
            self.avg_var[:] = func.running_var
        else:
            # Use running average statistics or fine-tuned statistics.
            mean = variable.Variable(self.avg_mean, volatile='auto')
            var = variable.Variable(self.avg_var, volatile='auto')
            ret = batch_normalization.fixed_batch_normalization(
                x, gamma, beta, mean, var, self.eps, self.use_cudnn)
        return ret
    def check_backward(self, inputs, grad_outputs, backend_config):

        xp = backend_config.xp
        if backend_config.use_cuda:
            inputs = cuda.to_gpu(inputs)
            grad_outputs = cuda.to_gpu(grad_outputs)

        x_data, W_data, b_data = inputs
        y_grad, = grad_outputs

        if not self.c_contiguous:
            x_data = xp.asfortranarray(x_data)
            W_data = xp.asfortranarray(W_data)
            y_grad = xp.asfortranarray(y_grad)
            assert not x_data.flags.c_contiguous
            assert not W_data.flags.c_contiguous
            assert not y_grad.flags.c_contiguous
            if b_data is not None:
                b = xp.empty((len(b_data) * 2,), dtype=b_data.dtype)
                b[::2] = b_data
                b_data = b[::2]
                assert not b_data.flags.c_contiguous

        args = (x_data, W_data)
        if b_data is not None:
            args = args + (b_data,)

        def f(*args):
            return F.deconvolution_2d(
                *args, stride=self.stride, pad=self.pad, outsize=self.outsize,
                dilate=self.dilate, group=self.group)

        with backend_config:
            gradient_check.check_backward(
                f, args, y_grad, **self.check_backward_options)
Exemple #23
0
 def test_forward_gpu_train(self):
     self.rnn.to_gpu()
     with chainer.using_config('use_cudnn', 'always'), \
             chainer.using_config('train', True):
         self.check_forward(
             cuda.to_gpu(self.h),
             [cuda.to_gpu(x) for x in self.xs])
def train(train_dl, N, model, optimizer, args, input_q, data_q):
    widgets = ["Training : ", Percentage(), Bar()]
    pbar = ProgressBar(maxval = N, widgets = widgets).start()
    sum_loss = 0

    # putting all data
    for i in range(0, N, 2):
        x_batch = train_dl[perm[i:i + 2]]

        input_q.put(x_batch)

    # training
    for i in range(0, N, args.batchsize):
        input_data, label = data_q.get()
        print (input_data.shape, label.shape)
        if args.gpu >= 0:
            input_data = cuda.to_gpu(input_data.astype(np.float32))
            label = cuda.to_gpu(label.astype(np.float32))

        optimizer.zero_grads()
        loss, pred = model.forward(input_data, label, train=True)
        loss.backward()
        optimizer.update()

        sum_loss += float(cuda.to_cpu(loss.data)) * batchsize
        pbar.update(i + args.batchsize if (i + args.batchsize) < N else N)

    return sum_loss
Exemple #25
0
def check_equivariance(im, layers, input_array, output_array, point_group):

    # Transform the image
    f = input_array(im)
    g = point_group.rand()
    gf = g * f
    im1 = gf.v

    # Apply layers to both images
    im = Variable(cuda.to_gpu(im))
    im1 = Variable(cuda.to_gpu(im1))

    fmap = im
    fmap1 = im1
    for layer in layers:
        layer.to_gpu()
        fmap = layer(fmap)
        fmap1 = layer(fmap1)

    # Transform the computed feature maps
    fmap1_garray = output_array(cuda.to_cpu(fmap1.data))
    r_fmap1_data = (g.inv() * fmap1_garray).v

    fmap_data = cuda.to_cpu(fmap.data)
    assert np.allclose(fmap_data, r_fmap1_data, rtol=1e-5, atol=1e-3)
Exemple #26
0
 def test_DNSP_sparse_matmul_forward_gpu(self):
     a = cuda.to_gpu(self.a)
     b = cuda.to_gpu(self.b)
     if self.a_dtype == numpy.float16 or self.b_dtype == numpy.float16:
         self.check_DNSP_forward(a, b, atol=1e-3, rtol=1e-3)
     else:
         self.check_DNSP_forward(a, b)
Exemple #27
0
    def train(self, x, y, actions=None):
        actions = actions.astype(np.int32)
        batch_size = len(actions)

        if self._gpu_device:
            x = cuda.to_gpu(x, self._gpu_device)
            y = cuda.to_gpu(y, self._gpu_device)
            actions = cuda.to_gpu(actions, self._gpu_device)

        q = self._model(x)
        q_subset = F.reshape(F.select_item(q, actions), (batch_size, 1))
        y = y.reshape(batch_size, 1)

        loss = F.sum(F.huber_loss(q_subset, y, 1.0))

        self._model.cleargrads()
        loss.backward()
        self._optimizer.update()

        self._loss_val = np.asscalar(cuda.to_cpu(loss.data))

        # Keeps track of the number of train() calls
        self._steps += 1
        if self._steps % self._target_update_interval == 0:
            # copy weights
            self._target.copyparams(self._model)
Exemple #28
0
    def get(self, n, balance=True):
        ind = np.random.permutation(self.data.shape[0])
        if not balance:
            d = self.data[ind[:n], :].astype(np.float32)
            l = self.label[ind[:n]].astype(np.int32)
            if gpu > -1:
                d = cuda.to_gpu(d)
                l = cuda.to_gpu(l)
            return d, l
        else:
            cnt = [0]*10
            m = 0
            ret_data = np.zeros((n, self.data.shape[1])).astype(np.float32)
            ret_label = np.zeros(n).astype(np.int32)
            for i in range(self.data.shape[0]):
                if cnt[self.label[ind[i]]] < n/10:
                    ret_data[m, :] = self.data[ind[i]]
                    ret_label[m] = self.label[ind[i]]

                    cnt[self.label[ind[i]]] += 1
                    m += 1
                    if m == n:
                        break
            if gpu > -1:
                ret_data = cuda.to_gpu(ret_data)
                ret_label = cuda.to_gpu(ret_label)
            return ret_data, ret_label
Exemple #29
0
def convert_data(before_data, gpu):
	d = []	# story: 15 → [[3通常文(mem), 1質問文, 1答え],[6通常文(mem), 1質問文, 1答え],...]
	# 文の最長単語数を求める
	sentence_maxlen = max(max(len(s.sentence) for s in story) for story in before_data)
	for story in before_data:
		mem = np.ones((50, sentence_maxlen), dtype=np.int32)		# mem: 50×sentence_maxlenのint32のゼロ行列
		mem = -1 * mem
		i = 0
		for sent in story:
#			# isinstance(object, class): objectがclassのインスタンスかどうか
			if isinstance(sent, data.Sentence):
				if i == 50:		# The capacity of memory is restricted to the most 50 sentence(1ストーリーあたり50文まで記憶する)
					mem[0:i-1, :] = mem[1:i, :]		# 一番古い情報をシフトする(1〜49→0〜48にシフト)
					# print mem[0,0:3]	# 0行目の0〜2列を取得
#					mem_length[0:i-1] = mem_length[1:i]
					i -= 1
				mem[i, 0:len(sent.sentence)] = sent.sentence
#				mem_length[i] = len(sent.sentence)
				i += 1
			elif isinstance(sent, data.Query):
				# question sentence
				query = np.ones(sentence_maxlen, dtype=np.int32)	# 質問文ベクトル
				query = -1 * query
				query[0:len(sent.sentence)] = sent.sentence
				query = query[np.newaxis, :]		# 1次元→2次元配列に変換
				answer = np.array([sent.answer], dtype=np.int32)
				if gpu >= 0:	# gpu
					d.append((cuda.to_gpu(mem),cuda.to_gpu(query),cuda.to_gpu(answer)))
				else:
					d.append((copy.deepcopy(mem),(query),answer))

	return d
 def test_backward_gpu(self):
     self.rnn.to_gpu()
     self.check_backward(
         cuda.to_gpu(self.h),
         [cuda.to_gpu(x) for x in self.xs],
         cuda.to_gpu(self.gh),
         [cuda.to_gpu(gy) for gy in self.gys])
 def test_forward_gpu_nobias(self):
     self.check_forward(cuda.to_gpu(self.x), cuda.to_gpu(self.W), None,
                        cuda.to_gpu(self.x.dot(self.W.T)))
 def test_forward_gpu_no_cudnn(self):
     self.check_forward(cuda.to_gpu(self.x), cuda.to_gpu(self.t), False)
 def test_forward_no_reduction_gpu(self):
     self.check_forward_no_reduction(cuda.to_gpu(self.x),
                                     cuda.to_gpu(self.t))
 def test_forward_gpu(self):
     self.check_forward(cuda.to_gpu(self.x), cuda.to_gpu(self.t))
 def test_backward_gpu(self):
     x1 = cuda.to_gpu(self.x1)
     x2 = cuda.to_gpu(self.x2)
     g = cuda.to_gpu(self.g)
     self.check_backward(x1, x2, g)
Exemple #36
0
 def test_backward_gpu(self):
     print 'test_backward_gpu'
     self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.ksizes),
                         cuda.to_gpu(self.gy))
Exemple #37
0
 def test_forward_gpu(self):
     print 'test_forward_gpu'
     self.check_forward(cuda.to_gpu(self.x), cuda.to_gpu(self.ksizes))
 def test_double_backward_gpu_nobias(self):
     self.check_double_backward(cuda.to_gpu(self.x),
                                cuda.to_gpu(self.W), None,
                                cuda.to_gpu(self.gy), cuda.to_gpu(self.ggx),
                                cuda.to_gpu(self.ggW), None)
 def test_backward_gpu_nobias(self):
     self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.W), None,
                         cuda.to_gpu(self.gy))
 def test_forward_gpu(self):
     self.check_forward(cuda.to_gpu(self.x), cuda.to_gpu(self.W),
                        cuda.to_gpu(self.b),
                        cuda.to_gpu(self.x.dot(self.W.T) + self.b))
 def test_backward_gpu(self):
     self.check_backward(cuda.to_gpu(self.x0), cuda.to_gpu(self.x1))
 def test_double_backward_gpu(self):
     self.check_double_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.W),
                                cuda.to_gpu(self.b), cuda.to_gpu(self.gy),
                                cuda.to_gpu(self.ggx),
                                cuda.to_gpu(self.ggW),
                                cuda.to_gpu(self.ggb))
 def test_backward_gpu_im2col_nobias(self):
     self.use_cudnn = 'never'
     self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.W), None,
                         cuda.to_gpu(self.gy))
 def test_backward_gpu(self):
     self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.W),
                         cuda.to_gpu(self.b), cuda.to_gpu(self.gy))
 def test_forward_gpu_non_contiguous(self):
     self.check_forward(cuda.cupy.asfortranarray(cuda.to_gpu(self.x)))
Exemple #46
0
def train_dcgan_labeled(gen, dis, epoch0=0):
    o_gen = optimizers.Adam(alpha=0.0002, beta1=0.5)
    o_dis = optimizers.Adam(alpha=0.0002, beta1=0.5)
    o_gen.setup(gen)
    o_dis.setup(dis)
    o_gen.add_hook(chainer.optimizer.WeightDecay(0.00001))
    o_dis.add_hook(chainer.optimizer.WeightDecay(0.00001))

    zvis = (xp.random.uniform(-1, 1, (100, nz), dtype=np.float32))
    
    for epoch in xrange(epoch0,n_epoch):
        perm = np.random.permutation(n_train)
        sum_l_dis = np.float32(0)
        sum_l_gen = np.float32(0)
        
        for i in xrange(0, n_train, batchsize):
            print("{}/{}".format(i, n_train))
            # discriminator
            # 0: from dataset
            # 1: from noise

            #print "load image start ", i
            x2 = np.zeros((batchsize, 3, 96, 96), dtype=np.float32)
            for j in range(batchsize):
                try:
                    rnd = np.random.randint(len(dataset))
                    rnd2 = np.random.randint(2)

                    img = np.asarray(Image.open(StringIO(dataset[rnd])).convert('RGB')).astype(np.float32).transpose(2, 0, 1)
                    if rnd2==0:
                        x2[j,:,:,:] = (img[:,:,::-1]-128.0)/128.0
                    else:
                        x2[j,:,:,:] = (img[:,:,:]-128.0)/128.0
                except:
                    print 'read image error occured', fs[rnd]
            #print "load image done"
            
            # train generator
            z = Variable(xp.random.uniform(-1, 1, (batchsize, nz), dtype=np.float32))
            x = gen(z)
            yl = dis(x)
            L_gen = F.softmax_cross_entropy(yl, Variable(xp.zeros(batchsize, dtype=np.int32)))
            L_dis = F.softmax_cross_entropy(yl, Variable(xp.ones(batchsize, dtype=np.int32)))
            
            # train discriminator
                    
            x2 = Variable(cuda.to_gpu(x2))
            yl2 = dis(x2)
            L_dis += F.softmax_cross_entropy(yl2, Variable(xp.zeros(batchsize, dtype=np.int32)))
            
            #print "forward done"

            o_gen.zero_grads()
            L_gen.backward()
            o_gen.update()
            
            o_dis.zero_grads()
            L_dis.backward()
            o_dis.update()
            
            sum_l_gen += L_gen.data.get()
            sum_l_dis += L_dis.data.get()
            
            #print "backward done"

            if i%image_save_interval==0:
                pylab.rcParams['figure.figsize'] = (16.0,16.0)
                pylab.clf()
                vissize = 100
                z = zvis
                z[50:,:] = (xp.random.uniform(-1, 1, (50, nz), dtype=np.float32))
                z = Variable(z)
                x = gen(z, test=True)
                x = x.data.get()
                for i_ in range(100):
                    tmp = ((np.vectorize(clip_img)(x[i_,:,:,:])+1)/2).transpose(1,2,0)
                    pylab.subplot(10,10,i_+1)
                    pylab.imshow(tmp)
                    pylab.axis('off')
                pylab.savefig('%s/vis_%d_%d.png'%(out_image_dir, epoch,i))
                
        serializers.save_hdf5("%s/dcgan_model_dis_%d.h5"%(out_model_dir, epoch),dis)
        serializers.save_hdf5("%s/dcgan_model_gen_%d.h5"%(out_model_dir, epoch),gen)
        serializers.save_hdf5("%s/dcgan_state_dis_%d.h5"%(out_model_dir, epoch),o_dis)
        serializers.save_hdf5("%s/dcgan_state_gen_%d.h5"%(out_model_dir, epoch),o_gen)
        print 'epoch end', epoch, sum_l_gen/n_train, sum_l_dis/n_train
Exemple #47
0
 def test_backward_gpu(self):
     self.check_backward(cuda.to_gpu(self.data), cuda.to_gpu(self.grad))
 def test_forward_gpu_no_cudnn(self):
     self.check_forward(cuda.to_gpu(self.x), 'never')
Exemple #49
0
 def test_backward_gpu(self):
     self.check_backward([cuda.to_gpu(x) for x in self.data],
                         [cuda.to_gpu(x) for x in self.grads])
 def test_backward_gpu_no_cudnn(self):
     self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.gy), 'never')
optimizer = optimizers.RMSprop(lr=args.learning_rate,
                               alpha=args.decay_rate,
                               eps=1e-8)
optimizer.setup(model)

whole_len = train_data.shape[0]
jump = whole_len / batchsize
epoch = 0
start_at = time.time()
cur_at = start_at
state = make_initial_state(n_units, batchsize=batchsize)
if args.gpu >= 0:
    accum_loss = Variable(cuda.zeros(()))
    for key, value in state.items():
        value.data = cuda.to_gpu(value.data)
else:
    accum_loss = Variable(np.zeros((), dtype=np.float32))

print 'going to train {} iterations'.format(jump * n_epochs)
for i in xrange(jump * n_epochs):
    x_batch = np.array(
        [train_data[(jump * j + i) % whole_len] for j in xrange(batchsize)])
    y_batch = np.array([
        train_data[(jump * j + i + 1) % whole_len] for j in xrange(batchsize)
    ])

    if args.gpu >= 0:
        x_batch = cuda.to_gpu(x_batch)
        y_batch = cuda.to_gpu(y_batch)
Exemple #52
0
 def test_forward_gpu(self):
     self.check_forward(cuda.to_gpu(self.data))
Exemple #53
0
 def test_backward_gpu(self):
     self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.indices),
                         cuda.to_gpu(self.g))
Exemple #54
0
 def test_forward_gpu(self):
     self.check_forward([cuda.to_gpu(x) for x in self.data])
Exemple #55
0
 def test_sqnorm_array(self):
     x = cuda.to_gpu(self.x)
     a = cuda.to_gpu(self.a)
     self.assertAlmostEqual(optimizer._sum_sqnorm(
         [self.x, self.a, x, a]), 8.75 * 2)
Exemple #56
0
 def test_forward_gpu(self):
     x1 = cuda.to_gpu(self.x1)
     x2 = cuda.to_gpu(self.x2)
     self.check_forward(x1, x2, self.y_expected)
Exemple #57
0
 def test_sqnorm_gpu(self):
     x = cuda.to_gpu(self.x)
     self.assertAlmostEqual(optimizer._sum_sqnorm([x]), 4.75)
Exemple #58
0
 def test_invlaid_gpu(self):
     self.check_invalid(cuda.to_gpu(self.x), cuda.to_gpu(self.ind))
 def test_backward_gpu(self):
     self.link.to_gpu()
     self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.gy))
Exemple #60
0
 def test_sqnorm_scalar_gpu(self):
     a = cuda.to_gpu(self.a)
     self.assertAlmostEqual(optimizer._sum_sqnorm([a]), 4)