做影视网站犯法吗不用wordpress 知乎
news/
2025/9/28 18:41:33/
文章来源:
做影视网站犯法吗,不用wordpress 知乎,东莞松山湖中学,网站平台建设基本情况总结#xff1a;
初级#xff1a;简单修改一下超参数#xff0c;效果一般般但是够用#xff0c;有时候甚至直接不够用
中级#xff1a;optuna得出最好的超参数之后#xff0c;再多一些epoch让train和testloss整体下降#xff0c;然后结果就很不错。
高级#xff1a;…总结
初级简单修改一下超参数效果一般般但是够用有时候甚至直接不够用
中级optuna得出最好的超参数之后再多一些epoch让train和testloss整体下降然后结果就很不错。
高级在中级的基础上更换更适合的损失函数之后在train的时候backward反向传播这个loss,optuna也更改这个loss标准现在效果有质的改变。 问题
最近在做cfd领域需要流场进行预测然后流场提取出来再深度学习就是一个多维度tensor,而神经网络的目的就是通过模型预测让预测的tensor与实际的tensor的结果尽可能的接近具体来说就是让每个值之间的误差尽可能小。
目前情况现在模型大概以及确定但是效果一般般这时候就需要进行下面的调优方法。
优化方法
一、初级优化
简单修改一下超参数效果一般般但是够用有时候甚至直接不够用 二、中级优化optuna调参然后epoch加多
optuna得出最好的超参数之后再多一些epoch让train和testloss整体下降然后结果就很不错。 三、高级优化
在中级的基础上现在更换更适合的损失函数之后在train的时候backward反向传播这个loss,optuna也更改这个loss标准现在效果有质的改变。 也就是下面这三行代码
smooth_l1 F.smooth_l1_loss(out.view(shape1, shape2), y.view(shape1, shape2))#
smooth_l1.backward() #用这个smooth_l1_loss反向传播#
return test_smooth_l1 #test中的最后一个epoch的test_smooth_l1通过上面预测的数据和实际的数据进行的对比可以发现预测的每个结果与实际的结果的误差在大约0.01范围之内实际数据在[-4,4]之间。 确定损失函数
要让两个矩阵的值尽可能接近选择合适的损失函数loss function是关键。常见的用于这种目的的损失函数包括以下几种 均方误差Mean Squared Error, MSE对预测值与真实值之间的平方误差求平均。MSE对大误差比较敏感能够显著惩罚偏离较大的预测值。 import torch.nn.functional as F loss F.mse_loss(predicted, target) 平均绝对误差Mean Absolute Error, MAE对预测值与真实值之间的绝对误差求平均。MAE对异常值不如MSE敏感适用于数据中存在异常值的情况。 import torch loss torch.mean(torch.abs(predicted - target)) 平滑L1损失Smooth L1 Loss又称Huber Loss当误差较小时平滑L1损失类似于L1损失当误差较大时类似于L2损失。适合在有噪声的数据集上使用。 import torch.nn.functional as F loss F.smooth_l1_loss(predicted, target) 总结如下 MSE适用于需要显著惩罚大偏差的情况。 MAE适用于数据中存在异常值并且你希望对异常值不那么敏感的情况。 Smooth L1 Loss适用于既有一定抗噪声能力又能对大偏差适当惩罚的情况。 这里根据任务选择Smooth L1 Loss。 具体做法
目前这个经过optuna调优然后先下面处理思想是将loss的反向传播和optuna优化标准全换为更适合这个任务的smooth_l1_loss函数
1. loss将mse更换为smooth_l1_loss2. l2.backward()更换为smooth_l1.backward()3. return test_l2更改为return test_smooth_l1
结果point_data看着值很接近每个值误差0.01范围内。说明用这个上面这个方法是对的。试了一下图也有优化。并step_loss现在极低。 下面代码中加感叹号的行都是上面思路修改我的项目中对应的代码行重要
import optuna
import time
import torch.optim as optim
# 求解loss的两个参数
shape1 -1
shape2 data.shape[1]* 3def objective1(trial):batch_size trial.suggest_categorical(batch_size, [32])learning_rate trial.suggest_float(learning_rate, 1e-6, 1e-2,logTrue)layers trial.suggest_categorical(layers, [2,4,6])width trial.suggest_categorical(width, [10,20,30])#新加的weight_decay trial.suggest_float(weight_decay, 1e-6, 1e-2,logTrue)#新加的#再加个优化器optimizer_name trial.suggest_categorical(optimizer, [Adam, SGD, RMSprop])# loss_function_name trial.suggest_categorical(loss_function, [LpLoss, MSELoss]) Read data # data是[1991, 80, 40, 30]而data_cp是为归一化的[2000, 80, 40, 30]train_a data[ntest:-1,:,:]#data:torch.Size50:, 80, 40, 30。train50对应的是predict5091train_u data_cp[ntest10:,:,:]#torch.Size([50, 64, 64, 10])#data_cp是未归一化的第11个对应的是data的第data的第1个,两者差10# print(train_a.shape)# print(train_u.shape)test_a data[:ntest,:,:]#选取最后200个当测试集test_u data_cp[10:ntest10,:,:]# print(test_a.shape)# print(test_u.shape)#torch.Size([40, 80, 40, 3])train_loader torch.utils.data.DataLoader(torch.utils.data.TensorDataset(train_a, train_u),batch_sizebatch_size, shuffleTrue)test_loader torch.utils.data.DataLoader(torch.utils.data.TensorDataset(test_a, test_u),batch_sizebatch_size, shuffleFalse)#没有随机的train_loader用于后面预测可视化data_loader_noshuffle torch.utils.data.DataLoader(torch.utils.data.TensorDataset(data[:,:,:], data_cp[9:,:,:]),batch_sizebatch_size, shuffleFalse)# %% The model definition device torch.device(cuda if torch.cuda.is_available() else cpu)model WNO1d(widthwidth, levellevel, layerslayers, sizeh, waveletwavelet,in_channelin_channel, grid_rangegrid_range).to(device)# print(count_params(model))# optimizer torch.optim.Adam(model.parameters(), lrlearning_rate, weight_decay1e-6)#调参数用优化器选择if optimizer_name Adam:optimizer optim.Adam(model.parameters(), lrlearning_rate, weight_decayweight_decay)elif optimizer_name SGD:optimizer optim.SGD(model.parameters(), lrlearning_rate, weight_decayweight_decay, momentum0.9)else: # RMSpropoptimizer optim.RMSprop(model.parameters(), lrlearning_rate, weight_decayweight_decay)scheduler torch.optim.lr_scheduler.StepLR(optimizer, step_sizestep_size, gammagamma)train_loss torch.zeros(epochs)test_loss torch.zeros(epochs)myloss LpLoss(size_averageFalse) Training and testing for ep in range(epochs):model.train()t1 default_timer()train_mse 0train_l2 0for x, y in train_loader:x, y x.to(device), y.to(device)optimizer.zero_grad()out model(x)mse F.mse_loss(out.view(shape1, shape2), y.view(shape1, shape2))# # 训练时使用 Smooth L1 Losssmooth_l1 F.smooth_l1_loss(out.view(shape1, shape2), y.view(shape1, shape2))#l2 myloss(out.view(shape1, shape2), y.view(shape1, shape2))# l2.backward()smooth_l1.backward() #用这个smooth_l1_loss反向传播#optimizer.step()train_mse mse.item()train_l2 l2.item()scheduler.step()model.eval()test_l2 0.0test_smooth_l1 0with torch.no_grad():for x, y in test_loader:x, y x.to(device), y.to(device)out model(x)test_l2 myloss(out.view(shape1, shape2), y.view(shape1, shape2)).item()test_smooth_l1 F.smooth_l1_loss(out.view(shape1, shape2), y.view(shape1, shape2)).item()#train_mse / ntrain#len(train_loader)train_l2 / ntraintest_l2 / ntesttest_smooth_l1 / ntest#train_loss[ep] train_l2test_loss[ep] test_l2t2 default_timer()print(Epoch-{}, Time-{:0.4f}, [step_loss:] - Train-MSE-{:0.4f}test_smooth_l1-{:0.4f} Train-L2-{:0.4f}, Test-L2-{:0.4f}.format(ep, t2-t1, train_mse,test_smooth_l1, train_l2, test_l2))#1if trial.should_prune():raise optuna.exceptions.TrialPruned()防止打印信息错位print(fTrial {trial.number} finished with value: {test_l2})return test_smooth_l1 #test中的最后一个epoch的test_smooth_l1 For saving the trained model and prediction data
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/920988.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!