假设我创建了一个张量并将其放在 GPU 上并且以后不需要它并且想要释放分配给它的 GPU 内存;我该怎么做?
import torch
a=torch.randn(3,4).cuda() # nvidia-smi shows that some mem has been allocated.
# do something
# a does not exist and nvidia-smi shows that mem has been freed.
Run Code Online (Sandbox Code Playgroud)
我试过了:
del a del a; torch.cuda.empty_cache()但它们都不起作用。
我有一堂课,里面有一个字典。我想通过直接“ instance.key”而不是“ instance.d [key]”来访问字典中的变量。怎么做?
class A:
def __init__(self):
self.a = 1
self.b = 2
self.fields = {'c': 3, 'd': 4}
def main():
a = A()
print(a.a) # 1
print(a.b) # 2
print(a.c) # currently raise Exception, I want it to print 3
print(a.d) # currently raise Exception, I want it to print 4
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)