PyTorch
PyTorch: Python的一个深度学习框架,不多介绍
使用
当然在 .py / .ipynb 使用
# 导入
import torch
type( ) #返回;类型
isinstance(实例,类型) #实例是不是指定类型
torvh gather
Pytorch
类型:Tensor
IntTensor
FloatTensor #默认类型
DoubleTensor
实例方法
size() #若有参数, 返回相应维度的大小
shape #返回以为的储存维度大小的,给下标可以看到对应维度的大小
type()
Size( )
from_numpy( )
# 里边给多个参数,每个参数分别是各个维度的大小(参数的数量就是维度数量)
tensor( ) #直接给要的张量
Tensor
...Tensor
rand( ) #随机值, 单个值∈[0,1]
rand_like() #随机值, 单个值∈N(0,1)
ones( ) #全1
# one_like( a)
zeros( ) #全0
eye( 大小) #单位矩阵
empty( )
randint( 最小值, 最大值, shape) #随机值, 单个值∈[最小值,最大值)
normal( 均值张量 ,方差张量) #自定义两个同shape矩阵, 前一个矩阵为均值, 后一个矩阵为方差
full( shape, 值) #
range( 起始值, 边界值[, 步长])
linspace( 起始值, 边界值, 步数) #数值平均分配
logspace( 起始值, 边界值, 步数) #↑,作为指数
randperm( )
# 切片
二分类
# 模型(网络)
model = nn.Sequential(
nn.Linear(105,30),
nn.ReLU(),
nn.Linear(30,10),
nn.ReLU(),
nn.Linear(10,2),
nn.ReLU(),
nn.Softmax(),
)
# 训练一个epoch
def train(X,Y,model,batch_size,loss,optimizer,loss_s=[],accuracy_s=[]):
# index_True=np.where(Y==True)
# index_False=np.where(Y==False)
# np.random.shuffle(index_False)
# index_train=np.concatenate([index_True,index_False[:2*len(index_True)]],axis=1 )
# X_batch = X[index_train]
# Y_batch = Y[index_train]
# 随机打乱
index_s = np.arange(0,X.shape[0])
np.random.shuffle(index_s)
X = X[index_s]
Y = Y[index_s]
# 计算批次
batch_num = int(X.shape[0]/batch_size)
for step in range(batch_num):
X_batch=X[step*batch_size:(step+1)*batch_size]
Y_batch=Y[step*batch_size:(step+1)*batch_size]
# 计算预测值
Y_hat = model(X_batch)
# 计算loss
l = loss(Y_hat,Y_batch.reshape(-1)).sum()
# 梯度置为0
optimizer.zero_grad()
# 反向传播
l.backward()
# 梯度下降
optimizer.step()
# 更新loss
loss_s+=[ float(l) ]
# 更新accuracy
accuracy = (Y_hat.argmax(axis=1)==Y_batch.reshape(-1)).sum()/Y_batch.shape[0]
accuracy_s+=[ float(accuracy) ]
# 保存模型
if accuracy>0.93:
curr_time = datetime.datetime.now()
time_str = curr_time.strftime("%Y_%m_%d_%H_%M_%S")
torch.save(model.state_dict(), f'./9_output/model_{time_str}_'+str(int(accuracy*1000))+'.pth')
return (loss_s,accuracy_s)
# 优化器,loss函数
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
loss = nn.CrossEntropyLoss()
# 保存 loss,accuracy
loss_s,accuracy_s=[],[]
epoch_num = 1000
# 训练epochs
for i in range(epoch_num):
loss_s,accuracy_s = train(X,Y,model,1000,loss,optimizer,loss_s,accuracy_s)
# 模型保存,文件夹/model_时间_精度.pth
curr_time = datetime.datetime.now()
time_str = curr_time.strftime(r'%Y_%m_%d_%H_%M_%S_%f') # 精确到毫秒
model_path = f'./9_output/model_{time_str}_'+str(int(accuracy*1000))+'.pth'
torch.save(model.state_dict(), model_path) #, _use_new_zipfile_serialization=False
# 模型载入
model_param = torch.load(model_path)
model.load_state_dict(model_param)