Local, global, free variables in Python

The Execution Model of Python (in python doc) says

If a name is bound in a block, it is a local variable of that block, unless declared as nonlocal. If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a free variable.

Actually "global variables are a subset of free variables". The CPython implementation detail is if CPython marks a variable as "free", it means the marked variable is pure "free" but not "global" (in turn it means if a variable is free and global, is_free in CPython will return false).

CPython symtable.c module, SET_SCOPE says "An implicit  global is a free variable for which the compiler has found no binding in an enclosing function scope"

Reference:
http://www.gossamer-threads.com/lists/python/dev/860658?do=post_view_flat#860658
http://stackoverflow.com/questions/12919278/how-to-define-free-variable-in-python

C:\Users\21912\AppData\Local\Programs\Python\Python313\python.exe C:\Users\21912\PycharmProjects\PythonProject\Science\实验\多尺度分块_LBP\train.py Epoch 1/20: 0%| | 0/10 [00:16<?, ?it/s] Traceback (most recent call last): File "C:\Users\21912\PycharmProjects\PythonProject\Science\实验\多尺度分块_LBP\train.py", line 180, in <module> train() ~~~~~^^ File "C:\Users\21912\PycharmProjects\PythonProject\Science\实验\多尺度分块_LBP\train.py", line 135, in train outputs = model(imgs) File "C:\Users\21912\AppData\Local\Programs\Python\Python313\Lib\site-packages\torch\nn\modules\module.py", line 1739, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^ File "C:\Users\21912\AppData\Local\Programs\Python\Python313\Lib\site-packages\torch\nn\modules\module.py", line 1750, in _call_impl return forward_call(*args, **kwargs) File "C:\Users\21912\PycharmProjects\PythonProject\Science\实验\多尺度分块_LBP\多尺度分块_LBP.py", line 109, in forward feat = processor(x, base_feat) File "C:\Users\21912\AppData\Local\Programs\Python\Python313\Lib\site-packages\torch\nn\modules\module.py", line 1739, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^ File "C:\Users\21912\AppData\Local\Programs\Python\Python313\Lib\site-packages\torch\nn\modules\module.py", line 1750, in _call_impl return forward_call(*args, **kwargs) File "C:\Users\21912\PycharmProjects\PythonProject\Science\实验\多尺度分块_LBP\多尺度分块_LBP.py", line 173, in forward fused = torch.cat([local_feat, global_feat, hist_embed], dim=1) torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 5.00 GiB. GPU 0 has a total capacity of 8.00 GiB of which 0 bytes is free. Of the allocated memory 15.03 GiB is allocated by PyTorch, and 3.00 GiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation. See documentation for Memory Management (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables) 进程已结束,退出代码为 1
03-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值