UnboundLocalError: local variable ‘loss’ referenced before assignment解决方法

简介: UnboundLocalError: local variable ‘loss’ referenced before assignment解决方法

Traceback (most recent call last):
  File "src/main.py", line 442, in <module>
    main(args)
  File "src/main.py", line 404, in main
    args.clip_max_norm, args)
  File "/home/wsx/0A_DATA/HFPN/src/engine.py", line 52, in train_one_epoch
    losses = sum(loss_dict[k] * weight_dict[k] for k in loss_dict.keys() if k in weight_dict)
UnboundLocalError: local variable 'loss_dict' referenced before assignment
Killing subprocess 21108

原因:分布式同时多任务训练导致显存爆了导致。

解决:改小batchsize,更换的ddp,降一下显存,处理一下数据传入。

另外可在报错语句前面加入以下进行预警。

try:
            ......
            ......
        except RuntimeError as e:
            if "out of memory" in str(e):
                sys.exit('Out Of Memory')
            else:
                raise e
目录
相关文章
RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.
RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.
2571 0
undefined reference to symbol 'dlsym@@GLIBC_2.17' libdl.so: error adding symbols: DSO missing from c
undefined reference to symbol 'dlsym@@GLIBC_2.17' libdl.so: error adding symbols: DSO missing from c
722 0
|
8月前
|
机器学习/深度学习 Python
【Python】已解决TypeError: init() got an unexpected keyword argument ‘threshold’
【Python】已解决TypeError: init() got an unexpected keyword argument ‘threshold’
449 0
|
10月前
|
编译器 C语言
gcc编译警告:warning: suggest parentheses around assignment used as truth value
gcc编译警告:warning: suggest parentheses around assignment used as truth value
499 0
成功解决ValueError: Found input variables with inconsistent numbers of samples: [86, 891]
成功解决ValueError: Found input variables with inconsistent numbers of samples: [86, 891]
|
机器学习/深度学习 PyTorch 算法框架/工具
【完美解决】RuntimeError: one of the variables needed for gradient computation has been modified by an inp
将loss.backward()函数内的参数retain_graph值设置为True, loss.backward(retain_graph=True),如果retain_graph设置为False,计算过程中的中间变量使用完即被释放掉。
1895 0
|
机器学习/深度学习 数据采集
ValueError: Found input variables with inconsistent numbers of samples: [140, 1120] 怎么解决?
这个错误通常发生在机器学习模型的训练中,它表示输入数据的样本数量不一致。在你的情况下,你的输入数据中有两个变量,一个变量的样本数量为140,另一个变量的样本数量为1120,因此这个错误就出现了。 为了解决这个问题,你需要确保所有输入变量的样本数量是相同的。你可以通过以下几种方式来解决这个问题: 检查数据:检查数据是否正确加载,可能会导致数据样本数量不一致。 数据清洗:检查是否有重复的样本或者缺失的样本,如果有则需要对数据进行清洗。 数据对齐:如果你使用了多个数据源,那么你需要对它们进行对齐以确保它们的样本数量一致。 数据重采样:如果数据中有不均衡的样本数量,你可以考虑使用数据重采样方
1071 0
error: implicit declaration of function ‘RAND_egd’ [-Werror=implicit-function-declaration]
error: implicit declaration of function ‘RAND_egd’ [-Werror=implicit-function-declaration]
151 0
解决办法:defined but not used [-Werror=unused-variable]
解决办法:defined but not used [-Werror=unused-variable]
853 0
解决办法:RuntimeError: dictionary changed size during iteration
解决办法:RuntimeError: dictionary changed size during iteration
193 0