首页 > 技术文章 > index_select ,clamp,detach

573177885qq 2018-06-01 15:42 原文

 


1、torch.clamp(input,min,max,out=None)-> Tensor

     将input中的元素限制在[min,max]范围内并返回一个Tensor

2、index_select()

 x = torch.randn(3, 4)
 print(x)

indices = torch.LongTensor([0, 2]) y = torch.index_select(x, 0, indices) print(y)

z = torch.index_select(x, 1, indices) print(z)

 

参考:https://blog.csdn.net/appleml/article/details/78630452 

3、detach
从当前图中分离的 Variable,返回的 Variable 永远不会需要梯度
用途:如果我们有两个网络A,B,两个关系是这样的y=A(x),z=B(y),现在我们想用z.backward()来为B网络的参数来求梯度,但是又不想求
A网络参数的梯度,我们可以这样https://blog.csdn.net/u012436149/article/details/76714349
# y=A(x), z=B(y) 求B中参数的梯度,不求A中参数的梯度
# 第一种方法
y = A(x)
z = B(y.detach())
z.backward()

 



 

推荐阅读