Merge branch 'master' into changes-chap16

This commit is contained in:
Aurélien Geron
2021-03-02 11:06:15 +13:00
committed by GitHub
33 changed files with 1709 additions and 1234 deletions

View File

@@ -309,6 +309,20 @@
"## Creating and Training the Model"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Warning**: the following code may take up to 24 hours to run, depending on your hardware. If you use a GPU, it may take just 1 or 2 hours, or less."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Note**: the `GRU` class will only use the GPU (if you have one) when using the default values for the following arguments: `activation`, `recurrent_activation`, `recurrent_dropout`, `unroll`, `use_bias` and `reset_after`. This is why I commented out `recurrent_dropout=0.2` (compared to the book)."
]
},
{
"cell_type": "code",
"execution_count": 18,
@@ -317,9 +331,11 @@
"source": [
"model = keras.models.Sequential([\n",
" keras.layers.GRU(128, return_sequences=True, input_shape=[None, max_id],\n",
" dropout=0.2, recurrent_dropout=0.2),\n",
" #dropout=0.2, recurrent_dropout=0.2),\n",
" dropout=0.2),\n",
" keras.layers.GRU(128, return_sequences=True,\n",
" dropout=0.2, recurrent_dropout=0.2),\n",
" #dropout=0.2, recurrent_dropout=0.2),\n",
" dropout=0.2),\n",
" keras.layers.TimeDistributed(keras.layers.Dense(max_id,\n",
" activation=\"softmax\"))\n",
"])\n",
@@ -346,6 +362,13 @@
" return tf.one_hot(X, max_id)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Warning**: the `predict_classes()` method is deprecated. Instead, we must use `np.argmax(model.predict(X_new), axis=-1)`."
]
},
{
"cell_type": "code",
"execution_count": 20,
@@ -353,6 +376,7 @@
"outputs": [],
"source": [
"X_new = preprocess([\"How are yo\"])\n",
"#Y_pred = model.predict_classes(X_new)\n",
"Y_pred = np.argmax(model.predict(X_new), axis=-1)\n",
"tokenizer.sequences_to_texts(Y_pred + 1)[0][-1] # 1st sentence, last char"
]
@@ -614,7 +638,7 @@
"metadata": {},
"outputs": [],
"source": [
"(X_train, y_test), (X_valid, y_test) = keras.datasets.imdb.load_data()"
"(X_train, y_train), (X_test, y_test) = keras.datasets.imdb.load_data()"
]
},
{
@@ -1785,6 +1809,7 @@
"metadata": {},
"outputs": [],
"source": [
"#ids = model.predict_classes(X_new)\n",
"ids = np.argmax(model.predict(X_new), axis=-1)\n",
"for date_str in ids_to_date_strs(ids):\n",
" print(date_str)"
@@ -1819,6 +1844,7 @@
"metadata": {},
"outputs": [],
"source": [
"#ids = model.predict_classes(X_new)\n",
"ids = np.argmax(model.predict(X_new), axis=-1)\n",
"for date_str in ids_to_date_strs(ids):\n",
" print(date_str)"
@@ -1847,6 +1873,7 @@
"\n",
"def convert_date_strs(date_strs):\n",
" X = prepare_date_strs_padded(date_strs)\n",
" #ids = model.predict_classes(X)\n",
" ids = np.argmax(model.predict(X), axis=-1)\n",
" return ids_to_date_strs(ids)"
]
@@ -2226,7 +2253,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**Warning**: due to a TF bug, this version only works using TensorFlow 2.2."
"**Warning**: due to a TF bug, this version only works using TensorFlow 2.2 or above."
]
},
{
@@ -2712,7 +2739,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.7.9"
},
"nav_menu": {},
"toc": {