fix add broadcasted backward grad wrong values
This commit is contained in:
parent
acb6c17cca
commit
418bb20b29
5 changed files with 2054 additions and 536 deletions
2574
examples/train.ipynb
2574
examples/train.ipynb
File diff suppressed because one or more lines are too long
Binary file not shown.
|
|
@ -132,7 +132,7 @@ class SumBackward:
|
|||
self.keepdim = keepdim
|
||||
|
||||
def backward(self, gradient):
|
||||
input_shape = self.input[0].shape
|
||||
input_shape = self.input[0].shape.copy()
|
||||
if self.axis == -1:
|
||||
# If axis is None, sum reduces the tensor to a scalar.
|
||||
grad_output = float(gradient.tensor.contents.data[0]) * self.input[0].ones_like()
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -2,15 +2,23 @@ from ..module import Module
|
|||
from ..parameter import Parameter
|
||||
|
||||
class Linear(Module):
|
||||
def __init__(self, input_dim, output_dim):
|
||||
def __init__(self, input_dim, output_dim, bias=True):
|
||||
super().__init__()
|
||||
self.input_dim = input_dim
|
||||
self.output_dim = output_dim
|
||||
self.weight = Parameter(shape=[self.output_dim, self.input_dim])
|
||||
self.bias = Parameter(shape=[self.output_dim, 1])
|
||||
|
||||
if bias:
|
||||
self.bias = Parameter(shape=[self.output_dim, 1])
|
||||
else:
|
||||
self.bias = None
|
||||
|
||||
def forward(self, x):
|
||||
z = self.weight @ x + self.bias
|
||||
if self.bias:
|
||||
z = self.weight @ x + self.bias
|
||||
else:
|
||||
z = self.weight @ x
|
||||
|
||||
return z
|
||||
|
||||
def inner_repr(self):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue