Ejemplo n.º 1
0
def stn_transform(output, input):
    imrec = input
    m = torch.size(output, 0)
    n = torch.size(output, 1)
    for i in range(m):
        for j in range(n):
            if ((abs(output[i][j][0]) + abs(output[i][j][1]) != 0)):
                imrec[i + math.floor(output[i][j][0])][
                    j + math.floor(output[i][j][1])] = input[i][j]
    return imrec
Ejemplo n.º 2
0
def tenmat(X, rdim, as_np=True, force_sparse=False):
    # return the mode-rdim matricization of input,
    # i.e. the rows of the output are the result of flattening along
    # mode rdims
    ndims = X.dim()
    Xshape = np.array(X.shape)
    cdims = np.flatnonzero(np.arange(ndims) != rdim)
    if X.is_sparse:
        indices = np.array(X.coalesce().indices())
        ridx = indices[rdim, :]
        csize = Xshape[cdims]
        cidx = np.ravel_multi_index(indices[cdims, :], csize)
        if as_np:
            Y = coo_matrix((X.values(), (ridx, cidx)))
        elif not as_np:
            new_inds = np.vstack([ridx, cidx])
            Y = torch.sparse.FloatTensor(
                new_inds, X.values(),
                torch.size([Xshape[rdim], np.prod(csize)]))
    else:
        Y = X.permute(rdim, *cdims).reshape(Xshape[rdim],
                                            np.prod(np.array(Xshape)[cdims]))
        if as_np:
            Y = np.array(Y)
            if force_sparse:
                Y = coo_matrix(Y)
        else:
            if force_sparse:
                Y = Y.to_sparse()
    return Y
Ejemplo n.º 3
0
 def update_active(t):
     view = t.data.view(-1, remaining_sents,
                        self.params.decoder_rnn_size)
     new_size = list(t.size())
     new_size[-2] = new_size[-2] * len(active_idx) \
                    // remaining_sents
     return Variable(
         view.index_select(1, active_idx).view(*new_size))
Ejemplo n.º 4
0
def get_size_mult(x):
    res = 1
    """
    this is after passing by the max pooling layer
    this is to pass the array to a flat version 
    """
    for dim in x.dim():
        res = res * torch.size()[dim]
Ejemplo n.º 5
0
 def forward(self, t_img1, t_img2):
     t_pyr1 = self.make_laplacian_pyramid(t_img1, self.max_levels)
     t_pyr2 = self.make_laplacian_pyramid(t_img2, self.max_levels)
     t_losses = [
         torch.norm(a - b, ord=1) / torch.size(a, out_type=torch.float32)
         for a, b in zip(t_pyr1, t_pyr2)
     ]
     t_loss = torch.sum(t_losses) * torch.shape(t_img1,
                                                out_type=torch.float32)[0]
     return t_loss
Ejemplo n.º 6
0
    def __init__(self, input_dim, output_dim, weights=None):
        super(Layer, self).__init__()

        if weights is None:
            self.W = nn.Parameter(
                torch.randn(input_dim, output_dim) / np.sqrt(input_dim),
                requires_grad=True,
            )
        else:
            assert weights.shape == torch.size((input_dim, output_dim))
            self.W = nn.Parameter(weights)
Ejemplo n.º 7
0
def max_pool_3d(params, k):
    _, indices = torch.topk(params, k, sorted=False)
    shape = torch.size(indices)
    r1 = torch.reshape(torch.range(shape[0]), (-1, 1))
    r2 = torch.reshape(torch.range(shape[1]), (-1, 1))
    r1 = r1.repeat((1, k * shape[1]))
    r2 = r2.repeat((1, k))
    r1 = torch.reshape(r1, (-1, 1))
    r2 = torch.reshape(r2, (-1, 1)).repeat(shape[0], 1)
    indices = torch.cat((r1, r2, torch.reshape(indices, (-1, 1))), 1)
    return torch.reshape(gater_3d(params, indices), (-1, shape[1], k))
Ejemplo n.º 8
0
def get_surrogate_objective(s_model, qz_x, z, mask, types, n, rho, device):
    mu, sigma = s_model(z, mask, device)
    kld = []
    l_r = []
    for i in range(types):
        if mu[i] == -1:
            continue
        tmp_mask = torch.sum(mask == i, dim=-1).bool()
        tmp_mu = mu[i]  #k * D
        tmp_sigma = sigma[i]

        ri = dist.Normal(tmp_mu, tmp_sigma)
        z_ri = ri.rsample(torch.size([n]))  #n * K * D
        l_ri = ri.log_prob(z_ri).sum(-1)  #n*K
        l_qi = qz_x.log_prob(z_ri).sum(-1)  #n*K
        l_qi = torch.logsumexp(l_qi).expand_as(l_ri)  #n*K
        kld.append(torch.mean(l_ri - l_qi, dim=1))
        l_r.append(torch.mean(l_ri, dim=1))

    l_r = torch.logsumexp(torch.stack(l_r), dim=0)  #n
    kld = torch.sum(torch.mean(torch.stack(kld)))  #1
    obj = kld * rho - torch.sum(l_r)
    return obj, kld, l_r
Ejemplo n.º 9
0
    def forward(
        self,
        observations,
        graph: Graph,
        diffusion_graph: Graph,
        observed,
        starts,
        targets,
        pairwise_node_features,
        steps=None,
    ):
        
        assert observed.shape[0] == starts.shape[0] == targets.shape[0]
        n_pred = observed.shape[0]
        
        if self.diffusion_graph_transformer is None and self.direction_edge_mpl is None:
            
            if graph.edges.shape != torch.size([graph.n_edge, 1]) or ((graph.Out_nodes_counts -1.0).abs() > 1e-5).any():
                rw_graphs = graph.update(edges=torch.ones([graph.n_edge, n_pred], device = graph.device))
                
                rw_graphs = rw_graphs.c_weights()
        else:
                rw_graphs = graph
                virtual_coords = None
                virtual_coords = self.compute_diffusion(diffusion_graph, observations)
                
        if self.double_way_diffusion:
                
                virtual_coords_reversed = self.compute_diffusion(diffusion_graph.reversed_edges(), observations)
                    
                virtual_coords = torch.cat([virtual_coords, virtual_coord_reversed])

                rw_graphs - self.compute_rw_weights(virtual_coords, observed, pairwise_node_features, targets, graph)
                
                target_distributions = self.compute_random_walk(rw_graphs, observations, starts, targets, steps)
            
                return target_distributions, virtual_coords, rw_weights
Ejemplo n.º 10
0
def train(model, start):
    optimizer = optim.Adam(model.paramaters(), lr=1e-4)
    criterion = nn.MSELoss()

    game = Game()
    dino = Dinosaur(game)
    game_state = GameEnvironment(dino, game)

    replay_memory = []

    actions = torch.zeros([model.number_of_actions], dtype=torch.cuda.float32)
    action[0] = 1

    image_data, reward, terminal, score, high_score = game_state.get_next_state(action)
    
    epsilon = model.initial_epsilon
    iteration = 0    

    epsilon decrements = np.linspace(model.initial_epsilon, model.final_epsilon, model.number_of_iterations) 
    while iteration < model.number_of_iterations:
        output = model(image_data)
        
        action = torch.zeros([model.number_of_actions], dtype=torch.float32)

        action = transfer_to_cuda(action)
        
        random_action = random.random() <= epsilon
        action_index = [torch.randint(model.number_of_actions, torch.size([]), dtype = torch.int)
                        if random_action
                        else torch.argmax(output)][0]

        action_index = transfer_to_cuda(action_index)

        # Setting the action to 1 because you gotta jump first to start the game
        action[action_index] = 1

        image_data_1, reward, terminal, score, high_score = game_state.get_next_state(action)
Ejemplo n.º 11
0
def build_l1_loss_with_mask(x_t, x_o, mask):

    mask_ratio = 1. - mask.view(-1).sum() / torch.size(mask)
    l1 = torch.abs(x_t - x_o)
    return mask_ratio * torch.mean(l1 * mask) + (1. - mask_ratio) * torch.mean(
        l1 * (1. - mask))
Ejemplo n.º 12
0
    def LSTMCell(input, hidden, w_ih, w_hh, count, b_ih=None, b_hh=None):
        #        if input.is_cuda:
        #            igates = F.linear(input, w_ih)
        #            hgates = F.linear(hidden[0], w_hh)
        #            state = fusedBackend.LSTMFused.apply
        #            return state(igates, hgates, hidden[1]) if b_ih is None else state(igates, hgates, hidden[1], b_ih, b_hh)

        hx, cx = hidden
        gates = F.linear(input, w_ih, b_ih) + F.linear(hx, w_hh, b_hh)

        #       if count == 0:
        #            cellmemory(:,:,count) = cellgate

        if preset == False:
            ingate, forgetgate, cellgate, outgate, sensitivitygate_1, sensitivitygate_2, sensitivitygate_3 = gates.chunk(
                7, 1)

            ingate = F.sigmoid(ingate)
            forgetgate = F.sigmoid(forgetgate)
            sensitivitygate_1 = F.relu(sensitivitygate_1)
            sensitivitygate_2 = F.sigmoid(sensitivitygate_2)
            sensitivitygate_3 = F.relu(sensitivitygate_3).round()

            cellgatetemp = cellgate.unsqueeze_(2)
            #            cellgatetemp.expand(:,:,torch.size(cellmemory,2))

            #with sensitivity gates
            sensitivitygate_1 = sensitivitygate.unsqueeze_(2)
            #            sensitivitygate_1.expand(:,:,torch.size(cellmemory,2))
            sensitivitygate_2 = sensitivitygate.unsqueeze_(2)
            #            sensitivitygate_2.expand(:,:,torch.size(cellmemory,2))
            sensitivitygate_3 = sensitivitygate.unsqueeze_(2)
            #            sensitivitygate_3.expand(:,:,torch.size(cellmemory,2))
            direction = torch.pow(
                .0001 * sensitivitygate_1,
                2) * sensitivitygate_2 * (cellmemory - cellgatetemp)
            distance = torch.pow(.0001 * sensitivtygate_1, 2) + torch.pow(
                (cellmemory - cellgate), (2 * sensitivitygate_3))
        else:
            ingate, forgetgate, cellgate, outgate = gates.chunk(4, 1)

            ingate = F.sigmoid(ingate)
            forgetgate = F.sigmoid(forgetgate)
            cellgatetemp = cellgate.unsqueeze_(2)
            #            cellgatetemp.expand(:,:,torch.size(cellmemory,2))

            #with preset sensitivities
            #            a = #between 0 and infinity
            #            b = #between 0 and 1
            #            c = #between 0 and infinity
            direction = torch.pow(.0001 * a,
                                  2) * b * (cellmemory - cellgatetemp)
            distance = torch.pow(.0001 * a, 2) + torch.pow(
                (cellmemory - cellgate), (2 * c))

# direction = (a^2 * b * (x-z))
# distance = (a^2 + (x-z)^(4*c))
# clustergrad = (a^2 * b * (x-z))/(a^2 + (x-z)^(4*c))

        clustergrad = torch.sum(
            (direction / distance), 2) / torch.size(cellmemory, 2)
        cellgate = F.tanh(cellgate + clustergrad)
        #            cellmemory(:,:,count) = cellgate
        outgate = F.sigmoid(outgate)

        cy = (forgetgate * cx) + (ingate * cellgate)
        hy = outgate * F.tanh(cy)

        return hy, cy, cellmemory
Ejemplo n.º 13
0
 def input_shape(self) -> torch.Size:
     return torch.size(self._input_shape)