def forward(self, *args, **kwargs): self.args = [self._convert2array(arg) for arg in args] if self.enable_auto_broadcast: self.args = self._autobroadcast(self.args) self.kwargs = kwargs out = self._forward(*tuple(arg.value for arg in self.args), **kwargs) if config.enable_backprop: return Array(out, parent=self) else: return Array(out, parent=None)
def __init__(self, kernel, stride, pad): super().__init__() self.in_ch = kernel.shape[-2] self.out_ch = kernel.shape[-1] self.kernel_size = kernel.shape[:2] self.stride = stride self.pad = pad kernel = kernel.value with self.set_parameter(): self.w = Array(kernel.reshape(-1, kernel.shape[-1]))
class Convolve2d(Network): def __init__(self, kernel, stride, pad): super().__init__() self.in_ch = kernel.shape[-2] self.out_ch = kernel.shape[-1] self.kernel_size = kernel.shape[:2] self.stride = stride self.pad = pad kernel = kernel.value with self.set_parameter(): self.w = Array(kernel.reshape(-1, kernel.shape[-1])) @property def kernel(self): return self.w.reshape(*self.kernel_size, self.in_ch, self.out_ch) def __call__(self, x): func = Convolve2dFunction(self.kernel_size, self.stride, self.pad) return func.forward(x, self.w)
def zeros(size): return Array(np.zeros(size, dtype=config.dtype))
def ones(size): return Array(np.ones(size, dtype=config.dtype))