Class: Torch::NN::GRU

Inherits:
RNNBase show all
Defined in:
lib/torch/nn/gru.rb

Instance Method Summary collapse

Methods inherited from RNNBase

#_apply, #extra_inspect, #flatten_parameters, #permute_hidden, #reset_parameters

Methods inherited from Module

#_apply, #add_module, #apply, #buffers, #call, #children, #cpu, #cuda, #double, #eval, #float, #half, #inspect, #load_state_dict, #method_missing, #modules, #named_buffers, #named_children, #named_modules, #named_parameters, #parameters, #register_buffer, #register_parameter, #requires_grad!, #respond_to?, #share_memory, #state_dict, #to, #train, #type, #zero_grad

Methods included from Utils

#_ntuple, #_pair, #_quadrupal, #_single, #_triple

Constructor Details

#initialize(*args, **options) ⇒ GRU

Returns a new instance of GRU.



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

def initialize(*args, **options)
  super("GRU", *args, **options)
end

Dynamic Method Handling

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

Instance Method Details

#forward(input, hx: nil) ⇒ Object



44
45
46
# File 'lib/torch/nn/gru.rb', line 44

def forward(input, hx: nil)
  forward_tensor(input, hx: hx)
end

#forward_impl(input, hx, batch_sizes, max_batch_size, sorted_indices) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/torch/nn/gru.rb', line 18

def forward_impl(input, hx, batch_sizes, max_batch_size, sorted_indices)
  if hx.nil?
    num_directions = @bidirectional ? 2 : 1
    hx = Torch.zeros(@num_layers * num_directions, max_batch_size, @hidden_size, dtype: input.dtype, device: input.device)
  else
    # Each batch of the hidden state should match the input sequence that
    # the user believes he/she is passing in.
    hx = permute_hidden(hx, sorted_indices)
  end

  check_forward_args(input, hx, batch_sizes)
  result = run_impl(input, hx, batch_sizes)
  output = result[0]
  hidden = result[1]
  [output, hidden]
end

#forward_tensor(input, hx: nil) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/torch/nn/gru.rb', line 35

def forward_tensor(input, hx: nil)
  batch_sizes = nil
  max_batch_size = @batch_first ? input.size(0) : input.size(1)
  sorted_indices = nil
  unsorted_indices = nil
  output, hidden = forward_impl(input, hx, batch_sizes, max_batch_size, sorted_indices)
  [output, permute_hidden(hidden, unsorted_indices)]
end

#run_impl(input, hx, batch_sizes) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/torch/nn/gru.rb', line 8

def run_impl(input, hx, batch_sizes)
  if batch_sizes.nil?
    Torch.gru(input, hx, _get_flat_weights, @bias, @num_layers,
                       @dropout, @training, @bidirectional, @batch_first)
  else
    Torch.gru(input, batch_sizes, hx, _get_flat_weights, @bias,
                       @num_layers, @dropout, @training, @bidirectional)
  end
end