Module: Torch::NN::Init

Defined in:
lib/torch/nn/init.rb

Class Method Summary collapse

Class Method Details

._calculate_fan_in_and_fan_out(tensor) ⇒ Object

TODO move to C++ when released



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/torch/nn/init.rb', line 48

def _calculate_fan_in_and_fan_out(tensor)
  dimensions = tensor.dim
  if dimensions < 2
    raise Error, "Fan in and fan out can not be computed for tensor with fewer than 2 dimensions"
  end

  if dimensions == 2
    fan_in = tensor.size(1)
    fan_out = tensor.size(0)
  else
    num_input_fmaps = tensor.size(1)
    num_output_fmaps = tensor.size(0)
    receptive_field_size = 1
    if tensor.dim > 2
      receptive_field_size = tensor[0][0].numel
    end
    fan_in = num_input_fmaps * receptive_field_size
    fan_out = num_output_fmaps * receptive_field_size
  end

  [fan_in, fan_out]
end

.calculate_gain(nonlinearity, param: 0.01) ⇒ Object



5
6
7
# File 'lib/torch/nn/init.rb', line 5

def calculate_gain(nonlinearity, param: 0.01)
  _calculate_gain(nonlinearity, param)
end

.kaiming_normal!(tensor, a: 0, mode: "fan_in", nonlinearity: "leaky_relu") ⇒ Object



35
36
37
# File 'lib/torch/nn/init.rb', line 35

def kaiming_normal!(tensor, a: 0, mode: "fan_in", nonlinearity: "leaky_relu")
  _kaiming_normal!(tensor, a, mode, nonlinearity)
end

.kaiming_uniform!(tensor, a: 0, mode: "fan_in", nonlinearity: "leaky_relu") ⇒ Object



31
32
33
# File 'lib/torch/nn/init.rb', line 31

def kaiming_uniform!(tensor, a: 0, mode: "fan_in", nonlinearity: "leaky_relu")
  _kaiming_uniform!(tensor, a, mode, nonlinearity)
end

.normal!(tensor, mean: 0.0, std: 1.0) ⇒ Object



13
14
15
# File 'lib/torch/nn/init.rb', line 13

def normal!(tensor, mean: 0.0, std: 1.0)
  _normal!(tensor, mean, std)
end

.orthogonal!(tensor, gain: 1) ⇒ Object



39
40
41
# File 'lib/torch/nn/init.rb', line 39

def orthogonal!(tensor, gain: 1)
  _orthogonal!(tensor, gain)
end

.sparse!(tensor, sparsity, std: 0.01) ⇒ Object



43
44
45
# File 'lib/torch/nn/init.rb', line 43

def sparse!(tensor, sparsity, std: 0.01)
  _sparse!(tensor, sparsity, std)
end

.uniform!(tensor, a: 0.0, b: 1.0) ⇒ Object



9
10
11
# File 'lib/torch/nn/init.rb', line 9

def uniform!(tensor, a: 0.0, b: 1.0)
  _uniform!(tensor, a, b)
end

.xavier_normal!(tensor, gain: 1.0) ⇒ Object



27
28
29
# File 'lib/torch/nn/init.rb', line 27

def xavier_normal!(tensor, gain: 1.0)
  _xavier_normal!(tensor, gain)
end

.xavier_uniform!(tensor, gain: 1.0) ⇒ Object



23
24
25
# File 'lib/torch/nn/init.rb', line 23

def xavier_uniform!(tensor, gain: 1.0)
  _xavier_uniform!(tensor, gain)
end