Python torch.cuda.FloatTensor() Examples
The following are 20
code examples of torch.cuda.FloatTensor().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch.cuda
, or try the search function
.
Example #1
Source File: network.py From pytorch-vfi-cft with GNU General Public License v3.0 | 6 votes |
def interpolation(self, uvm, image, index): u, v = torch.index_select(uvm, dim=1, index=LongTensor([0+3*index, 1+3*index])).permute(0, 2, 3, 1).split(1, dim=3) row_num = FloatTensor() col_num = FloatTensor() im_size = image.shape[2:4] torch.arange(im_size[0], out=row_num) torch.arange(im_size[1], out=col_num) row_num = row_num.view(1, im_size[0], 1, 1) col_num = col_num.view(1, 1, im_size[1], 1) x_norm = 2*(u+col_num)/(im_size[1]-1)-1 y_norm = 2*(v+row_num)/(im_size[0]-1)-1 xy_norm = torch.clamp(torch.cat((x_norm, y_norm), dim=3), -1, 1) interp = nn.functional.grid_sample(image, xy_norm) w = torch.index_select(uvm, dim=1, index=LongTensor([3*index+2]))+0.5 return interp, w, u, v
Example #2
Source File: visual.py From pytorch-deep-generative-replay with MIT License | 6 votes |
def visualize_image(tensor, name, label=None, env='main', w=250, h=250, update_window_without_label=False): tensor = tensor.cpu() if isinstance(tensor, CUDATensor) else tensor title = name + ('-{}'.format(label) if label is not None else '') _WINDOW_CASH[title] = _vis(env).image( tensor.numpy(), win=_WINDOW_CASH.get(title), opts=dict(title=title, width=w, height=h) ) # This is useful when you want to maintain the most recent images. if update_window_without_label: _WINDOW_CASH[name] = _vis(env).image( tensor.numpy(), win=_WINDOW_CASH.get(name), opts=dict(title=name, width=w, height=h) )
Example #3
Source File: visual.py From pytorch-deep-generative-replay with MIT License | 6 votes |
def visualize_images(tensor, name, label=None, env='main', w=400, h=400, update_window_without_label=False): tensor = tensor.cpu() if isinstance(tensor, CUDATensor) else tensor title = name + ('-{}'.format(label) if label is not None else '') _WINDOW_CASH[title] = _vis(env).images( tensor.numpy(), win=_WINDOW_CASH.get(title), nrow=6, opts=dict(title=title, width=w, height=h) ) # This is useful when you want to maintain the most recent images. if update_window_without_label: _WINDOW_CASH[name] = _vis(env).images( tensor.numpy(), win=_WINDOW_CASH.get(name), nrow=6, opts=dict(title=name, width=w, height=h) )
Example #4
Source File: visual.py From pytorch-ewc with MIT License | 6 votes |
def visualize_images(vis, tensor, name, label=None, w=250, h=250, update_window_without_label=False): tensor = tensor.cpu() if isinstance(tensor, CUDATensor) else tensor title = name + ('-{}'.format(label) if label is not None else '') _WINDOW_CASH[title] = vis.images( tensor.numpy(), win=_WINDOW_CASH.get(title), opts=dict(title=title, width=w, height=h) ) # This is useful when you want to maintain the most recent images. if update_window_without_label: _WINDOW_CASH[name] = vis.images( tensor.numpy(), win=_WINDOW_CASH.get(name), opts=dict(title=name, width=w, height=h) )
Example #5
Source File: visual.py From pytorch-ewc with MIT License | 6 votes |
def visualize_image(vis, tensor, name, label=None, w=250, h=250, update_window_without_label=False): tensor = tensor.cpu() if isinstance(tensor, CUDATensor) else tensor title = name + ('-{}'.format(label) if label is not None else '') _WINDOW_CASH[title] = vis.image( tensor.numpy(), win=_WINDOW_CASH.get(title), opts=dict(title=title, width=w, height=h) ) # This is useful when you want to maintain the most recent images. if update_window_without_label: _WINDOW_CASH[name] = vis.image( tensor.numpy(), win=_WINDOW_CASH.get(name), opts=dict(title=name, width=w, height=h) )
Example #6
Source File: util_funcs.py From PointCNN.Pytorch with MIT License | 6 votes |
def knn_indices_func_cpu(rep_pts : FloatTensor, # (N, pts, dim) pts : FloatTensor, # (N, x, dim) K : int, D : int ) -> LongTensor: # (N, pts, K) """ CPU-based Indexing function based on K-Nearest Neighbors search. :param rep_pts: Representative points. :param pts: Point cloud to get indices from. :param K: Number of nearest neighbors to collect. :param D: "Spread" of neighboring points. :return: Array of indices, P_idx, into pts such that pts[n][P_idx[n],:] is the set k-nearest neighbors for the representative points in pts[n]. """ rep_pts = rep_pts.data.numpy() pts = pts.data.numpy() region_idx = [] for n, p in enumerate(rep_pts): P_particular = pts[n] nbrs = NearestNeighbors(D*K + 1, algorithm = "ball_tree").fit(P_particular) indices = nbrs.kneighbors(p)[1] region_idx.append(indices[:,1::D]) region_idx = torch.from_numpy(np.stack(region_idx, axis = 0)) return region_idx
Example #7
Source File: visual.py From pytorch-ewc with MIT License | 5 votes |
def visualize_scalars(vis, scalars, names, title, iteration): assert len(scalars) == len(names) # Convert scalar tensors to numpy arrays. scalars, names = list(scalars), list(names) scalars = [s.cpu() if isinstance(s, CUDATensor) else s for s in scalars] scalars = [s.detach().numpy() if hasattr(s, 'numpy') else np.array([s]) for s in scalars] multi = len(scalars) > 1 num = len(scalars) options = dict( fillarea=True, legend=names, width=400, height=400, xlabel='Iterations', ylabel=title, title=title, marginleft=30, marginright=30, marginbottom=80, margintop=30, ) X = ( np.column_stack(np.array([iteration] * num)) if multi else np.array([iteration] * num) ) Y = np.column_stack(scalars) if multi else scalars[0] if title in _WINDOW_CASH: vis.line( X=X, Y=Y, win=_WINDOW_CASH[title], opts=options, update='append' ) else: _WINDOW_CASH[title] = vis.line(X=X, Y=Y, opts=options)
Example #8
Source File: loss_functions.py From pytorch-vfi-cft with GNU General Public License v3.0 | 5 votes |
def __init__(self): super(VGG, self).__init__() vgg = vgg19(pretrained=True) self.vgg_mean = FloatTensor([[[[0.485]], [[0.456]], [[0.406]]]]) self.vgg_std = FloatTensor([[[[0.229]], [[0.224]], [[0.225]]]]) self.vgg_relu4_4 = vgg.features[:27]
Example #9
Source File: visual.py From pytorch-ewc with MIT License | 5 votes |
def visualize_kernel(vis, kernel, name, label=None, w=250, h=250, update_window_without_label=False, compress_tensor=False): # Do not visualize kernels that does not exists. if kernel is None: return assert len(kernel.size()) in (2, 4) title = name + ('-{}'.format(label) if label is not None else '') kernel = kernel.cpu() if isinstance(kernel, CUDATensor) else kernel kernel_norm = kernel if len(kernel.size()) == 2 else ( (kernel**2).mean(-1).mean(-1) if compress_tensor else kernel.view( kernel.size()[0] * kernel.size()[2], kernel.size()[1] * kernel.size()[3], ) ) kernel_norm = kernel_norm.abs() visualized = ( (kernel_norm - kernel_norm.min()) / (kernel_norm.max() - kernel_norm.min()) ).numpy() _WINDOW_CASH[title] = vis.image( visualized, win=_WINDOW_CASH.get(title), opts=dict(title=title, width=w, height=h) ) # This is useful when you want to maintain the most recent images. if update_window_without_label: _WINDOW_CASH[name] = vis.image( visualized, win=_WINDOW_CASH.get(name), opts=dict(title=name, width=w, height=h) )
Example #10
Source File: model.py From SAE-NAD with MIT License | 5 votes |
def __init__(self, D_in, H, D_out, da=20, dropout_rate=0.5): """ Initialize the model configurations and parameters. In the model, the network structure is like [D_in, H1, H, H1, D_out], and D_in equals to D_out. :param D_in: the dimension of the input :param H: the dimension of the bottleneck layer :param D_out: the dimension of the output :param H1: the dimension of the first hidden layer :param da: the dimension of the attention model """ super(AutoEncoder, self).__init__() self.H1 = H[0] self.H = H[1] self.dropout_rate = dropout_rate if torch.cuda.is_available(): self.linear1 = torch.nn.Linear(D_in, self.H1, bias=False).cuda() self.linear2 = torch.nn.Linear(self.H1, self.H).cuda() self.linear3 = torch.nn.Linear(self.H, self.H1).cuda() self.linear4 = torch.nn.Linear(self.H1, D_out).cuda() self.attention_matrix1 = Variable(torch.zeros(da, self.H1).type(T.FloatTensor), requires_grad=True) # self.attention_matrix2 = Variable(torch.zeros(20, 30).type(T.FloatTensor), requires_grad=True) self.attention_matrix1 = torch.nn.init.xavier_uniform_(self.attention_matrix1) # self.attention_matrix2 = torch.nn.init.xavier_uniform(self.attention_matrix2) self.self_attention = torch.nn.Linear(da, 1).cuda() else: self.linear1 = torch.nn.Linear(D_in, self.H1, bias=False) self.linear2 = torch.nn.Linear(self.H1, self.H) self.linear3 = torch.nn.Linear(self.H, self.H1) self.linear4 = torch.nn.Linear(self.H1, D_out) self.attention_matrix1 = Variable(torch.zeros(da, self.H1).type(T.FloatTensor), requires_grad=True) # self.attention_matrix2 = Variable(torch.zeros(20, 30).type(T.FloatTensor), requires_grad=True) self.attention_matrix1 = torch.nn.init.xavier_uniform_(self.attention_matrix1) # self.attention_matrix2 = torch.nn.init.xavier_uniform(self.attention_matrix2) self.self_attention = torch.nn.Linear(da, 1)
Example #11
Source File: model.py From GATE with MIT License | 5 votes |
def forward(self, batch_item_index, batch_x, batch_word_seq, batch_neighbor_index): z_1 = F.tanh(self.linear1(batch_x)) # z_1 = F.dropout(z_1, self.drop_rate) z_rating = F.tanh(self.linear2(z_1)) z_content = self.get_content_z(batch_word_seq) gate = F.sigmoid(z_rating.mm(self.gate_matrix1) + z_content.mm(self.gate_matrix2) + self.gate_bias) gated_embedding = gate * z_rating + (1 - gate) * z_content # save the embedding for direct lookup self.item_gated_embedding.weight[batch_item_index] = gated_embedding.data gated_neighbor_embedding = self.item_gated_embedding(batch_neighbor_index) # aug_gated_embedding: [256, 1, 50] aug_gated_embedding = torch.unsqueeze(gated_embedding, 1) score = torch.matmul(aug_gated_embedding, torch.unsqueeze(self.neighbor_attention, 0)) # score: [256, 1, 480] score = torch.bmm(score, gated_neighbor_embedding.permute(0, 2, 1)) # make the 0 in score, which will make a difference in softmax score = torch.where(score == 0, T.FloatTensor([float('-inf')]), score) score = F.softmax(score, dim=2) # if the vectors all are '-inf', softmax will generate 'nan', so replace with 0 score = torch.where(score != score, T.FloatTensor([0]), score) gated_neighbor_embedding = torch.bmm(score, gated_neighbor_embedding) gated_neighbor_embedding = torch.squeeze(gated_neighbor_embedding, 1) # gated_embedding = F.dropout(gated_embedding, self.drop_rate) # gated_neighbor_embedding = F.dropout(gated_neighbor_embedding, self.drop_rate) z_3 = F.tanh(self.linear3(gated_embedding)) # z_3 = F.dropout(z_3, self.drop_rate) z_3_neighbor = F.tanh(self.linear3(gated_neighbor_embedding)) # z_3_neighbor = F.dropout(z_3_neighbor, self.drop_rate) y_pred = F.sigmoid(self.linear4(z_3) + z_3_neighbor.mm(self.linear4.weight.t())) return y_pred
Example #12
Source File: util_funcs.py From PointCNN.Pytorch with MIT License | 5 votes |
def knn_indices_func_gpu(rep_pts : cuda.FloatTensor, # (N, pts, dim) pts : cuda.FloatTensor, # (N, x, dim) k : int, d : int ) -> cuda.LongTensor: # (N, pts, K) """ GPU-based Indexing function based on K-Nearest Neighbors search. Very memory intensive, and thus unoptimal for large numbers of points. :param rep_pts: Representative points. :param pts: Point cloud to get indices from. :param K: Number of nearest neighbors to collect. :param D: "Spread" of neighboring points. :return: Array of indices, P_idx, into pts such that pts[n][P_idx[n],:] is the set k-nearest neighbors for the representative points in pts[n]. """ region_idx = [] for n, qry in enumerate(rep_pts): ref = pts[n] n, d = ref.size() m, d = qry.size() mref = ref.expand(m, n, d) mqry = qry.expand(n, m, d).transpose(0, 1) dist2 = torch.sum((mqry - mref)**2, 2).squeeze() _, inds = torch.topk(dist2, k*d + 1, dim = 1, largest = False) region_idx.append(inds[:,1::d]) region_idx = torch.stack(region_idx, dim = 0) return region_idx
Example #13
Source File: util_funcs.py From PointCNN with MIT License | 5 votes |
def knn_indices_func_gpu(rep_pts : cuda.FloatTensor, # (N, pts, dim) pts : cuda.FloatTensor, # (N, x, dim) K : int, D : int ) -> cuda.LongTensor: # (N, pts, K) """ GPU-based Indexing function based on K-Nearest Neighbors search. Very memory intensive, and thus unoptimal for large numbers of points. :param rep_pts: Representative points. :param pts: Point cloud to get indices from. :param K: Number of nearest neighbors to collect. :param D: "Spread" of neighboring points. :return: Array of indices, P_idx, into pts such that pts[n][P_idx[n],:] is the set k-nearest neighbors for the representative points in pts[n]. """ region_idx = [] for n, qry in enumerate(rep_pts): qry = qry.half() ref = pts[n].half() r_A = torch.sum(qry * qry, dim = 1, keepdim = True) r_B = torch.sum(ref * ref, dim = 1, keepdim = True) dist2 = r_A - 2 * torch.matmul(qry, torch.t(ref)) + torch.t(r_B) _, inds = torch.topk(dist2, D*K + 1, dim = 1, largest = False) region_idx.append(inds[:,1::D]) region_idx = torch.stack(region_idx, dim = 0) return region_idx
Example #14
Source File: util_funcs.py From PointCNN with MIT License | 5 votes |
def knn_indices_func_approx(rep_pts : FloatTensor, # (N, pts, dim) pts : FloatTensor, # (N, x, dim) K : int, D : int ) -> LongTensor: # (N, pts, K) """ Approximate CPU-based Indexing function based on K-Nearest Neighbors search. :param rep_pts: Representative points. :param pts: Point cloud to get indices from. :param K: Number of nearest neighbors to collect. :param D: "Spread" of neighboring points. :return: Array of indices, P_idx, into pts such that pts[n][P_idx[n],:] is the set k-nearest neighbors for the representative points in pts[n]. """ if rep_pts.is_cuda: rep_pts = rep_pts.cpu() if pts.is_cuda: pts = pts.cpu() rep_pts = rep_pts.data.numpy() pts = pts.data.numpy() region_idx = [] for n, p in enumerate(rep_pts): P_particular = pts[n] lshf = LSHForest(n_estimators = 20, n_candidates = 100, n_neighbors = D*K + 1) lshf.fit(P_particular) indices = lshf.kneighbors(p, return_distance = False) region_idx.append(indices[:,1::D])
Example #15
Source File: util_funcs.py From PointCNN with MIT License | 5 votes |
def knn_indices_func_cpu(rep_pts : FloatTensor, # (N, pts, dim) pts : FloatTensor, # (N, x, dim) K : int, D : int ) -> LongTensor: # (N, pts, K) """ CPU-based Indexing function based on K-Nearest Neighbors search. :param rep_pts: Representative points. :param pts: Point cloud to get indices from. :param K: Number of nearest neighbors to collect. :param D: "Spread" of neighboring points. :return: Array of indices, P_idx, into pts such that pts[n][P_idx[n],:] is the set k-nearest neighbors for the representative points in pts[n]. """ if rep_pts.is_cuda: rep_pts = rep_pts.cpu() if pts.is_cuda: pts = pts.cpu() rep_pts = rep_pts.data.numpy() pts = pts.data.numpy() region_idx = [] for n, p in enumerate(rep_pts): P_particular = pts[n] nbrs = NearestNeighbors(D*K + 1, algorithm = "auto").fit(P_particular) indices = nbrs.kneighbors(p)[1] region_idx.append(indices[:,1::D]) region_idx = torch.from_numpy(np.stack(region_idx, axis = 0)) return region_idx
Example #16
Source File: visual.py From pytorch-deep-generative-replay with MIT License | 5 votes |
def visualize_scalars(scalars, names, title, iteration, env='main'): assert len(scalars) == len(names) # Convert scalar tensors to numpy arrays. scalars, names = list(scalars), list(names) scalars = [s.cpu() if isinstance(s, CUDATensor) else s for s in scalars] scalars = [s.numpy() if hasattr(s, 'numpy') else np.array([s]) for s in scalars] multi = len(scalars) > 1 num = len(scalars) options = dict( fillarea=True, legend=names, width=400, height=400, xlabel='Iterations', ylabel=title, title=title, marginleft=30, marginright=30, marginbottom=80, margintop=30, ) X = ( np.column_stack(np.array([iteration] * num)) if multi else np.array([iteration] * num) ) Y = np.column_stack(scalars) if multi else scalars[0] if title in _WINDOW_CASH: _vis(env).updateTrace(X=X, Y=Y, win=_WINDOW_CASH[title], opts=options) else: _WINDOW_CASH[title] = _vis(env).line(X=X, Y=Y, opts=options)
Example #17
Source File: visual.py From pytorch-deep-generative-replay with MIT License | 5 votes |
def visualize_kernel(kernel, name, label=None, env='main', w=250, h=250, update_window_without_label=False, compress_tensor=False): # Do not visualize kernels that does not exists. if kernel is None: return assert len(kernel.size()) in (2, 4) title = name + ('-{}'.format(label) if label is not None else '') kernel = kernel.cpu() if isinstance(kernel, CUDATensor) else kernel kernel_norm = kernel if len(kernel.size()) == 2 else ( (kernel**2).mean(-1).mean(-1) if compress_tensor else kernel.view( kernel.size()[0] * kernel.size()[2], kernel.size()[1] * kernel.size()[3], ) ) kernel_norm = kernel_norm.abs() visualized = ( (kernel_norm - kernel_norm.min()) / (kernel_norm.max() - kernel_norm.min()) ).numpy() _WINDOW_CASH[title] = _vis(env).image( visualized, win=_WINDOW_CASH.get(title), opts=dict(title=title, width=w, height=h) ) # This is useful when you want to maintain the most recent images. if update_window_without_label: _WINDOW_CASH[name] = _vis(env).image( visualized, win=_WINDOW_CASH.get(name), opts=dict(title=name, width=w, height=h) )
Example #18
Source File: loss_functions.py From pytorch-vfi-cft with GNU General Public License v3.0 | 5 votes |
def forward(self, input): vgg_mean = FloatTensor([[[[0.485]], [[0.456]], [[0.406]]]]) vgg_std = FloatTensor([[[[0.229]], [[0.224]], [[0.225]]]]) return self.vgg_relu4_4((input-vgg_mean)/vgg_std)
Example #19
Source File: run.py From GATE with MIT License | 4 votes |
def evaluate_model(train_matrix, test_set, item_word_seq, item_neighbor_index, GAT, batch_size): num_items, num_users = train_matrix.shape num_batches = int(num_items / batch_size) + 1 item_indexes = np.arange(num_items) pred_matrix = None for batchID in range(num_batches): start = batchID * batch_size end = start + batch_size if batchID == num_batches - 1: if start < num_items: end = num_items else: break batch_item_index = item_indexes[start:end] # get mini-batch data batch_x = train_matrix[batch_item_index].toarray() batch_word_seq = item_word_seq[batch_item_index] batch_neighbor_index = item_neighbor_index[batch_item_index] batch_item_index = Variable(torch.from_numpy(batch_item_index).type(T.LongTensor), requires_grad=False) batch_word_seq = Variable(torch.from_numpy(batch_word_seq).type(T.LongTensor), requires_grad=False) batch_neighbor_index = Variable(torch.from_numpy(batch_neighbor_index).type(T.LongTensor), requires_grad=False) batch_x = Variable(torch.from_numpy(batch_x.astype(np.float32)).type(T.FloatTensor), requires_grad=False) # Forward pass: Compute predicted y by passing x to the model rating_pred = GAT(batch_item_index, batch_x, batch_word_seq, batch_neighbor_index) rating_pred = rating_pred.cpu().data.numpy().copy() if batchID == 0: pred_matrix = rating_pred.copy() else: pred_matrix = np.append(pred_matrix, rating_pred, axis=0) topk = 50 pred_matrix[train_matrix.nonzero()] = 0 pred_matrix = pred_matrix.transpose() # reference: https://stackoverflow.com/a/23734295, https://stackoverflow.com/a/20104162 ind = np.argpartition(pred_matrix, -topk) ind = ind[:, -topk:] arr_ind = pred_matrix[np.arange(len(pred_matrix))[:, None], ind] arr_ind_argsort = np.argsort(arr_ind)[np.arange(len(pred_matrix)), ::-1] pred_list = ind[np.arange(len(pred_matrix))[:, None], arr_ind_argsort] precision, recall, MAP, ndcg = [], [], [], [] for k in [5, 10, 15, 20, 30, 40, 50]: precision.append(precision_at_k(test_set, pred_list, k)) recall.append(recall_at_k(test_set, pred_list, k)) MAP.append(mapk(test_set, pred_list, k)) ndcg.append(ndcg_k(test_set, pred_list, k)) return precision, recall, MAP, ndcg
Example #20
Source File: model.py From SAE-NAD with MIT License | 4 votes |
def forward(self, batch_item_index, place_correlation): """ The forward pass of the autoencoder. :param batch_item_index: a list of arrays that each array stores the place id a user has been to :param place_correlation: the pairwise poi relation matrix :return: the predicted ratings """ item_vector = self.linear1.weight[:, T.LongTensor(batch_item_index[0].astype(np.int32))] # Compute the neighbor inner products inner_product = item_vector.t().mm(self.linear4.weight.t()) item_corr = Variable( torch.from_numpy(place_correlation[batch_item_index[0]].toarray()).type(T.FloatTensor)) inner_product = inner_product * item_corr neighbor_product = inner_product.sum(dim=0).unsqueeze(0) # Compute the self attention score score = F.tanh(self.attention_matrix1.mm(item_vector)) score = F.softmax(score, dim=1) embedding_matrix = score.mm(item_vector.t()) linear_z = self.self_attention(embedding_matrix.t()).t() # print score for i in range(1, len(batch_item_index)): item_vector = self.linear1.weight[:, T.LongTensor(batch_item_index[i].astype(np.int32))] # Compute the neighbor inner products inner_product = item_vector.t().mm(self.linear4.weight.t()) item_corr = Variable( torch.from_numpy(place_correlation[batch_item_index[i]].toarray()).type(T.FloatTensor)) inner_product = inner_product * item_corr inner_product = inner_product.sum(dim=0).unsqueeze(0) neighbor_product = torch.cat((neighbor_product, inner_product), 0) # Compute the self attention score score = F.tanh(self.attention_matrix1.mm(item_vector)) score = F.softmax(score, dim=1) embedding_matrix = score.mm(item_vector.t()) tmp_z = self.self_attention(embedding_matrix.t()).t() linear_z = torch.cat((linear_z, tmp_z), 0) z = F.tanh(linear_z) z = F.dropout(z, training=self.training, p=self.dropout_rate) z = F.tanh(self.linear2(z)) z = F.dropout(z, training=self.training, p=self.dropout_rate) d_z = F.tanh(self.linear3(z)) d_z = F.dropout(d_z, training=self.training, p=self.dropout_rate) y_pred = F.sigmoid(self.linear4(d_z) + neighbor_product) return y_pred