def conv1d_input(input_size, weight, grad_output, stride=1, padding=0, dilation=1, groups=1, bias=None): r""" Computes the gradient of conv1d with respect to the input of the convolution. This is same as the 1D transposed convolution operator under the hood but requires the shape of the gradient w.r.t. input to be specified explicitly. Args: input_size : Shape of the input gradient tensor weight: weight tensor (out_channels x in_channels/groups x kW) grad_output : output gradient tensor (minibatch x out_channels x oW) stride (int or tuple, optional): Stride of the convolution. Default: 1 padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0 dilation (int or tuple, optional): Spacing between kernel elements. Default: 1 groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1 bias: optional bias tensor (out_channels). Default: None Examples:: >>> input = torch.randn(1,1,3, requires_grad=True) >>> weight = torch.randn(1,1,1, requires_grad=True) >>> output = F.conv1d(input, weight) >>> grad_output = torch.randn(output.shape) >>> grad_input = torch.autograd.grad(output, input, grad_output) >>> F.grad.conv1d_input(input.shape, weight, grad_output) """ stride = _single(stride) padding = _single(padding) dilation = _single(dilation) kernel_size = [weight.shape[2]] if input_size is None: raise ValueError("grad.conv1d_input requires specifying an input_size") grad_input_padding = _grad_input_padding(grad_output, input_size, stride, padding, kernel_size) return torch.conv_transpose1d(grad_output, weight, bias, stride, padding, grad_input_padding, groups, dilation)
def pwm_conv1d_input(input, weight, bg_tensor, grad_output, stride=1, padding=0, dilation=1, groups=1): """ Gradient calculation for the filter weights of the PWM-based convolution. Parameters ---------- input : tensor input tensor weight : tensor weight tensor bg_tensor : tensor background probabilities tensor grad_output : tensor Gradient output from backwards pass Returns ------- Calculated gradients """ stride = _single(stride) padding = _single(padding) dilation = _single(dilation) kernel_size = [weight.shape[2]] input_size = input.shape if input_size is None: raise ValueError("grad.conv1d_input requires specifying an input_size") grad_input_padding = _grad_input_padding(grad_output, input_size, stride, padding, kernel_size) pwm = weight.div(bg_tensor).log2() return torch.conv_transpose1d(grad_output, pwm, None, stride, padding, grad_input_padding, groups, dilation)
def conv1d_input(input_size, weight, grad_output, stride=1, padding=0, dilation=1, groups=1, bias=None): r""" Computes the gradient of conv1d with respect to the input of the convolution. This is same as the 1D transposed convolution operator under the hood but requires the shape of the gradient w.r.t. input to be specified explicitly. Args: input_size : Shape of the input gradient tensor weight: weight tensor (out_channels x in_channels/groups x kW) grad_output : output gradient tensor (minibatch x out_channels x oW) stride (int or tuple, optional): Stride of the convolution. Default: 1 padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0 dilation (int or tuple, optional): Spacing between kernel elements. Default: 1 groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1 bias: optional bias tensor (out_channels). Default: None Examples:: >>> input = torch.randn(1,1,3, requires_grad=True) >>> weight = torch.randn(1,1,1, requires_grad=True) >>> output = F.conv1d(input, weight) >>> grad_output = torch.randn(output.shape) >>> grad_input = torch.autograd.grad(output, input, grad_output) >>> F.grad.conv1d_input(input.shape, weight, grad_output) """ stride = _single(stride) padding = _single(padding) dilation = _single(dilation) kernel_size = [weight.shape[2]] if input_size is None: raise ValueError("grad.conv1d_input requires specifying an input_size") grad_input_padding = _grad_input_padding(grad_output, input_size, stride, padding, kernel_size) return torch.conv_transpose1d( grad_output, weight, bias, stride, padding, grad_input_padding, groups, dilation)
def conv_transpose1d(input, *args, **kwargs): return torch.conv_transpose1d(input.q, *args, **kwargs)