def forward(self, x=None, c=None, train=True):

        if train:
            if self.conditional:
                c = idx2onehot(c, n=2)  # used to be 10
                full_x = torch.cat((x, c), dim=-1)
            else:
                full_x = x

            full_x = self.MLP_q(full_x)

            alpha_q_lin = self.linear_latent_q(full_x)
            alpha_q = self.softmax_q(alpha_q_lin)

        else:
            alpha_q_lin = None
            alpha_q = None
            if self.conditional:
                c = idx2onehot(c, n=2)  # used to be 10

        c = self.MLP_p(c)

        alpha_p_lin = self.linear_latent_p(c)
        alpha_p = self.softmax_p(alpha_p_lin)
        alpha_p_sparsemax = self.sparsemax_p(alpha_p_lin)

        return alpha_q, alpha_p, alpha_p_sparsemax, c
    def forward(self, x=None, c=None, train=True):

        if train:
            if self.conditional:
                c = idx2onehot(c, n=self.num_labels)  # used to be 10

                # concatenate labels to the images as in:
                # https://github.com/rasbt/deeplearning-models/blob/master/pytorch_ipynb/autoencoder/ae-cnn-cvae_no-out-concat.ipynb
                c_concat = c.view(-1, self.num_labels, 1, 1)
                c_concat = torch.ones(c.size()[0],
                                      self.num_labels,
                                      x.size()[2],
                                      x.size()[3],
                                      dtype=x.dtype).cuda() * c_concat

                full_x = torch.cat((x, c_concat), dim=1)

            full_x = self.MLP_q(full_x)

            alpha_q_lin = self.linear_latent_q(full_x)
            alpha_q = self.softmax_q(alpha_q_lin)

        else:
            alpha_q_lin = None
            alpha_q = None
            if self.conditional:
                c = idx2onehot(c, n=2)

        c = self.MLP_p(c)

        alpha_p_lin = self.linear_latent_p(c)
        alpha_p = self.softmax_p(alpha_p_lin)
        alpha_p_max = self.sparsemax_p(alpha_p_lin)

        return alpha_q, alpha_p, alpha_p_max, c
示例#3
0
    def forward(self, z, c=None, d=None):

        if self.conditional:
            c = idx2onehot(c, n=self.num_labels)
            z = torch.cat((z, c), dim=-1)
        if self.num_domains > 0:
            d = idx2onehot(d.cpu(), n=self.num_domains).to(self.device)
            z = torch.cat((z, d), dim=-1)
        x = self.fc1(z)
        x = self.fc2(x)
        x = self.decoder(x.view(-1, 1024, 1, 1))
        return x
示例#4
0
 def decode(self, z, c):
     c = idx2onehot(c, n=10)
     z = torch.cat((z, c), dim=-1)
     h2 = self.fc3(z)
     h2 = h2.view(-1, 2, 14, 14)
     h2 = self.conv2(h2)
     return h2
    def forward(self, x, c=None):
        x = x.view([-1, 1, 28, 28])

        if self.conditional:
            c = idx2onehot(c, n=28)
            c = c.reshape(-1, 1, 28, 1)
            x = torch.cat((x, c), dim=-1)

        print(x.shape)
        x = self.conv1(x)
        print(x.shape)
        x = self.pool(x)
        x = self.relu(x)
        x = self.conv2(x)
        print(x.shape)
        x = self.pool(x)
        x = self.relu(x)
        x = self.conv3(x)
        print(x.shape)
        x = self.pool(x)
        x = self.relu(x)

        x = x.view([-1, 128 * 4 * 4])
        #x = self.fc(x)
        print(x.shape)

        if not (self.variational or self.conditional):
            return self.linear_z(x)

        means = self.linear_means(x)
        log_vars = self.linear_log_var(x)

        return means, log_vars
示例#6
0
    def forward(self, z,d=None):

        if self.num_domains > 0:
            d = idx2onehot(d.cpu(), n=self.num_domains).to(self.device)
            z = torch.cat((z,d), dim=-1)
        x = self.MLP(z)
        x = F.normalize(x, p=2, dim=1)
        return x
示例#7
0
 def decode(self, z, y, label, alpha):
     z = z.view(-1, 20)
     y_c = idx2onehot(y, 10, label, alpha)
     cat = torch.cat((z, y_c), dim=-1)
     h3 = self.relu(self.fc3(cat))
     ret = self.sigmoid(self.fc4(h3))
     ret = torch.narrow(ret, 1, 0, 784)
     return ret
示例#8
0
    def forward(self, z, c):

        if self.conditional:
            c = idx2onehot(c.type(torch.LongTensor).to('cuda'), n=3*18)
            z = torch.cat((z, c), dim=-1)

        x = self.model(z)

        return x
示例#9
0
    def forward(self, z, c):

        if self.conditional:
            c = idx2onehot(c.cpu(), n=10).to(self.device)
            z = torch.cat((z, c), dim=-1)

        x = self.MLP(z)

        return x
示例#10
0
    def forward(self, z, c):

        if self.conditional:
            c = idx2onehot(c, n=10)
            z = torch.cat((z, c), dim=-1)

        x = self.MLP(z)

        return x
示例#11
0
    def forward(self, x, c=None):

        if self.conditional:
            c = idx2onehot(c, n=10)
            x = torch.cat((x, c), dim=-1)

        z = self.MLP(x)

        return z
示例#12
0
 def forward(self, x, c=None):
     if self.conditional:
         c = idx2onehot(c, n=28)
         c = c.reshape(-1, 1, 28, 1)
         x = T.cat((x, c), dim=-1)
     f_d = self.convs(x)
     x = self.last_conv(f_d)
     f_d = F.avg_pool2d(f_d, 4, 1, 0)
     return x.squeeze(), f_d.squeeze()
示例#13
0
 def encode(self, x, c):
     c = idx2onehot(c, n=10)
     c = torch.unsqueeze(c, -1)
     c = torch.unsqueeze(c, -1)
     c = c.expand(-1, -1, x.size()[2], x.size()[3])
     x = torch.cat((x, c), dim=1)
     h1 = self.conv1(x)
     h1 = h1.view(-1, 784)
     h1 = self.fc1(h1)
     return self.fc21(h1), self.fc22(h1)
示例#14
0
    def forward(self, z, c, c2=None, alpha=1):

        if self.conditional:
            #print("decoder")
            c = idx2onehot(c, 10, c2, alpha)
            z = torch.cat((z, c), dim=-1)

        x = self.MLP(z)

        return x
示例#15
0
    def decode(self, z, c):
        if c is not None:
            # restructure input
            qq = utils.idx2onehot(c.view(-1, 1), self.num_class)
            z = torch.cat([z, qq], 1)

        recon_x = self.Decoder_input(z)
        recon_x = recon_x.view(recon_x.size(0), -1, self.img_size_s,
                               self.img_size_s)
        recon_x = self.Decoder(recon_x)
        return recon_x
    def forward(self, z, c):

        if self.conditional:
            c = idx2onehot(c, n=self.num_labels)
            z = torch.cat((z, c), dim=-1)
        elif self.subspace:
            z = torch.cat((z, c), dim=-1)

        z = self.fc(z)
        x = self.decoder(z)

        return x
示例#17
0
    def forward(self, z, c):
        device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
        if self.conditional:
            c = idx2onehot(c, n=10)
            c = c.to(device)
            z = z.to(device)
            z = torch.cat((z, c), dim=-1)

        z = z.to(device)
        x = self.MLP(z)

        return x
示例#18
0
    def forward(self, x, c=None):

        if self.conditional:
            c = idx2onehot(c, n=10)
            x = torch.cat((x, c), dim=-1)

        x = self.MLP(x)

        means = self.linear_means(x)
        log_vars = self.linear_log_var(x)

        return means, log_vars
示例#19
0
    def forward(self, x, d=None):

        if self.num_domains>0:
            d = idx2onehot(d.cpu(), n=self.num_domains).to('cuda')
            x = torch.cat((x, d), dim=-1)

        x = self.MLP(x)

        means = self.linear_means(x)
        log_vars = self.linear_log_var(x)
        means = F.normalize(means, p=2, dim=1)
        log_vars = F.normalize(log_vars, p=2, dim=1)
        return means, log_vars
示例#20
0
    def forward(self, z, c):
        if self.conditional:
            c = idx2onehot(c, n=10)
            z = torch.cat((z, c), dim=1)

        x = self.fc(z)
        x = x.view(-1, 64, 7, 7)
        x = self.conv(x)
        # BECAUSE MNIST is -1 to 1, rescale from 0 to 1
        x = x.view(-1, 28 * 28)
        x = x * 2 - 1

        return x
示例#21
0
    def forward(self, x, c=None, c2=None, alpha=1):

        if self.conditional:
            #print("encoder")
            c = idx2onehot(c, 10, c2, alpha)
            x = torch.cat((x, c), dim=-1)

        x = self.MLP(x)

        means = self.linear_means(x)
        log_vars = self.linear_log_var(x)

        return means, log_vars
    def forward(self, x, c=None):

        x = self.encoder(x)
        c = idx2onehot(c, n=self.num_labels)
        x = torch.cat((x, c), dim=-1)
        x = self.fc(x)

        means = self.linear_means(x)
        log_vars = self.linear_log_var(x)

        w_sam = self.reparameterize(means, log_vars)

        return means, log_vars, w_sam
示例#23
0
    def forward(self, x, c=None):
        x = x.view(-1, 1, 28, 28)
        x = self.conv(x)
        x = x.view(-1, 64 * 7 * 7)

        if self.conditional:
            c = idx2onehot(c, n=10)
            x = torch.cat((x, c), dim=1)
        x = self.fc(x)

        means = self.linear_means(x)
        log_vars = self.linear_log_var(x)

        return means, log_vars
示例#24
0
    def encode(self, x, c):
        if c is not None:
            # restructure input
            qq = utils.idx2onehot(c.view(-1, 1), self.num_class)
            qq = qq.view(-1, self.num_class, 1,
                         1).expand(-1, -1, self.img_size, self.img_size)
            x = torch.cat([x, qq], 1)

        x = self.Encoder(x)
        x = x.view(x.size(0), -1)

        # exit()
        mu, logvar = self.mu(x), self.logvar(x)
        z = self.reparameterize(mu, logvar)
        return mu, logvar, z
示例#25
0
    def forward(self, z, c=None):
        if self.conditional:
            c = idx2onehot(c, n=10)
            z = T.cat((z, c), dim=-1)
        z = self.fc(z)
        z = z.view([-1, 256, 4, 4])
        x = self.c1(z)
        x = self.relu(x)
        x = self.c2(x)
        x = self.relu(x)
        x = self.c3(x)
        x = self.sig(x)
        #x = x.view([-1,28*28])

        return x
示例#26
0
    def forward(self, x, c=None):
        if self.conditional:
            c = idx2onehot(c.type(torch.LongTensor).to('cuda'), n=3*18)
            c = c.reshape(-1,3,  18, 1)
            x = torch.cat((x, c), dim=-1)
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.pool(x)
        x = self.relu(x)
        x = self.model(x)
        if not (self.variational or self.conditional):
            return self.linear_z(x)

        means = self.linear_means(x)
        log_vars = self.linear_log_var(x)

        return means, log_vars
示例#27
0
    def forward(self, x, c=None):
        if self.conditional:
            c = idx2onehot(c, n=28)
            c = c.reshape(-1, 1, 28, 1)
            x = torch.cat((x, c), dim=-1)
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.pool(x)
        x = self.relu(x)
        x = self.model(x)
        if not (self.variational or self.conditional):
            return self.linear_z(x)

        means = self.linear_means(x)
        log_vars = self.linear_log_var(x)

        return means, log_vars
示例#28
0
    def learning(self):
        torch.manual_seed(self.opt.seed)
        coef_hat = torch.FloatTensor([self.opt.coef_hat]*self.opt.batch_size*self.opt.n_step).view(self.opt.batch_size, self.opt.n_step)
        rho_hat = torch.FloatTensor([self.opt.rho_hat]*self.opt.batch_size*self.opt.n_step).view(self.opt.batch_size, self.opt.n_step)
        while True:
            # batch-trace
            states, actions, rewards, dones, action_log_probs = self.q_batch.get(block=True)

            logit_log_probs = self.actor(states)
            V = self.critic(states).view(self.opt.batch_size, self.opt.n_step) * (1 - dones)

            action_probs = torch.exp(action_log_probs)
            logit_probs = torch.exp(logit_log_probs)

            is_rate = torch.prod(logit_probs / (action_probs + 1e-6), dim=-1).detach()
            coef = torch.min(coef_hat, is_rate) * (1 - dones)
            rho = torch.min(rho_hat, is_rate) * (1 - dones)

            # V-trace
            v_trace = torch.zeros((self.opt.batch_size, self.opt.n_step)).to(self.device)
            target_V = V.detach()
            for rev_step in reversed(range(states.size(1) - 1)):
                v_trace[:, rev_step] = target_V[:, rev_step] \
                                       + rho[:, rev_step] * (rewards[:, rev_step] + self.opt.gamma*target_V[:, rev_step+1] - target_V[:, rev_step]) \
                                       + self.opt.gamma * coef[:, rev_step] * (v_trace[:, rev_step+1] - target_V[:, rev_step+1])

            # actor loss
            onehot_actions = torch.FloatTensor(
                idx2onehot(actions.cpu().numpy(), self.opt.batch_size, self.n_act)).to(self.device)
            logit_log_probs = torch.sum(logit_log_probs * onehot_actions, dim=-1)
            advantages = rewards + self.opt.gamma * v_trace - V
            pg_loss = -torch.sum(logit_log_probs * advantages.detach())
            actor_loss = pg_loss

            self.actor_optimizer.zero_grad()
            actor_loss.backward()
            self.actor_optimizer.step()

            # critic
            critic_loss = torch.mean((v_trace.detach() - V)**2)

            self.critic_optimizer.zero_grad()
            critic_loss.backward()
            self.critic_optimizer.step()
示例#29
0
                    if isValidation:
                        validation_loss = self.calculate_loss(X_val, y_val)
                        logger.info('epoch %i, seq %i/%i, tr loss %f te loss %f lr: %f' %
                                    (epoch + 1, idx + 1, n_train, train_loss, validation_loss, learning_rate))
                    else:
                        logger.info('epoch %i, seq %i/%i, train loss %f lr: %f' %
                                    (epoch + 1, idx + 1, n_train, train_loss, learning_rate))

            learning_rate *= learning_rate_decay


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    in_dim, hidden_dim, out_dim = 300, 128, 3
    data = cPickle.load(open("./data/corpus.p", "rb"))
    W = cPickle.load(open("./data/word2vec.p", "rb"))
    W2V = np.array(W[0]).astype(theano.config.floatX)

    train_X, train_Y = data[0], data[1]
    train_x = [np.matrix(W2V[sen_idx]) for sen_idx in train_X]
    train_y = [idx2onehot(label, out_dim) for l in train_Y for label in l]

    model = VanillaRNN(n_in=in_dim, n_out=out_dim, n_hidden=hidden_dim)
    model.build_model()
    t0 = datetime.now()
    model.f_update(train_x[0], train_y[0], 0.01, 0.5)
    print model.by.get_value().shape
    print "Elapsed time: %f" % ((datetime.now() - t0).microseconds / 1000.0)


示例#30
0
 def encode(self, x, y, label, alpha):
     y = idx2onehot(y, 10, label, alpha)
     con = torch.cat((x, y), dim=-1)
     h1 = self.relu(self.fc1(con))
     return self.fc21(h1), self.fc22(h1)
示例#31
0
                        logger.info(
                            'epoch %i, seq %i/%i, tr loss %f te loss %f lr: %f'
                            % (epoch + 1, idx + 1, n_train, train_loss,
                               validation_loss, learning_rate))
                    else:
                        logger.info(
                            'epoch %i, seq %i/%i, train loss %f lr: %f' %
                            (epoch + 1, idx + 1, n_train, train_loss,
                             learning_rate))

            learning_rate *= learning_rate_decay


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    in_dim, hidden_dim, out_dim = 300, 128, 3
    data = cPickle.load(open("./data/corpus.p", "rb"))
    W = cPickle.load(open("./data/word2vec.p", "rb"))
    W2V = np.array(W[0]).astype(theano.config.floatX)

    train_X, train_Y = data[0], data[1]
    train_x = [np.matrix(W2V[sen_idx]) for sen_idx in train_X]
    train_y = [idx2onehot(label, out_dim) for l in train_Y for label in l]

    model = VanillaRNN(n_in=in_dim, n_out=out_dim, n_hidden=hidden_dim)
    model.build_model()
    t0 = datetime.now()
    model.f_update(train_x[0], train_y[0], 0.01, 0.5)
    print model.by.get_value().shape
    print "Elapsed time: %f" % ((datetime.now() - t0).microseconds / 1000.0)