88
99class BasicUsageTest (unittest .TestCase ):
1010 def test_transformer (self ):
11+ keras .utils .set_random_seed (1337 )
12+
1113 # Tokenize some inputs with a binary label.
1214 vocab = ["[UNK]" , "the" , "qu" , "##ick" , "br" , "##own" , "fox" , "." ]
1315 sentences = ["The quick brown fox jumped." , "The fox slept." ]
@@ -32,12 +34,19 @@ def test_transformer(self):
3234 outputs = keras .layers .Dense (1 , activation = "sigmoid" )(outputs )
3335 model = keras .Model (inputs , outputs )
3436
35- # Run a single batch of gradient descent.
36- model .compile (loss = "binary_crossentropy" )
37- loss = model .train_on_batch (x , y )
37+ # Train the model end-to-end.
38+ model .compile (
39+ optimizer = keras .optimizers .Adam (learning_rate = 0.01 ),
40+ loss = "binary_crossentropy" ,
41+ )
42+ initial_loss = model .evaluate (x , y , verbose = 0 )
43+ model .fit (x , y , epochs = 5 , verbose = 0 )
44+ final_loss = model .evaluate (x , y , verbose = 0 )
3845
39- # Make sure we have a valid loss.
40- self .assertGreater (loss , 0 )
46+ # Make sure training produces a finite, decreasing loss.
47+ self .assertTrue (np .isfinite (initial_loss ))
48+ self .assertTrue (np .isfinite (final_loss ))
49+ self .assertLess (final_loss , initial_loss )
4150
4251 def test_quickstart (self ):
4352 """This roughly matches the quick start example in our base README."""
0 commit comments