Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Machine learning Pytorch,切片张量导致运行时错误::梯度计算所需的变量之一已通过就地操作修改:_Machine Learning_Regression_Pytorch - Fatal编程技术网

Machine learning Pytorch,切片张量导致运行时错误::梯度计算所需的变量之一已通过就地操作修改:

Machine learning Pytorch,切片张量导致运行时错误::梯度计算所需的变量之一已通过就地操作修改:,machine-learning,regression,pytorch,Machine Learning,Regression,Pytorch,我用Pycharm用LSTM cell写了一个RNN。这种网络的特点是,RNN的输出被输入到积分运算中,用龙格库塔法计算。 集成需要一些输入,并在时间上提前一步进行传播。为了做到这一点,我需要沿着批处理维度切片特征张量X,并将其传递给Runge-kutta MyLSTM类(火炬模块): 定义初始值(self,ni,no,采样间隔,nh=10,nlayers=1): 超级(MyLSTM,self)。\uuuu init\uuuuu() self.device=火炬装置(“cpu”) self.dt

我用Pycharm用LSTM cell写了一个RNN。这种网络的特点是,RNN的输出被输入到积分运算中,用龙格库塔法计算。 集成需要一些输入,并在时间上提前一步进行传播。为了做到这一点,我需要沿着批处理维度切片特征张量X,并将其传递给Runge-kutta

MyLSTM类(火炬模块):
定义初始值(self,ni,no,采样间隔,nh=10,nlayers=1):
超级(MyLSTM,self)。\uuuu init\uuuuu()
self.device=火炬装置(“cpu”)
self.dtype=torch.float
self.ni=ni
self.no=否
self.nh=nh
self.nlayers=nlayers
self.lstms=torch.nn.ModuleList(
[torch.nn.LSTMCell(self.ni,self.nh)]+[torch.nn.LSTMCell(self.nh,self.nh)用于范围内的i(nlayers-1)])
self.out=torch.nn.Linear(self.nh,self.no)
self.do=torch.nn.辍学率(p=0.2)
self.actfn=torch.nn.Sigmoid()
self.sampling\u interval=采样间隔
self.scaler_states=无
#选择权
#整个区块的描述
def向前(自身、x、h0、序列=假、积分=真):
x0=x.clone().需要梯度(真)
hs=x#启动隐藏状态
如果h0为无:
h=焊炬零点(hs.形状[0],self.nh,设备=self.device)
c=焊炬零点(hs.形状[0],self.nh,设备=self.device)
其他:
(h,c)=h0
#LSTM单元
对于范围内的i(self.nlayers):
h、 c=自身lstms[i](hs,(h,c))
如果列车:
hs=自身do(h)
其他:
hs=h
#输出层
#y=自动作Fn(自输出(hs))
y=自输出(hs)
如果集成代码:
p=y
y=自积分(x0,p)
返回y,(h,c)
def集成(自、x0、p):
#RK4每间隔步数
M=4
DT=自采样间隔/M
X=x0
#X=self.scaler\u特征。逆变换(x0)
对于范围内的b(X.shape[0]):
xx=X[b,:]
对于范围内的j(M):
k1=self.ode(xx,p[b,:]))
k2=self.ode(xx+DT/2*k1,p[b:])
k3=自赋(xx+DT/2*k2,p[b,:])
k4=自赋(xx+DT*k3,p[b:])
xx=xx+DT/6*(k1+2*k2+2*k3+k4)
X_all[b,:]=xx
返回X_all
定义常微分方程(自、x0、y):
#这里我要介绍一个动态模型
我得到这个错误:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor []], which is output 0 of SelectBackward, is at version 64; expected version 63 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).
问题出在操作中
xx=X[b,:]
p[b,:]
。我知道,因为我选择了批次维度1,所以我可以用
xx=X
p
替换前面的两个方程,这是可行的。如何在不失去梯度的情况下分裂张量