Torch库入门学习:数值计算

tensor的属性

import  torch

a=torch.tensor([1,2,3],dtype=int)
print(a)
print(a.dtype)

# b=torch.tensor([4,5,6],dtype=float64)
# print(b)

tensor = torch.tensor([[1,2,3],
                      [4,5,6]])
print(tensor.ndim)  #数据的维度    2     	相当于tensor对象的属性
print(tensor.shape) #数据的形状   torch.Size([2, 3])    相当于tensor对象的属性
print(tensor.size()) #数据的形状  torch.Size([2, 3])   相当于tensor对象的方法
print(tensor.dtype) #数据的类型  torch.int64  		相当于tensor对象的属性

数据生成

import torch

print(torch.ones(2,3))  #输出全为1的矩阵
# tensor([[1., 1., 1.],
#         [1., 1., 1.]])

print(torch.zeros(2,3))  #输出全为0 的矩阵
# tensor([[0., 0., 0.],
#         [0., 0., 0.]])

print(torch.rand(3,4)) #生成随机的数 浮点数
#jupter shift+Tab 查看函数的使用方法  pycharm光标移动到方法就有了
# tensor([[0.1829, 0.3624, 0.8165, 0.1371],
#         [0.3147, 0.8630, 0.5146, 0.8846],
#         [0.1628, 0.2560, 0.5614, 0.4894]])

print(torch.randint(0,10,(2,3))) #生成随机的数0-10的整数  形状2行3列
# tensor([[2, 5, 5],
#         [5, 9, 9]])

print(torch.randn((3,4))) #胜负符合正态分布的随机数
# tensor([[-0.7377,  0.4818, -1.5796, -0.9336],
#         [ 0.8454,  0.4926, -1.6019, -0.8239],
#         [ 0.0721, -1.2550, -0.2576,  0.1191]])

a=torch.tensor([[1,2],
               [3,4],
                [5,6]])
b=torch.rand_like(a,dtype=float)   #生成的b形状和a是一样的 但数据是随机的 float类型的
print(b.shape)  #torch.Size([3, 2])    查看数据的形状
print(b.size())#torch.Size([3, 2])

c=b.view(6) #生成一维数据,只有6个值
print(c)
# tensor([0.4948, 0.8333, 0.1455, 0.6152, 0.0642, 0.0853], dtype=torch.float64)
#view相当于reshape

d=b.view(2,3)
print(d)
# tensor([[0.4948, 0.8333, 0.1455],
#         [0.6152, 0.0642, 0.0853]], dtype=torch.float64)

d=d.reshape(6)  #改变数据的形状 和view功能一样 用哪个都行
print(d)
# tensor([0.4948, 0.8333, 0.1455, 0.6152, 0.0642, 0.0853], dtype=torch.float64)

print(d[1]) #获取第一个数值 tensor(0.8333, dtype=torch.float64)
print(d[1].item()) #获取具体的那个数,吧tensor去掉   0.8333

#d.item() 错误 不能所有换成ython里面标准的数值,每次只能1次

import numpy as np
e = np.array(d)  #把tensor变成np.array的一个方法
print(e)
#数字我都换了
# [0.63471338 0.64638701 0.32631209 0.0376451  0.10436153 0.33815973]

#把array转变成tensor
array = np.array([1,2,3])
print(array)  #[1 2 3]
tensor =torch.tensor(array)
print(tensor) #tensor([1, 2, 3], dtype=torch.int32)
``
```python
import torch

a=torch.randint(1,5,(2,3))
b=torch.randint(1,5,(2,3))
print(a)
print(b)
# tensor([[1, 3, 4],
#         [1, 4, 3]])
# tensor([[4, 1, 3],
#         [3, 1, 2]])

#矩阵相加
print(a+b) #两矩阵相加
print(torch.add(a,b))  #和上面效果一样

result = torch.zeros(2,3)
torch.add(a,b,out=result)  #矩阵a+b相加的结果放在result

#a=a+b
#注意:任何使张量tensor会发生变化的操作都有一个前缀'_'例如:a.add_(), b.sub_()
a.add_(b) #a+b的值赋值给a自己  a=a+b

print(a - b)
print(a * b)

print(a%b) #取余数
print(a/b) #a除b
print(a//b) #取整数 这种方式会报错,改成下面方式div
print(torch.div(a, b, rounding_mode='trunc')) #a整除b

torch.matmul(a,b)
#如果涉及到数据类型不匹配,转换 a=a.float()
#查看a的类型 a.dtype

print(a.T)  #矩阵转置


## 基本操作2

```python
import torch

sample = torch.rand(3,2)
print('sample:',sample)

#计算矩阵所有值的累计和
a = torch.sum(sample)
print('sample torch.sum:',a)

#求出矩阵sample中最小的那个值
b = torch.min(sample)
print('sample torch.min',b)

#求出矩阵sample中最大的那个值
c = torch.max(sample)
print('sample torch.max',c)

#求最小值所在的位置/索引
index_min = torch.argmin(sample)
print('sample torch.argmin',index_min)

#求最大值所在的位置/索引
index_max = torch.argmax(sample)
print('sample torch.argmax',index_max)

#求数据平均值 所有值的平均值
average = torch.mean(sample)
print("average",average)

#求中位数
print(torch.median(sample))

#计算开方 里面所有数计算开方
print(torch.sqrt(sample))
#等价 sample**2

# sample: tensor([[0.1259, 0.0302],
#         [0.2830, 0.6841],
#         [0.8272, 0.2799]])
# sample torch.sum: tensor(2.2303)
# sample torch.min tensor(0.0302)
# sample torch.max tensor(0.8272)
# sample torch.argmin tensor(1)
# sample torch.argmax tensor(4)
# average tensor(0.3717)
# tensor(0.2799)
# tensor([[0.3548, 0.1739],
#         [0.5320, 0.8271],
#         [0.9095, 0.5291]])

数值索引

import torch

#
tensor = torch.arange(2,14)
print(tensor)

print('tensor[2]',tensor[2])

print('tensor[1:4]',tensor[1:4])

print('tensor[2:-1]',tensor[2:-1])

print('tensor[:5]',tensor[:5])

print('tensor[-3:]',tensor[-3:])

index =[1,3,4,5,5]
print(tensor[index])

# tensor([ 2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13])
# tensor[2] tensor(4)
# tensor[1:4] tensor([3, 4, 5])
# tensor[2:-1] tensor([ 4,  5,  6,  7,  8,  9, 10, 11, 12])
# tensor[:5] tensor([2, 3, 4, 5, 6])
# tensor[-3:] tensor([11, 12, 13])
# tensor([3, 5, 6, 7, 7])

你可能感兴趣的:(Python,深度学习,学习,python,深度学习,pytorch)