<jupyter_start><jupyter_text>Initialnizing a Tesnsor  Directly from data<jupyter_code>data = [[1, 2],[3, 4]]
x_data = torch.tensor(data)<jupyter_output><empty_output><jupyter_text>From a Numpy array<jupyter_code>np_array = np.array(data)
x_np = torch.from_numpy(np_array)<jupyter_output><empty_output><jupyter_text>From another tesnsor<jupyter_code>x_ones = torch.ones_like(x_data) # retains the properties of x_data
print(f"Ones Tensor: \n {x_ones} \n")

x_rand = torch.rand_like(x_data, dtype=torch.float) # overrides the datatype of x_data
print(f"Random Tensor: \n {x_rand} \n")<jupyter_output>Ones Tensor: 
 tensor([[1, 1],
        [1, 1]]) 

Random Tensor: 
 tensor([[0.5340, 0.3798],
        [0.2675, 0.7671]])<jupyter_text>With random or constant values<jupyter_code>shape = (2,3,)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(shape)

print(f"Random Tensor: \n {rand_tensor} \n")
print(f"Ones Tensor: \n {ones_tensor} \n")
print(f"Zeros Tensor: \n {zeros_tensor}")<jupyter_output>Random Tensor: 
 tensor([[0.7259, 0.4660, 0.7823],
        [0.4336, 0.3983, 0.8255]]) 

Ones Tensor: 
 tensor([[1., 1., 1.],
        [1., 1., 1.]]) 

Zeros Tensor: 
 tensor([[0., 0., 0.],
        [0., 0., 0.]])<jupyter_text>Attributes of a Tesnsor<jupyter_code>tensor = torch.rand(3,4)

print(f"Shape of tensor: {tensor.shape}")
print(f"Datatype of tensor: {tensor.dtype}")
print(f"Device tensor is stored on: {tensor.device}")<jupyter_output>Shape of tensor: torch.Size([3, 4])
Datatype of tensor: torch.float32
Device tensor is stored on: cpu<jupyter_text>Operations on Tensors<jupyter_code># We move our tensor to the GPU if available
if torch.cuda.is_available():
    tensor = tensor.to('cuda')<jupyter_output><empty_output><jupyter_text>Standard numpy-like indexing and slicing<jupyter_code>tensor = torch.ones(4, 4)
print('First row: ', tensor[0])
print('First column: ', tensor[:, 0])
print('Last column:', tensor[..., -1])
tensor[:,1] = 0
print(tensor)<jupyter_output>First row:  tensor([1., 1., 1., 1.])
First column:  tensor([1., 1., 1., 1.])
Last column: tensor([1., 1., 1., 1.])
tensor([[1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.]])<jupyter_text>Joining tensors<jupyter_code>t1 = torch.cat([tensor, tensor, tensor], dim=0)
print(t1)
t1 = torch.cat([tensor, tensor, tensor], dim=1)
print(t1)<jupyter_output>tensor([[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
        [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
        [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
        [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.]])<jupyter_text>Arithmetic operations<jupyter_code># This computes the matrix multiplication between two tensors. y1, y2, y3 will have the same value
y1 = tensor @ tensor.T
y2 = tensor.matmul(tensor.T)

y3 = torch.rand_like(tensor)
torch.matmul(tensor, tensor.T, out=y3)


# This computes the element-wise product. z1, z2, z3 will have the same value
z1 = tensor * tensor
z2 = tensor.mul(tensor)

z3 = torch.rand_like(tensor)
torch.mul(tensor, tensor, out=z3)<jupyter_output><empty_output><jupyter_text>Single-element tensors<jupyter_code>agg = tensor.sum()
agg_item = agg.item()
print(agg_item, type(agg_item))<jupyter_output>12.0 <class 'float'><jupyter_text>In-place operations In-place operations save some memory, but can be problematic when computing derivatives because of an immediate loss of history. Hence, their use is discouraged.<jupyter_code>print(tensor, "\n")
tensor.add_(5)
print(tensor)
def f1(a: int = 1, *, b: float = 2) -> float:
    r"""
    *的两个用法：
        1.def f(*args):
              print(args)
              
        >>> f(1,2,3)
        1,2,3
        
        2.def f(a, b, *, c, d)
        *后面的所有参数，如c,d，只能以关键字参数形式参数，如a = 1, b = 2，不能以位置参数形式传入，如1, 2
        
        >>> f(1, 2, 1, 2)
         SyntaxError
        >>> f(1, 2, c = 1, d = 2)
        Right Output
    
    /的用法：
        /前面的参数只能以位置参数的形式传入，而不能以关键字参数的形式传入
         def f(a, b, /, c, d)
         
        >>> f(a = 1, 2, 3, 4)
        SyntaxError
        >>> f(1, 2, 3, 4)
        Right Output 
    """
    res = a + b
    return b
f1(a = 3, b = 2)<jupyter_output><empty_output><jupyter_text>Bridge with Numpy  Tensor to Numpy array<jupyter_code>t = torch.ones(5)
print(f"t: {t}")
n = t.numpy()
print(f"n: {n}")<jupyter_output>t: tensor([1., 1., 1., 1., 1.])
n: [1. 1. 1. 1. 1.]<jupyter_text>A change in the tensor reflects in the NumPy array.<jupyter_code>t.add_(1)
print(f"t: {t}")
print(f"n: {n}")<jupyter_output>t: tensor([2., 2., 2., 2., 2.])
n: [2. 2. 2. 2. 2.]<jupyter_text>Numpy array to Tensor<jupyter_code>n = np.ones(5)
t = torch.from_numpy(n)<jupyter_output><empty_output><jupyter_text>Changes in the NumPy array reflects in the tensor.<jupyter_code>t.add_(1)
print(f"t: {t}")
print(f"n: {n}")<jupyter_output>t: tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
n: [2. 2. 2. 2. 2.]
{'fraction_non_alphanumeric': 0.18633034987794955, 'fraction_numerical': 0.05187144019528071, 'mean_word_length': 3.573953488372093, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '473899', 'license': 'MIT', 'n_tokens_mistral': 2207, 'n_tokens_neox': 2048, 'n_words': 491, 'path': 'pythonExample/pytorchExample/IntroductionTopytorch/Tensors.ipynb', 'repo_name': 'YuTao0310/programmer', 'size': '4916'}