微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用JAX和autorgrad对皮肤进行反向传播?

如何解决如何使用JAX和autorgrad对皮肤进行反向传播?

有时候,我建立了一个基于function ParentComponent(){ // do sth to get the userinfo here return( <ParentComponent> {userinfo? <Profile userinfo={userinfo} /> : null} <ParentComponent/> ) } 的机器学习“图书馆”作为学校的作业。它纯粹基于numpy,但现在我想将其转换为JAX。我在调整反向传播过程时遇到了麻烦。

该库模拟pytorch,因此每一层都是具有numpyforward方法的类。在backward中,我的线性层是

numpy

现在我很难将其转换为JAX。我试图定义一个class Linear: def __init__(self,in_features: int,out_features: int): self.W = np.random.randn(in_features,out_features) self.b = np.random.randn(out_features) def forward(self,x: Tensor) -> Tensor: self.input = x return x @ self.W + self.b def backward(self,grad: Tensor) -> Tensor: # in_feat by batch_size @ batch_size by out_feat self.dydw = self.input.T @ grad # we sum across batches and get shape (out_features) self.dydb = grad.sum(axis=0) # output must be of shape (batch_size,out_features) return grad @ self.W.T 方法

__matmul__

然后使用def __matmul__( self,weight: Tensor,input_: Tensor,bias: Tensor ) -> Tensor: return jnp.dot(weight,input_.T) + bias

jax.grad

不起作用。如果仅在def __grad__(self,) -> Tuple[Callable,Callable,Callable]: return ( grad(self.__matmul__,argnums=0),grad(self.__matmul__,argnums=1),argnums=2),) 输出上使用grad,则结果似乎不正确。在这里使用JAX的autograd的正确方法是什么?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。