├── .gitignore ├── README.md └── tensors_1d └── geral.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.env 2 | *.db 3 | venv -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pytorch_Pratices 2 | -------------------------------------------------------------------------------- /tensors_1d/geral.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import numpy as np 3 | 4 | a = torch.tensor([0,1,2,3,4]) # criar tensor 5 | 6 | print(a.dtype) # ver tipo do conteudo do tensor 7 | print(a.type()) # ver tipo do tensor 8 | 9 | b = torch.tensor([0.0,1.0,2.0,3.0,4.0], dtype= torch.int32) # definir o tipo do tensor 10 | # ou 11 | c = torch.FloatTensor([0,1,2,3,4]) # para obter o mesmo resultado 12 | # definindo esse tipo ao rodar o tensor ajusta ao seu tipo 13 | 14 | print(b.type()) 15 | print(c.type()) 16 | 17 | # Se quiser muda o tipo do tensor so rodar 18 | 19 | d = torch.tensor([0,1,2,3,4]) 20 | d = d.type(torch.FloatTensorS) 21 | 22 | # ver quantos itens tem em um tensor 23 | 24 | print(d.size()) # output 5 25 | 26 | # ver quantos dimensoes o tensor possui 27 | 28 | print(d.ndimension()) # output 1 29 | 30 | # converter tensores 1d para 2d 31 | 32 | e = torch.Tensor([0,1,2,3,4,5]) 33 | 34 | e_col = e.view(6,1) #tamnanho x e y 35 | # ou 36 | e_col = e.view(-1,1) 37 | 38 | # convertendo np array para tensores 39 | 40 | numpy_array = np.array([0.0,1.0,2.0,3.0,4.0]) 41 | torch_tensor = torch.from_numpy(numpy_array) 42 | # converter de novo para numpy 43 | back_numpy=torch_tensor.numpy() --------------------------------------------------------------------------------