<jupyter_start><jupyter_text>__Ensuring that PyTorch is using GPU:__<jupyter_code>torch.cuda.current_device()
torch.cuda.device_count()
torch.cuda.get_device_name(0)<jupyter_output><empty_output><jupyter_text>__Unline tensorflow, pytorch doesn't utilize the ENTIRE gpu memory, allocation is based on the need__<jupyter_code>x = torch.empty(5, 3)
print(x) # near-zero values
x = torch.rand(3, 4) # random unif~[0,1]
print(x)
x = torch.zeros(5, 3, dtype=torch.long) # specify datatype , i.e., long
print(x)<jupyter_output>tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])<jupyter_text>__List or array to torch tensor__<jupyter_code>x = torch.tensor([[4.1, 2.3], [-1.8, 0.]], dtype=torch.float32)
print(x)
x = x.new_ones(3, 4) # a new tensor of ones with the same datatype as x
print(x)
x = x.new_full([3, 4], fill_value = 6.0)
print(x)
x = torch.rand_like(x) # random-unif, same dims and same datatype as x
print(x)
x.size()
x**2
x + x
y = torch.randn_like(x) # random normal
print(y)
z = torch.add(x, y) 
z = x.add(y)
torch.add(x, y, out=z) # store output in tensor z
print(z)
# in place addition - use understore '_'
y.add_(x) # y = y + x
y<jupyter_output><empty_output><jupyter_text>__Resize/reshape tensor, using view__<jupyter_code>x = torch.randn(4, 4)
x.size()
y = x.view([2, 8])
y.size()
z = x.view(-1, 16)  # the size -1 implies infer from other dimensions
z.size()<jupyter_output><empty_output><jupyter_text>The Torch Tensor and NumPy array __will share their underlying memory locations, and changing one will change the other.__<jupyter_code>a = torch.ones(5)
print(a)
b = a.numpy() # convert to numpy array
print(b)
a.mul_(3.) # modify torch tensor 'a', both variables are modified
print(a)
print(b)
import numpy as np
a = np.ones(7)
a
b = torch.from_numpy(a) # from numpy to torch tensor
print(b)
c = torch.tensor(a) # this way of conversion doesnt correspond to shared memory locations
c.add_(3.5)
print(c)
print(a, b)<jupyter_output>[1. 1. 1. 1. 1. 1. 1.] tensor([1., 1., 1., 1., 1., 1., 1.], dtype=torch.float64)<jupyter_text>All the Tensors on the CPU CUDA Tensors: Tensors can be moved onto any device using the `.to` method.<jupyter_code>torch.cuda.is_available()
# let us run this cell only if CUDA is available
# We will use ``torch.device`` objects to move tensors in and out of GPU
if torch.cuda.is_available():
    device = torch.device("cuda")          # a CUDA device object
    
    # create a tensor DIRECTLY on GPU
    y = torch.ones_like(x, device=device)  
    
    # OR just use strings ``.to("cuda")`` if you want to port to another device after creation 
    x = x.to(device)               
    
    z = x + y # z is by default on GPU
    
    print(z)
    
    # Transfer it to CPU 
    print(z.to("cpu", torch.double))       # ``.to`` can also change dtype together!
z.cpu() # on cpu
z.cuda() # on gpu, 0 implies 1st GPU<jupyter_output><empty_output><jupyter_text>The __rank of a tensor__ refers to the number of dimensions present within the tensor.<jupyter_code>len(z.size())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13762953367875647, 'fraction_numerical': 0.02299222797927461, 'mean_word_length': 3.9424, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '120911', 'license': 'MIT', 'n_tokens_mistral': 1214, 'n_tokens_neox': 1171, 'n_words': 358, 'path': '01_quick_blitz.ipynb', 'repo_name': 'HareeshBahuleyan/pytorch-examples', 'size': '3088'}