Skip to content

Commit d33f03e

Browse files
committed
Lazify cost objective (hasattr uses getattr)
1 parent 24df477 commit d33f03e

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

neuralmonkey/trainers/objective.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,15 @@ def __init__(self, decoder: GenericModelPart,
5858
weight: ObjectiveWeight = None) -> None:
5959
check_argument_types()
6060

61-
if not hasattr(decoder, "cost"):
62-
raise TypeError("The decoder does not have a `cost` attribute")
63-
6461
name = "{} - cost".format(str(decoder))
6562
Objective[GenericModelPart].__init__(self, name, decoder)
6663
self._weight = weight
6764

6865
@tensor
6966
def loss(self) -> tf.Tensor:
70-
assert hasattr(self.decoder, "cost")
67+
if not hasattr(self.decoder, "cost"):
68+
raise TypeError("The decoder does not have the 'cost' attribute")
69+
7170
return getattr(self.decoder, "cost")
7271

7372
@tensor

0 commit comments

Comments
 (0)