()
| 156 | |
| 157 | |
| 158 | def main(): |
| 159 | x_train, y_train, x_test, y_test = ( |
| 160 | get_imdb_data(FLAGS.vocabulary_size, FLAGS.max_len)) |
| 161 | |
| 162 | model = train_model(FLAGS.model_type, |
| 163 | FLAGS.vocabulary_size, |
| 164 | FLAGS.embedding_size, |
| 165 | x_train, |
| 166 | y_train, |
| 167 | x_test, |
| 168 | y_test, |
| 169 | FLAGS.epochs, |
| 170 | FLAGS.batch_size) |
| 171 | |
| 172 | # Display a number test phrases and their final classification. |
| 173 | forward_index = get_word_index() |
| 174 | reverse_index = get_word_index(reverse=True) |
| 175 | print('\n') |
| 176 | for i in range(FLAGS.num_show): |
| 177 | print('--- Test Case %d ---' % (i + 1)) |
| 178 | print('Sentence: "' + |
| 179 | ' '.join(indices_to_words(reverse_index, x_test[i, :])) + '"') |
| 180 | print('Truth: %d' % y_test[i]) |
| 181 | print('Prediction: %s\n' % model.predict(x_test[i : i + 1, :])[0][0]) |
| 182 | |
| 183 | # Save metadata, including word index, INDEX_FROM and max_len and model |
| 184 | # hyperparameters. |
| 185 | metadata = { |
| 186 | 'word_index': forward_index, |
| 187 | 'index_from': INDEX_FROM, |
| 188 | 'max_len': FLAGS.max_len, |
| 189 | 'model_type': FLAGS.model_type, |
| 190 | 'vocabulary_size': FLAGS.vocabulary_size, |
| 191 | 'embedding_size': FLAGS.embedding_size, |
| 192 | 'epochs': FLAGS.epochs, |
| 193 | 'batch_size': FLAGS.batch_size, |
| 194 | } |
| 195 | |
| 196 | if not os.path.isdir(FLAGS.artifacts_dir): |
| 197 | os.makedirs(FLAGS.artifacts_dir) |
| 198 | metadata_json_path = os.path.join(FLAGS.artifacts_dir, 'metadata.json') |
| 199 | json.dump(metadata, open(metadata_json_path, 'wt')) |
| 200 | print('\nSaved model metadata at: %s' % metadata_json_path) |
| 201 | |
| 202 | tfjs.converters.save_keras_model(model, FLAGS.artifacts_dir) |
| 203 | print('\nSaved model artifacts in directory: %s' % FLAGS.artifacts_dir) |
| 204 | |
| 205 | |
| 206 | if __name__ == '__main__': |
no test coverage detected