From f2c7ee7137140351563525ee643c50c91947c98e Mon Sep 17 00:00:00 2001 From: BALAGANESH <85802233+balaganesh102004@users.noreply.github.com> Date: Thu, 11 Apr 2024 22:30:32 +0530 Subject: [PATCH 1/3] Documentation Fix - Ragged Tensor --- site/en/guide/ragged_tensor.ipynb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/site/en/guide/ragged_tensor.ipynb b/site/en/guide/ragged_tensor.ipynb index d36010699db..a4d9b0b8814 100644 --- a/site/en/guide/ragged_tensor.ipynb +++ b/site/en/guide/ragged_tensor.ipynb @@ -674,7 +674,7 @@ "source": [ "### Keras\n", "\n", - "[tf.keras](https://www.tensorflow.org/guide/keras) is TensorFlow's high-level API for building and training deep learning models. Ragged tensors may be passed as inputs to a Keras model by setting `ragged=True` on `tf.keras.Input` or `tf.keras.layers.InputLayer`. Ragged tensors may also be passed between Keras layers, and returned by Keras models. The following example shows a toy LSTM model that is trained using ragged tensors." + "[tf.keras](https://www.tensorflow.org/guide/keras) is TensorFlow's high-level API for building and training deep learning models. Ragged tensors can be passed as inputs to a Keras model by using ragged tensors between Keras layers, and returning ragged tensors by Keras models. The following example shows a toy LSTM model that is trained using ragged tensors:" ] }, { @@ -700,9 +700,9 @@ "\n", "# Build the Keras model.\n", "keras_model = tf.keras.Sequential([\n", - " tf.keras.layers.Input(shape=[None], dtype=tf.int64, ragged=True),\n", - " tf.keras.layers.Embedding(hash_buckets, 16),\n", - " tf.keras.layers.LSTM(32, use_bias=False),\n", + " tf.keras.layers.Embedding(hash_buckets, 16, input_length=hashed_words.shape[1]),\n", + " tf.keras.layers.LSTM(32, return_sequences=True, use_bias=False),\n", + " tf.keras.layers.Flatten(),\n", " tf.keras.layers.Dense(32),\n", " tf.keras.layers.Activation(tf.nn.relu),\n", " tf.keras.layers.Dense(1)\n", @@ -710,7 +710,7 @@ "\n", "keras_model.compile(loss='binary_crossentropy', optimizer='rmsprop')\n", "keras_model.fit(hashed_words, is_question, epochs=5)\n", - "print(keras_model.predict(hashed_words))" + "print(keras_model.predict(hashed_words))\n" ] }, { From 47be34d66a5a3c02c555a2cf17186e085b501445 Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Fri, 23 Aug 2024 10:53:08 -0700 Subject: [PATCH 2/3] debug --- site/en/guide/ragged_tensor.ipynb | 78 +++++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 13 deletions(-) diff --git a/site/en/guide/ragged_tensor.ipynb b/site/en/guide/ragged_tensor.ipynb index a4d9b0b8814..d7872a3ff0e 100644 --- a/site/en/guide/ragged_tensor.ipynb +++ b/site/en/guide/ragged_tensor.ipynb @@ -681,7 +681,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "id": "pHls7hQVJlk5" + "id": "ucYf2sSzTvQo" }, "outputs": [], "source": [ @@ -691,26 +691,77 @@ " 'She turned me into a newt.',\n", " 'A newt?',\n", " 'Well, I got better.'])\n", - "is_question = tf.constant([True, False, True, False])\n", - "\n", + "is_question = tf.constant([True, False, True, False])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "MGYKmizJTw8B" + }, + "outputs": [], + "source": [ "# Preprocess the input strings.\n", "hash_buckets = 1000\n", "words = tf.strings.split(sentences, ' ')\n", "hashed_words = tf.strings.to_hash_bucket_fast(words, hash_buckets)\n", - "\n", + "hashed_words.to_list()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "7FTujwOlUT8J" + }, + "outputs": [], + "source": [ + "hashed_words.to_tensor()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "vzWudaESUBOZ" + }, + "outputs": [], + "source": [ + "tf.keras.Input?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "pHls7hQVJlk5" + }, + "outputs": [], + "source": [ "# Build the Keras model.\n", "keras_model = tf.keras.Sequential([\n", - " tf.keras.layers.Embedding(hash_buckets, 16, input_length=hashed_words.shape[1]),\n", + " tf.keras.layers.Embedding(hash_buckets, 16, mask_zero=True),\n", " tf.keras.layers.LSTM(32, return_sequences=True, use_bias=False),\n", - " tf.keras.layers.Flatten(),\n", + " tf.keras.layers.GlobalAveragePooling1D(),\n", " tf.keras.layers.Dense(32),\n", " tf.keras.layers.Activation(tf.nn.relu),\n", " tf.keras.layers.Dense(1)\n", "])\n", "\n", "keras_model.compile(loss='binary_crossentropy', optimizer='rmsprop')\n", - "keras_model.fit(hashed_words, is_question, epochs=5)\n", - "print(keras_model.predict(hashed_words))\n" + "keras_model.fit(hashed_words.to_tensor(), is_question, epochs=5)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "1IAjjmdTU9OU" + }, + "outputs": [], + "source": [ + "print(keras_model.predict(hashed_words.to_tensor()))" ] }, { @@ -799,7 +850,7 @@ "source": [ "### Datasets\n", "\n", - "[tf.data](https://www.tensorflow.org/guide/data) is an API that enables you to build complex input pipelines from simple, reusable pieces. Its core data structure is `tf.data.Dataset`, which represents a sequence of elements, in which each element consists of one or more components. " + "[tf.data](https://www.tensorflow.org/guide/data) is an API that enables you to build complex input pipelines from simple, reusable pieces. Its core data structure is `tf.data.Dataset`, which represents a sequence of elements, in which each element consists of one or more components." ] }, { @@ -1078,9 +1129,11 @@ "import tempfile\n", "\n", "keras_module_path = tempfile.mkdtemp()\n", - "tf.saved_model.save(keras_model, keras_module_path)\n", - "imported_model = tf.saved_model.load(keras_module_path)\n", - "imported_model(hashed_words)" + "keras_model.save(keras_module_path+\"/my_model.keras\")\n", + "\n", + "imported_model = tf.keras.models.load_model(keras_module_path+\"/my_model.keras\")\n", + "\n", + "imported_model(hashed_words.to_tensor())" ] }, { @@ -2125,7 +2178,6 @@ ], "metadata": { "colab": { - "collapsed_sections": [], "name": "ragged_tensor.ipynb", "toc_visible": true }, From 426ab2a25e20a50a0e187c78aec5c347e9ba073c Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Fri, 23 Aug 2024 10:55:49 -0700 Subject: [PATCH 3/3] Update site/en/guide/ragged_tensor.ipynb --- site/en/guide/ragged_tensor.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/en/guide/ragged_tensor.ipynb b/site/en/guide/ragged_tensor.ipynb index d7872a3ff0e..ba0be2928ce 100644 --- a/site/en/guide/ragged_tensor.ipynb +++ b/site/en/guide/ragged_tensor.ipynb @@ -674,7 +674,7 @@ "source": [ "### Keras\n", "\n", - "[tf.keras](https://www.tensorflow.org/guide/keras) is TensorFlow's high-level API for building and training deep learning models. Ragged tensors can be passed as inputs to a Keras model by using ragged tensors between Keras layers, and returning ragged tensors by Keras models. The following example shows a toy LSTM model that is trained using ragged tensors:" + "[tf.keras](https://www.tensorflow.org/guide/keras) is TensorFlow's high-level API for building and training deep learning models. It doesn't have ragged support. But it does support masked tensors. So the easiest way to use a ragged tensor in a Keras model is to convert the ragged tensor to a dense tensor, using `.to_tensor()` and then using Keras's builtin masking:" ] }, {