1、PyTorch介绍与张量的创建
1. 大佬视频
1、PyTorch介绍与张量的创建
2. python 代码
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName :torch_learning01.py
# @Time :2024/11/16 18:37
# @Author :Jason Zhang
import torch
import numpy as np
torch.set_printoptions(sci_mode=False, precision=3)
if __name__ == "__main__":
run_code = 0
data = [[1, 2], [3.0, 4]]
data_tensor = torch.tensor(data)
print(f"data={data},type(data)={type(data)}")
print(f"data_tensor={data_tensor},type(data_tensor)={type(data_tensor)}")
print(f"data_tensor.dtype={data_tensor.dtype}")
data_numpy = np.array([[1, 2], [3, 4]])
data_torch = torch.from_numpy(data_numpy)
print(f"data_numpy={type(data_numpy)},type(data_torch)={type(data_torch)}")
b = torch.arange(12, dtype=torch.float32).reshape((3, 4))
b_ones_like = torch.ones_like(b)
b_zeros_like = torch.zeros_like(b)
b_rand_like = torch.rand_like(b)
print(f"b=\n{b}")
print(f"b_ones_like=\n{b_ones_like}")
print(f"b_zeros_like=\n{b_zeros_like}")
print(f"b_rand_like=\n{b_rand_like}")
rand_shape23 = torch.rand((2, 3))
print(f"rand_shape23=\n{rand_shape23}")
ones_shape23 = torch.ones((2, 3))
print(f"ones_shape23=\n{ones_shape23}")
zeros_shape23 = torch.zeros((2, 3))
print(f"zeros_shape23=\n{zeros_shape23}")
zeros_shape23_dtype = zeros_shape23.dtype
print(f"zeros_shape23_dtype={zeros_shape23_dtype}")
zeros_shape23_shape = zeros_shape23.shape
print(f"zeros_shape23_shape={zeros_shape23_shape}")
zeros_shape23_device = zeros_shape23.device
print(f"zeros_shape23_device={zeros_shape23_device}")
cuda_is_available = torch.cuda.is_available()
print(f"cuda_is_available=\n{cuda_is_available}")
zeros_shape23 = zeros_shape23.to('cuda')
print(f"zeros_shape23_device={zeros_shape23.device}")
np_23 = np.random.random((2, 3))
np_23_is_tensor = torch.is_tensor(np_23)
print(f"{np_23} is {np_23_is_tensor} torch.tensor")
np_23_torch = torch.from_numpy(np_23)
np_23_torch_is_tensor = torch.is_tensor(np_23_torch)
print(f"{np_23_torch} is {np_23_torch_is_tensor} torch.tensor")
one_tensor = torch.tensor(1.0)
one_tensor_is_nonzero = torch.is_nonzero(one_tensor)
print(f"one_tensor={one_tensor}")
print(f"one_tensor_is_nonzero={one_tensor_is_nonzero}")
rand_23 = torch.rand((2, 3))
rand_23_numel = torch.numel(rand_23)
print(f"{rand_23} is {rand_23_numel}")
torch_arange = torch.arange(0, 5)
print(f"torch_arange=\n{torch_arange}")
# torch_range = torch.range(start=0, end=5)
# print(f"torch_range=\n{torch_range}")
torch_arange2 = torch.arange(1, 6) % 5
print(f"torch_arange2=\n{torch_arange2}")
for i in torch.arange(7):
print(f"i={i}")
torch_li = torch.linspace(1, 10, 5)
print(f"torch_li={torch_li}")
torch_eye5 = torch.eye(5)
print(f"torch_eye5=\n{torch_eye5}")
torch_eye24 = torch.eye(2, 4)
print(f"torch_eye24=\n{torch_eye24}")
torch_full_314 = torch.full([3, 4], 3.14)
torch_ones_314 = 3.14 * torch.ones((3, 4))
print(f"torch_full_314=\n{torch_full_314}")
print(f"torch_ones_314=\n{torch_ones_314}")
cat1 = torch.rand((2, 3))
cat2 = torch.rand((2, 3))
torch_cat12 = torch.cat((cat1, cat2), 0)
torch_cat12_1 = torch.cat((cat1, cat2), 1)
print(f"cat1=\n{cat1}")
print(f"cat2=\n{cat2}")
print(f"torch_cat12=\n{torch_cat12}")
print(f"torch_cat12_1=\n{torch_cat12_1}")
- 结果:
data=[[1, 2], [3.0, 4]],type(data)=<class 'list'>
data_tensor=tensor([[1., 2.],
[3., 4.]]),type(data_tensor)=<class 'torch.Tensor'>
data_tensor.dtype=torch.float32
data_numpy=<class 'numpy.ndarray'>,type(data_torch)=<class 'torch.Tensor'>
b=
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])
b_ones_like=
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
b_zeros_like=
tensor([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
b_rand_like=
tensor([[0.334, 0.780, 0.210, 0.252],
[0.235, 0.528, 0.647, 0.188],
[0.991, 0.704, 0.177, 0.809]])
rand_shape23=
tensor([[0.377, 0.035, 0.664],
[0.471, 0.954, 0.777]])
ones_shape23=
tensor([[1., 1., 1.],
[1., 1., 1.]])
zeros_shape23=
tensor([[0., 0., 0.],
[0., 0., 0.]])
zeros_shape23_dtype=torch.float32
zeros_shape23_shape=torch.Size([2, 3])
zeros_shape23_device=cpu
cuda_is_available=
True
zeros_shape23_device=cuda:0
[[0.99230163 0.0692696 0.80117093]
[0.73090885 0.51565045 0.52366436]] is False torch.tensor
tensor([[0.992, 0.069, 0.801],
[0.731, 0.516, 0.524]], dtype=torch.float64) is True torch.tensor
one_tensor=1.0
one_tensor_is_nonzero=True
tensor([[0.445, 0.668, 0.124],
[0.717, 0.891, 0.951]]) is 6
torch_arange=
tensor([0, 1, 2, 3, 4])
torch_arange2=
tensor([1, 2, 3, 4, 0])
i=0
i=1
i=2
i=3
i=4
i=5
i=6
torch_li=tensor([ 1.000, 3.250, 5.500, 7.750, 10.000])
torch_eye5=
tensor([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
torch_eye24=
tensor([[1., 0., 0., 0.],
[0., 1., 0., 0.]])
torch_full_314=
tensor([[3.140, 3.140, 3.140, 3.140],
[3.140, 3.140, 3.140, 3.140],
[3.140, 3.140, 3.140, 3.140]])
torch_ones_314=
tensor([[3.140, 3.140, 3.140, 3.140],
[3.140, 3.140, 3.140, 3.140],
[3.140, 3.140, 3.140, 3.140]])
cat1=
tensor([[0.868, 0.323, 0.289],
[0.773, 0.597, 0.806]])
cat2=
tensor([[0.522, 0.376, 0.304],
[0.854, 0.298, 0.656]])
torch_cat12=
tensor([[0.868, 0.323, 0.289],
[0.773, 0.597, 0.806],
[0.522, 0.376, 0.304],
[0.854, 0.298, 0.656]])
torch_cat12_1=
tensor([[0.868, 0.323, 0.289, 0.522, 0.376, 0.304],
[0.773, 0.597, 0.806, 0.854, 0.298, 0.656]])