|
31 | 31 | class Model(tf.keras.Model):
|
32 | 32 | """Keras model for program generation using RNN."""
|
33 | 33 |
|
34 |
| - def __init__(self, vocabulary: list, num_ops_per_sample: int, num_nops_separator: int, |
35 |
| - embedding_dim: int, num_rnn_units: int): |
| 34 | + def __init__(self, vocabulary: list, |
| 35 | + embedding_dim: int, num_rnn_units: int, |
| 36 | + num_samples: int, sample_size: int, |
| 37 | + num_ops_per_sample: int, num_nops_separator: int, |
| 38 | + program_ids: list): |
36 | 39 |
|
37 | 40 | super().__init__(self)
|
38 | 41 | self.vocabulary = vocabulary
|
39 |
| - self.num_ops_per_sample = num_ops_per_sample |
40 |
| - self.num_nops_separator = num_nops_separator |
41 | 42 | self.embedding_dim = embedding_dim
|
42 | 43 | self.num_rnn_units = num_rnn_units
|
| 44 | + self.num_samples = num_samples |
| 45 | + self.sample_size = sample_size |
| 46 | + self.num_ops_per_sample = num_ops_per_sample |
| 47 | + self.num_nops_separator = num_nops_separator |
| 48 | + self.program_ids = program_ids |
43 | 49 |
|
44 | 50 | # Initialize token <-> ID lookup layers.
|
45 | 51 | self.tokens_to_ids = tf.keras.layers.StringLookup(
|
@@ -73,10 +79,22 @@ def call(self, inputs, states=None, return_state=False, training=False):
|
73 | 79 |
|
74 | 80 | def get_config(self):
|
75 | 81 | return {"vocabulary": self.vocabulary,
|
| 82 | + "embedding_dim": self.embedding_dim, |
| 83 | + "num_rnn_units": self.num_rnn_units, |
| 84 | + "num_samples": self.num_samples, |
| 85 | + "sample_size": self.sample_size, |
76 | 86 | "num_ops_per_sample": self.num_ops_per_sample,
|
77 | 87 | "num_nops_separator": self.num_nops_separator,
|
78 |
| - "embedding_dim": self.embedding_dim, |
79 |
| - "num_rnn_units": self.num_rnn_units} |
| 88 | + "program_ids": self.program_ids} |
| 89 | + |
| 90 | + def summary(self, line_length=None, positions=None, print_fn=None, |
| 91 | + expand_nested=False, show_trainable=False, layer_range=None): |
| 92 | + super().summary(line_length, positions, print_fn, |
| 93 | + expand_nested, show_trainable, layer_range) |
| 94 | + print("Vocabulary size:", self.get_vocab_size()) |
| 95 | + print("Sample size:", self.sample_size) |
| 96 | + print("Trained samples:", self.num_samples) |
| 97 | + print("Trained programs:", len(self.program_ids)) |
80 | 98 |
|
81 | 99 | @classmethod
|
82 | 100 | def from_config(cls, config):
|
@@ -287,20 +305,26 @@ def train_model(program_cache: ProgramCache, num_programs: int = -1,
|
287 | 305 | Return:
|
288 | 306 | This function returns the trained Keras model.
|
289 | 307 | """
|
| 308 | + # Get random program IDs. |
| 309 | + program_ids = util.get_random_program_ids(program_cache, num_programs) |
| 310 | + |
290 | 311 | # Load programs and convert to tokens and vocabulary.
|
291 |
| - merged_programs, _, sample_size = util.merge_programs( |
292 |
| - program_cache, |
293 |
| - num_programs=num_programs, |
| 312 | + merged_programs, num_samples, sample_size = util.merge_programs( |
| 313 | + program_cache, program_ids, |
294 | 314 | num_ops_per_sample=num_ops_per_sample,
|
295 | 315 | num_nops_separator=num_nops_separator)
|
296 | 316 | tokens, vocabulary = util.program_to_tokens(merged_programs)
|
297 | 317 |
|
298 | 318 | # Create Keras model and dataset, run the training, and save the model.
|
| 319 | + program_ids = sorted(program_ids) |
299 | 320 | model = Model(vocabulary,
|
| 321 | + embedding_dim=embedding_dim, |
| 322 | + num_rnn_units=num_rnn_units, |
| 323 | + num_samples=num_samples, |
| 324 | + sample_size=sample_size, |
300 | 325 | num_ops_per_sample=num_ops_per_sample,
|
301 | 326 | num_nops_separator=num_nops_separator,
|
302 |
| - embedding_dim=embedding_dim, |
303 |
| - num_rnn_units=num_rnn_units) |
| 327 | + program_ids=program_ids) |
304 | 328 | ids = model.tokens_to_ids(tokens)
|
305 | 329 | dataset = __create_dataset(ids, sample_size=sample_size)
|
306 | 330 | loss = tf.losses.SparseCategoricalCrossentropy(from_logits=True)
|
|
0 commit comments