import torch
# Create tensors
x = torch.tensor([1, 2, 3], dtype=torch.float32)
y = torch.ones(3, 2)
z = torch.randn(2, 3, 4)
# Basic operations
result = x + 5
reshaped = z.view(-1, 4)
print(f"Shape: {result.shape}")
print(f"Device: {result.device}")
# Enable gradient computation
x = torch.tensor([2.0], requires_grad=True)
y = torch.tensor([3.0], requires_grad=True)
# Forward pass
z = x * y + x**2
loss = z.sum()
# Backward pass
loss.backward()
print(f"dL/dx: {x.grad}") # 7.0
print(f"dL/dy: {y.grad}") # 2.0