python
dlvector
be better man,基础不牢,地动山摇!
展开
-
python深度拷贝和浅拷贝的差异
# add a new element to orignal 1D list aimport copy a = [1, 2]b = a c = a.copy() d = a[:]e = list(a)f = copy.copy(a)g = copy.deepcopy(a) print("add a new element to orignal 1D list a")a.append(3)print("original a = ", a)print("assign b = ",原创 2022-02-18 23:45:19 · 312 阅读 · 0 评论 -
pytorch中GPU数据常见的数据操作
x.data 与 x.detach() 返回的 tensor :相同:都和 x 共享同一块数据都和 x 的 计算历史无关requires_grad = False不同:y=x.data 在某些情况下不安全, 某些情况, 指的就是 上述 inplace operation 的第二种情况.detach(),从反向传播图中切割出来,不参与反向传播。但是会参与前向计算,因为储存空间共享。.cpu().item() 标量可以这样提取数值。.cpu().detach().numpy(),使用nu.原创 2020-09-28 09:56:27 · 632 阅读 · 0 评论 -
pytorch中自己写loss需要注意的地方
不要使用inplace操作比如out += residual #会导致原来的out被覆盖,反向传播出现问题。a.add_(10.) # 或者 a += 10.# RuntimeError: a leaf Variable that requires grad has been used in an in-place operation.需要反向传播,并再次赋值,正确的做法:a.data.fill_(10.)# 或者 a.detach().fill_(10.)参考博客:PyTor.原创 2020-09-28 09:45:55 · 1005 阅读 · 0 评论 -
python闭包的作用和来源
什么是闭包一个闭包例子:def print_msg(): # print_msg 是外围函数 msg = "zen of python" def printer(): # printer 是嵌套函数 print(msg) return printeranother = print_msg()# 输出 zen of pythonanother()注意,这里返回的是嵌套函数的名称,没有括号,也就是没有调用。后来给了another.原创 2020-07-23 14:28:38 · 345 阅读 · 0 评论