import torch
import numpy as np
# 随机初始化很大或者很小的数
m=torch.empty(5,3)
print(m)
# 随机初始化 0-1 范围的数
n=torch.rand(5,3)
# 随机生成 标准正态分布
n=torch.randn(5,3)
print(n)
# 生出数据类型为long的零矩阵
x=torch.zeros(3,5,dtype=torch.long)
y=torch.zeros(3,5).long()
print(x)
print(y)
# 生成向量
t=torch.tensor([2,3],dtype=torch.double)
print(t)
# 生成和t形状一致的标准正态分布
q=torch.randn_like(t)
print(q)
# t.shape=t.size() 求向量的形状
print(t.shape)
print(t.size())
# 加 +=torch.add()
x=torch.randn(3,5)
y=torch.rand(3,5)
sum=torch.add(x,y)
sum=x+y
# 索引 同一般的索引一样
t=torch.randn(5,3)
print(t[:,2:])
# resize t.view() t.reshape() 都可
x=torch.rand(4,4)
x=x.view(16)
x=x.reshape(2,8)
print(x)
# tensor 向量的内置方法
x=torch.tensor([2,3])
print(x)
print(x.data)
# 取出向量里面的数字
for i in x:
print(i.item())
# transpose ???
x=torch.rand(2,3)
print(x.shape)
x.transpose(1,0)
print(x.size())
# gpu
if torch.cuda.is_available():
print('true')
# 1.
device=o=torch.device("cuda")
x.to(device)
# 2.
x.to('cuda') #/'cpu'
# 3.
x.cuda()
# tensor 和 numpy 之间的转换
# 1. array---> tensor torch.from_numpy
a=np.array([3,4,5])
b=torch.from_numpy(a)
print(b)
# 2.tensor ---> array a.numpy()
a=b.numpy()
print(a)
# model=model.cuda() # 把整个模型都搬到 cuda上运算