import torch # Create a 1D tensor a = torch.Tensor([1, 2, 3, 4, 5]) # Create a 2D tensor b = torch.Tensor([[1, 2], [3, 4], [5, 6]]) # Perform element-wise multiplication on two tensors c = a * b # Reshape a tensor d = a.view(5, 1) # Print outputs print(a) print(b) print(c) print(d)
import torch # Create a tensor filled with ones a = torch.ones(3, 4) # Create a tensor filled with random numbers b = torch.randn(2, 2) # Add tensors element-wise c = a + b # Squeeze a tensor d = c.squeeze() # Print outputs print(a) print(b) print(c) print(d)In this example, we first create a 3x4 tensor filled with ones and a 2x2 tensor filled with random numbers. We then add these tensors element-wise and store the result in tensor `c`. Finally, we squeeze tensor `c` to remove any dimensions with size 1 and store the result in tensor `d`. We print all the tensors to the console to verify their contents. In both examples, we use the PyTorch library, which is a popular machine learning library for Python.