首页 > 编程知识 正文

pytorch detach,tensorflow predict

时间:2023-05-04 03:19:04 阅读:175226 作者:2689

参考链接:backward(gradient=none,retain_graph=None,create_graph=False ) )。

原文及翻译:

backward(gradient=none,retain_graph=None,create_graph=False )方法:backward ) gradient=none, retain_gard create_graph=False ) computesthegradientofcurrenttensorw.r.t.graph leaves .计算图上当前gldmt对叶节点的梯度. thegraphisdifferentiatedusingthechaingthechaingt datahasmorethanoneelement ) and requires gradient,thefunctionadditionallyrequiresspecifyinggradient.itshouldbeatensororofmatchmatchmatchmatch thatcontainsthegradientofthedifferentiatedfunctionw.r.t.self .我们使用连锁定律对计算图进行微分推导来计算梯度。 如果gldmttensor不是标量,即:的数据包含多个元素,则需要为此函数指定其他梯度。指定的梯度是gldmt,必须满足一定的条件,其类型和位置必须匹配thisfunctionaccumulatesgradientsintheleaves-youmightneedtozerothembeforecallingit .此函数累计叶子节点的梯度-因此,在调用此函数之前Parameters参数gradient(tensorornone )-gradient w.r.t.the tensor.ifitisatensor, itwillbeautomaticallyconvertedtoatensorthatdoesnotrequiregradunlesscreate _ graphis true.nonevaluescanbespecifiedforscalarted . ifanonevaluewouldbeacceptablethenthisargumentisoptional.gradient (tensorgldmt或none )tensorgldmt的梯度。 对于gldmt,除非参数create_graph可以将True. None值指定给标量类型的tensor,或者指定给不需要坡度的gldmt,否则将自动转换。 此参数是可选的。 retain_graph(bool,optional )- If False, thegraphusedtocomputethegradswillbefreed.notethatinnearlyallcasessettingthisoptiontotrueisnotneedandoftencanbeworkedaro undaro efaultstothevalueofcreate _ graph.retain _ graph (布尔型,可选) -如果此参数为False,则用于计算坡度的此计算将从内存中释放。 () )。 将此选项设置为True的大多数使用情形不需要设置为True,否则通常可以更有效地执行。 此参数的默认值为create_graph的值。 create_graph(bool,optional (if true graphofthederivativewillbeconstructed,allowingtocomputehigherorderivativeproducted

该参数如果是True,导数的计算图将会被创建,可以用于 计算更高阶数的导数结果.该参数的默认值是False. Microsoft Windows [版本 10.0.18363.1256](c) 2019 Microsoft Corporation。保留所有权利。C:Userschenxuqi>conda activate ssd4pytorch1_2_0(ssd4pytorch1_2_0) C:Userschenxuqi>pythonPython 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32Type "help", "copyright", "credits" or "license" for more information.>>> import torch>>> alpha = torch.tensor([10.0, 100.0, 1000.0])>>> alphatensor([ 10., 100., 1000.])>>>>>> X = torch.tensor([4.0, 3.0, 2.0],requires_grad=True)>>> Xtensor([4., 3., 2.], requires_grad=True)>>>>>> Y = torch.zeros(3)>>> Ytensor([0., 0., 0.])>>> x0,x1,x2 = X[0],X[1],X[2]>>> y0 = 3*x0+7*x1**2+6*x2**3>>> y1 = 4*x0+8*x1**2+3*x2**3>>> y2 = 5*x0+9*x1**2+1*x2**3>>> Ytensor([0., 0., 0.])>>> Y[0],Y[1],Y[2] = y0,y1,y2>>> Ytensor([123., 112., 109.], grad_fn=<CopySlices>)>>> X.grad>>> print(X.grad)None>>> print(Y.grad)None>>>>>> Y.backward(gradient=alpha)>>> print(Y.grad)None>>> print(X.grad)tensor([ 5430., 59220., 16320.])>>>>>> # params.grad.zero_()>>>>>> Y.backward(gradient=alpha)Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:Anaconda3envsssd4pytorch1_2_0libsite-packagestorchtensor.py", line 118, in backward torch.autograd.backward(self, gradient, retain_graph, create_graph) File "D:Anaconda3envsssd4pytorch1_2_0libsite-packagestorchautograd__init__.py", line 93, in backward allow_unreachable=True) # allow_unreachable flagRuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.>>>>>>>>>>>> alpha = torch.tensor([10.0, 100.0, 1000.0])>>> alphatensor([ 10., 100., 1000.])>>> X = torch.tensor([4.0, 3.0, 2.0],requires_grad=True)>>> Y = torch.zeros(3)>>> x0,x1,x2 = X[0],X[1],X[2]>>> y0 = 3*x0+7*x1**2+6*x2**3>>> y1 = 4*x0+8*x1**2+3*x2**3>>> y2 = 5*x0+9*x1**2+1*x2**3>>> Ytensor([0., 0., 0.])>>> Y[0],Y[1],Y[2] = y0,y1,y2>>> Ytensor([123., 112., 109.], grad_fn=<CopySlices>)>>> print(X.grad)None>>> print(Y.grad)None>>> Y.backward(gradient=alpha,retain_graph=True)>>> print(X.grad)tensor([ 5430., 59220., 16320.])>>> print(Y.grad)None>>> Y.backward(gradient=alpha,retain_graph=True)>>> print(X.grad)tensor([ 10860., 118440., 32640.])>>> print(Y.grad)None>>> Y.backward(gradient=alpha,retain_graph=True)>>> print(X.grad)tensor([ 16290., 177660., 48960.])>>> print(Y.grad)None>>> X.grad.zero_()tensor([0., 0., 0.])>>> print(X.grad)tensor([0., 0., 0.])>>> print(Y.grad)None>>> Y.backward(gradient=alpha,retain_graph=True)>>> print(X.grad)tensor([ 5430., 59220., 16320.])>>> print(Y.grad)None>>>>>>>>> X.grad.zero_()tensor([0., 0., 0.])>>> print(X.grad)tensor([0., 0., 0.])>>> print(Y.grad)None>>> Y.backward(gradient=alpha)>>> print(X.grad)tensor([ 5430., 59220., 16320.])>>> print(Y.grad)None>>>>>>>>>

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。