山内セミナーⅠ(2020/07/22)work

Defining the tensor data type

import torch
  
x = torch.ones(2, 2)
print(x)
print(x.dtype)
    

import torch
  
x = torch.ones(2, 2, dtype=torch.uint8)
print(x)
print(x.dtype)
    

Changing the tensor's data type

import torch
   
x = torch.ones(1, dtype=torch.uint8)
print(x.dtype)
  
x = x.type(torch.float)
print(x.dtype)
    

Converting tensors into NumPy arrays

import torch
import numpy as np
   
x = torch.rand(2, 2)
print(x)
print(x.dtype)
   
y = x.numpy()
print(y)
print(y.dtype)
    

Converting NumPy arrays into tensors

import torch
import numpy as np
   
x = np.zeros((2, 2),dtype=np.float32)
print(x)
print(x.dtype)
   
y = torch.from_numpy(x)
print(y)
print(y.dtype)