Skip to content

Commit c810d0b

Browse files
committed
Improving docstrings and comments in train profiler, objectives, etc
1 parent 33beaad commit c810d0b

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

neuralmonkey/attention/combination.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,14 @@ def masks_concat(self) -> tf.Tensor:
180180

181181
def initial_loop_state(self) -> AttentionLoopState:
182182

183+
# Similarly to the feed_forward attention, we need to build the encoder
184+
# projections and masks before the while loop is entered so they are
185+
# not created as a part of the loop
186+
183187
# pylint: disable=not-an-iterable
184-
# TODO blessing
185188
for val in self.encoder_projections_for_logits:
186-
debug(val)
187-
debug(self.masks_concat)
189+
debug(val, "bless")
190+
debug(self.masks_concat, "bless")
188191

189192
length = sum(tf.shape(s)[1] for s in self._encoders_tensors)
190193
# pylint: enable=not-an-iterable
@@ -195,7 +198,7 @@ def initial_loop_state(self) -> AttentionLoopState:
195198
return empty_attention_loop_state(self.batch_size, length,
196199
self.context_vector_size)
197200

198-
def get_encoder_projections(self, scope) -> List[tf.Tensor]:
201+
def get_encoder_projections(self, scope: str) -> List[tf.Tensor]:
199202
encoder_projections = []
200203
with tf.variable_scope(scope):
201204
for i, encoder_tensor in enumerate(self._encoders_tensors):

neuralmonkey/trainers/objective.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,59 +14,81 @@
1414

1515

1616
class Objective(Generic[MP]):
17-
"""The training objective.
18-
19-
Attributes:
20-
name: The name for the objective. Used in TensorBoard.
21-
decoder: The decoder which generates the value to optimize.
22-
loss: The loss tensor fetched by the trainer.
23-
gradients: Manually specified gradients. Useful for reinforcement
24-
learning.
25-
weight: The weight of this objective. The loss will be multiplied by
26-
this so the gradients can be controled in case of multiple
27-
objectives.
28-
"""
17+
"""The training objective base class."""
2918

3019
def __init__(self, name: str, decoder: MP) -> None:
20+
"""Construct the objective.
21+
22+
Arguments:
23+
name: The name for the objective. This will be used e.g. in
24+
TensorBoard.
25+
"""
3126
self._name = name
3227
self._decoder = decoder
3328

3429
@property
3530
def decoder(self) -> MP:
31+
"""Get the decoder used by the objective."""
3632
return self._decoder
3733

3834
@property
3935
def name(self) -> str:
36+
"""Get the name of the objective."""
4037
return self._name
4138

4239
@abstractproperty
4340
def loss(self) -> tf.Tensor:
41+
"""Return the loss tensor fetched by the trainer."""
4442
raise NotImplementedError()
4543

4644
@property
4745
def gradients(self) -> Optional[Gradients]:
46+
"""Manually specified gradients - useful for reinforcement learning."""
4847
return None
4948

5049
@property
5150
def weight(self) -> Optional[tf.Tensor]:
51+
"""Return the weight of this objective.
52+
53+
The loss will be multiplied by this so the gradients can be controlled
54+
in case of multiple objectives.
55+
56+
Returns:
57+
An optional tensor. If None, default weight of 1 is assumed.
58+
"""
5259
return None
5360

5461

5562
class CostObjective(Objective[GenericModelPart]):
63+
"""Cost objective class.
64+
65+
This class represent objectives that are based directly on a `cost`
66+
attribute of any compatible model part.
67+
"""
5668

5769
def __init__(self, decoder: GenericModelPart,
5870
weight: ObjectiveWeight = None) -> None:
71+
"""Construct a new instance of the `CostObjective` class.
72+
73+
Arguments:
74+
decoder: A `GenericModelPart` instance that has a `cost` attribute.
75+
weight: The weight of the objective.
76+
77+
Raises:
78+
`TypeError` when the decoder argument does not have the `cost`
79+
attribute.
80+
"""
5981
check_argument_types()
82+
if "cost" not in dir(decoder):
83+
raise TypeError("The decoder does not have the 'cost' attribute")
6084

6185
name = "{} - cost".format(str(decoder))
86+
6287
Objective[GenericModelPart].__init__(self, name, decoder)
6388
self._weight = weight
6489

6590
@tensor
6691
def loss(self) -> tf.Tensor:
67-
if "cost" not in dir(self.decoder):
68-
raise TypeError("The decoder does not have the 'cost' attribute")
69-
7092
return getattr(self.decoder, "cost")
7193

7294
@tensor

neuralmonkey/training_profiler.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77

88

99
class TrainingProfiler:
10+
"""Training profiler class.
11+
12+
This class is used for measuring inter-validation and validation times
13+
during training. It stores the training profile in the `inter_val_times`
14+
and `validation_times` lists. These can be accessed by the toolkit to
15+
provide the user with insight about the training and validation time ratio.
16+
17+
Additionally, this class provides getters for last logging and validation
18+
times, which can be used for deciding whether to log training progress
19+
or validate the model.
20+
"""
1021

1122
def __init__(self) -> None:
1223
self._start_time = None # type: Optional[float]

0 commit comments

Comments
 (0)