Get IMDB data for training and validation. Args: vocabulary_size: Size of the vocabulary, as an `int`. max_len: Cut text after this number of words. Returns: x_train: An int array of shape `(num_examples, max_len)`: index-encoded sentences. y_train: An int array of shape
(vocabulary_size, max_len)
| 71 | |
| 72 | |
| 73 | def get_imdb_data(vocabulary_size, max_len): |
| 74 | """Get IMDB data for training and validation. |
| 75 | |
| 76 | Args: |
| 77 | vocabulary_size: Size of the vocabulary, as an `int`. |
| 78 | max_len: Cut text after this number of words. |
| 79 | |
| 80 | Returns: |
| 81 | x_train: An int array of shape `(num_examples, max_len)`: index-encoded |
| 82 | sentences. |
| 83 | y_train: An int array of shape `(num_examples,)`: labels for the sentences. |
| 84 | x_test: Same as `x_train`, but for test. |
| 85 | y_test: Same as `y_train`, but for test. |
| 86 | """ |
| 87 | print("Getting IMDB data with vocabulary_size %d" % vocabulary_size) |
| 88 | (x_train, y_train), (x_test, y_test) = tf.keras.datasets.imdb.load_data( |
| 89 | num_words=vocabulary_size) |
| 90 | x_train = tf.keras.preprocessing.sequence.pad_sequences(x_train, maxlen=max_len) |
| 91 | x_test = tf.keras.preprocessing.sequence.pad_sequences(x_test, maxlen=max_len) |
| 92 | return x_train, y_train, x_test, y_test |
| 93 | |
| 94 | |
| 95 | def train_model(model_type, |