训练保存模型checkpoint时报错SyntaxError: invalid syntax

在使用pytorch训练保存checkpoint时,出现如下报错:

python 复制代码
root@autodl-container-745411b452-c5cebfed:~/kvasir-seg-main# python train_transunet.py --loss_function="IoULoss" --training_augmentation=0
  File "train_transunet.py", line 306
    torch.save(state, f'checkpoints/{SETTINGS["model_name"]}_{'headnum6'}.pth')
                                                               ^
SyntaxError: invalid syntax

报错的代码是这句这一句原本代码如下:

python 复制代码
torch.save(state, f'checkpoints/{SETTINGS["model_name"]}.pth')

因为我修改了一个超参数head_num,希望把这个超参数加入到checkpoint名称里,以区分每次训练保存的结果。于是增加了_{'headnum6'}。

出错的原因,是 f-string 本身也是用单引号包围的,而我又在单引号内部又用了一次但引导,导致f-string产生了混淆,无法正确解析。

所以只要把单引号替换成双引号就可以了

python 复制代码
torch.save(state, f'checkpoints/{SETTINGS["model_name"]}_{"headnum6"}.pth')
相关推荐
Xiaok101813 小时前
NumPy 数组 vs PyTorch Tensor
人工智能·pytorch·numpy
淼澄研学4 天前
PyTorch深度学习实战:从零构建神经网络模型的五个核心步骤
pytorch·深度学习·神经网络
xx_xxxxx_5 天前
AI基本结构12-lstm实现nlp
人工智能·pytorch·rnn·lstm
工业机器视觉设计和实现5 天前
cifar10训练突破80分(三,摸到pytorch尾灯!)
人工智能·pytorch·cudnn微积分
想会飞的蒲公英5 天前
PyTorch reshape、view、transpose、广播到底怎么选?
人工智能·pytorch·python
风痕天际6 天前
Pytorch开发教程2——张量核心操作完全指南
人工智能·pytorch·python
m沐沐6 天前
【深度学习】循环神经网络RNN——结构、原理与长期依赖问题解析
人工智能·pytorch·python·rnn·深度学习·算法·机器学习
FriendshipT6 天前
Ultralytics:简要解读YOLOv8 → YOLO11 → YOLO26网络架构
人工智能·pytorch·python·深度学习·yolo·目标检测
工业机器视觉设计和实现7 天前
动量的好处与困扰(二,摸到pytorch尾灯!)
人工智能·pytorch·cudnn微积分
盼小辉丶7 天前
PyTorch强化学习实战(20)——优势演员-评论家(Advantage Actor-Critic, A2C)
pytorch·深度学习·强化学习·优势演员-评论家算法