Returns a tensor filled with random integers generated uniformly between The shape of the tensor is defined by the variable argument Note With the global dtype default [ low [int, optional] – Lowest integer to be drawn from the distribution. Default: 0. high
[int] – One above the highest integer to be drawn from the distribution. size [tuple] – a tuple defining the shape of the output tensor.torch.
randint
[low=0, high, size, \*, generator=None, out=None, dtype=None, layout=torch.strided,
device=None, requires_grad=False] → Tensor¶low
[inclusive] and high
[exclusive].size
.torch.float32
], this function returns a tensor with dtype torch.int64
.
generator [
torch.Generator
, optional] – a pseudorandom number generator for samplingout [Tensor, optional] – the output tensor.
dtype [torch.dtype, optional] – if
None
, this function returns a tensor with dtypetorch.int64
.layout [
torch.layout
, optional] – the desired layout of returned Tensor. Default:torch.strided
.device [
torch.device
, optional] – the desired device of returned tensor. Default: ifNone
, uses the current device for the default tensor type [seetorch.set_default_tensor_type[]
].device
will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.requires_grad [bool, optional] – If autograd should record operations on the returned tensor. Default:
False
.
Example:
>>> torch.randint[3, 5, [3,]] tensor[[4, 3, 4]] >>> torch.randint[10, [2, 2]] tensor[[[0, 2], [5, 5]]] >>> torch.randint[3, 10, [2, 2]] tensor[[[4, 5], [6, 7]]]
The following are 30 code examples of torch.randint[]. 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, or try the search function
Example #1
def get_batch[source, i, train]: if train: i = torch.randint[low=0, high=[len[source] - args.bptt], size=[1,]].long[].item[] seq_len = args.bptt target = source[i + 1:i + 1 + seq_len].t[] else: seq_len = min[args.bptt, len[source] - 1 - i] target = source[i + seq_len, :] data = source[i:i + seq_len].t[] data_mask = [data != pad].unsqueeze[-2] target_mask = make_std_mask[data.long[]] # reshape target to match what cross_entropy expects target = target.contiguous[].view[-1] return data, target, data_mask, target_mask
Example #2
def __init__[self, thresh=1e-8, projDim=8192, input_dim=512]: super[CBP, self].__init__[] self.thresh = thresh self.projDim = projDim self.input_dim = input_dim self.output_dim = projDim torch.manual_seed[1] self.h_ = [ torch.randint[0, self.output_dim, [self.input_dim,],dtype=torch.long], torch.randint[0, self.output_dim, [self.input_dim,],dtype=torch.long] ] self.weights_ = [ [2 * torch.randint[0, 2, [self.input_dim,]] - 1].float[], [2 * torch.randint[0, 2, [self.input_dim,]] - 1].float[] ] indices1 = torch.cat[[torch.arange[input_dim, dtype=torch.long].reshape[1, -1], self.h_[0].reshape[1, -1]], dim=0] indices2 = torch.cat[[torch.arange[input_dim, dtype=torch.long].reshape[1, -1], self.h_[1].reshape[1, -1]], dim=0] self.sparseM = [ torch.sparse.FloatTensor[indices1, self.weights_[0], torch.Size[[self.input_dim, self.output_dim]]].to_dense[], torch.sparse.FloatTensor[indices2, self.weights_[1], torch.Size[[self.input_dim, self.output_dim]]].to_dense[], ]
Example #3
def test_adam_poincare[params]: torch.manual_seed[44] manifold = geoopt.PoincareBall[] ideal = manifold.random[10, 2] start = manifold.random[10, 2] start = geoopt.ManifoldParameter[start, manifold=manifold] def closure[]: idx = torch.randint[10, size=[3,]] start_select = torch.nn.functional.embedding[idx, start, sparse=True] ideal_select = torch.nn.functional.embedding[idx, ideal, sparse=True] optim.zero_grad[] loss = manifold.dist2[start_select, ideal_select].sum[] loss.backward[] assert start.grad.is_sparse return loss.item[] optim = geoopt.optim.SparseRiemannianSGD[[start], **params] for _ in range[2000]: optim.step[closure] np.testing.assert_allclose[start.data, ideal, atol=1e-5, rtol=1e-5]
Example #4
def test_adam_poincare[params]: torch.manual_seed[44] manifold = geoopt.PoincareBall[] ideal = manifold.random[10, 2] start = manifold.random[10, 2] start = geoopt.ManifoldParameter[start, manifold=manifold] def closure[]: idx = torch.randint[10, size=[3,]] start_select = torch.nn.functional.embedding[idx, start, sparse=True] ideal_select = torch.nn.functional.embedding[idx, ideal, sparse=True] optim.zero_grad[] loss = manifold.dist2[start_select, ideal_select].sum[] loss.backward[] assert start.grad.is_sparse return loss.item[] optim = geoopt.optim.SparseRiemannianAdam[[start], **params] for _ in range[2000]: optim.step[closure] np.testing.assert_allclose[start.data, ideal, atol=1e-5, rtol=1e-5]
Example #5
def neg_sample[self, batch]: batch = batch.repeat[self.walks_per_node * self.num_negative_samples] rws = [batch] for i in range[self.walk_length]: keys = self.metapath[i % len[self.metapath]] batch = torch.randint[0, self.num_nodes_dict[keys[-1]], [batch.size[0], ], dtype=torch.long] rws.append[batch] rw = torch.stack[rws, dim=-1] rw.add_[self.offset.view[1, -1]] walks = [] num_walks_per_rw = 1 + self.walk_length + 1 - self.context_size for j in range[num_walks_per_rw]: walks.append[rw[:, j:j + self.context_size]] return torch.cat[walks, dim=0]
Example #6
def test_gnn_explainer[]: model = Net[] explainer = GNNExplainer[model, log=False] assert explainer.__repr__[] == 'GNNExplainer[]' x = torch.randn[8, 3] edge_index = torch.tensor[[[0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7], [1, 0, 2, 1, 3, 2, 4, 3, 5, 4, 6, 5, 7, 6]]] y = torch.randint[0, 6, [8, ], dtype=torch.long] node_feat_mask, edge_mask = explainer.explain_node[2, x, edge_index] assert node_feat_mask.size[] == [x.size[1], ] assert node_feat_mask.min[] >= 0 and node_feat_mask.max[] = 0 and edge_mask.max[] 0: # pragma: no cover tmp = torch.randint[num_nodes, [rest.numel[], ], dtype=torch.long] idx_2 = i[rest] * num_nodes + tmp mask = torch.from_numpy[np.isin[idx_2, idx_1]].to[torch.bool] k[rest] = tmp rest = rest[mask.nonzero[].view[-1]] return edge_index[0], edge_index[1], k.to[edge_index.device]