Class: Torch::NN::Conv2d

Inherits:
Module
  • Object
show all
Defined in:
lib/torch/nn/conv2d.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Module

#method_missing, #parameters, #respond_to?, #zero_grad

Constructor Details

#initialize(in_channels, out_channels, kernel_size, stride: 1, padding: 0) ⇒ Conv2d

, dilation: 1, groups: 1)



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/torch/nn/conv2d.rb', line 6

def initialize(in_channels, out_channels, kernel_size, stride: 1, padding: 0) #, dilation: 1, groups: 1)
  @in_channels = in_channels
  @out_channels = out_channels
  @kernel_size = pair(kernel_size)
  @stride = pair(stride)
  @padding = pair(padding)
  # @dilation = pair(dilation)

  # TODO divide by groups
  @weight = Parameter.new(Tensor.new(out_channels, in_channels, *@kernel_size))
  @bias = Parameter.new(Tensor.new(out_channels))

  reset_parameters
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Torch::NN::Module

Instance Attribute Details

#biasObject (readonly)

Returns the value of attribute bias.



4
5
6
# File 'lib/torch/nn/conv2d.rb', line 4

def bias
  @bias
end

#weightObject (readonly)

Returns the value of attribute weight.



4
5
6
# File 'lib/torch/nn/conv2d.rb', line 4

def weight
  @weight
end

Instance Method Details

#call(input) ⇒ Object



30
31
32
# File 'lib/torch/nn/conv2d.rb', line 30

def call(input)
  F.conv2d(input, @weight, @bias, stride: @stride, padding: @padding) #, @dilation, @groups)
end

#inspectObject



34
35
36
# File 'lib/torch/nn/conv2d.rb', line 34

def inspect
  "Conv2d(#{@in_channels}, #{@out_channels}, kernel_size: #{@kernel_size.inspect}, stride: #{@stride.inspect})"
end

#reset_parametersObject



21
22
23
24
25
26
27
28
# File 'lib/torch/nn/conv2d.rb', line 21

def reset_parameters
  Init.kaiming_uniform_(@weight, Math.sqrt(5))
  if @bias
    fan_in, _ = Init.calculate_fan_in_and_fan_out(@weight)
    bound = 1 / Math.sqrt(fan_in)
    Init.uniform_(@bias, -bound, bound)
  end
end