MCPcopy Create free account
hub / github.com/pytorch/pytorch / apply_over_sequence

Method apply_over_sequence

caffe2/python/rnn_cell.py:75–140  ·  view source on GitHub ↗
(
        self,
        model,
        inputs,
        seq_lengths=None,
        initial_states=None,
        outputs_with_grads=None,
    )

Source from the content-addressed store, hash-verified

73 return self.name + '/' + name if self.name is not None else name
74
75 def apply_over_sequence(
76 self,
77 model,
78 inputs,
79 seq_lengths=None,
80 initial_states=None,
81 outputs_with_grads=None,
82 ):
83 if initial_states is None:
84 with scope.NameScope(self.name):
85 if self.initializer is None:
86 raise Exception("Either initial states "
87 "or initializer have to be set")
88 initial_states = self.initializer.create_states(model)
89
90 preprocessed_inputs = self.prepare_input(model, inputs)
91 step_model = ModelHelper(name=self.name, param_model=model)
92 input_t, timestep = step_model.net.AddScopedExternalInputs(
93 'input_t',
94 'timestep',
95 )
96 utils.raiseIfNotEqual(
97 len(initial_states), len(self.get_state_names()),
98 "Number of initial state values provided doesn't match the number "
99 "of states"
100 )
101 states_prev = step_model.net.AddScopedExternalInputs(*[
102 s + '_prev' for s in self.get_state_names()
103 ])
104 states = self._apply(
105 model=step_model,
106 input_t=input_t,
107 seq_lengths=seq_lengths,
108 states=states_prev,
109 timestep=timestep,
110 )
111
112 external_outputs = set(step_model.net.Proto().external_output)
113 for state in states:
114 if state not in external_outputs:
115 step_model.net.AddExternalOutput(state)
116
117 if outputs_with_grads is None:
118 outputs_with_grads = [self.get_output_state_index() * 2]
119
120 # states_for_all_steps consists of combination of
121 # states gather for all steps and final states. It looks like this:
122 # (state_1_all, state_1_final, state_2_all, state_2_final, ...)
123 states_for_all_steps = recurrent.recurrent_net(
124 net=model.net,
125 cell_net=step_model.net,
126 inputs=[(input_t, preprocessed_inputs)],
127 initial_cell_inputs=list(zip(states_prev, initial_states)),
128 links=dict(zip(states_prev, states)),
129 timestep=timestep,
130 scope=self.name,
131 forward_only=self.forward_only,
132 outputs_with_grads=outputs_with_grads,

Callers 2

_LSTMFunction · 0.45
LSTMWithAttentionFunction · 0.45

Calls 12

prepare_inputMethod · 0.95
get_state_namesMethod · 0.95
_applyMethod · 0.95
ModelHelperClass · 0.90
ExceptionClass · 0.85
listFunction · 0.85
AddExternalOutputMethod · 0.80
create_statesMethod · 0.45
ProtoMethod · 0.45

Tested by

no test coverage detected