|
711 | 711 | "\n",
|
712 | 712 | "<!--TODO(markdaoust) link to full examples or these referenced models.-->\n",
|
713 | 713 | "\n",
|
714 |
| - "The easiest way is to `@autograph.convert()` the `call` method. See the [keras guide](https://tensorflow.org/guide/keras#build_advanced_models) for details on how to build on these classes. \n", |
| 714 | + "The easiest way to use autograph is keras layers and models is to `@autograph.convert()` the `call` method. See the [keras guide](https://tensorflow.org/guide/keras#build_advanced_models) for details on how to build on these classes. \n", |
715 | 715 | "\n",
|
716 | 716 | "Here is a simple example of the [stocastic network depth](https://arxiv.org/abs/1603.09382) technique :"
|
717 | 717 | ]
|
|
857 | 857 | "execution_count": 0,
|
858 | 858 | "outputs": []
|
859 | 859 | },
|
860 |
| - { |
861 |
| - "metadata": { |
862 |
| - "id": "cpUD21HQWcOq", |
863 |
| - "colab_type": "text" |
864 |
| - }, |
865 |
| - "cell_type": "markdown", |
866 |
| - "source": [ |
867 |
| - "### RNN Cells\n", |
868 |
| - "\n", |
869 |
| - "The [standard approach](https://www.tensorflow.org/api_docs/python/tf/keras/layers/RNN) to custom RNN cells has the same issues that are solved by autograph.\n", |
870 |
| - "\n", |
871 |
| - "Implementing RNN cells with `autograph` is not much different from implementing them [under eager execution](https://colab.sandbox.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/examples/nmt_with_attention/nmt_with_attention.ipynb).\n", |
872 |
| - "\n", |
873 |
| - "To implement the prediction step in a keras model you could say:\n", |
874 |
| - "\n" |
875 |
| - ] |
876 |
| - }, |
877 |
| - { |
878 |
| - "metadata": { |
879 |
| - "id": "798S1r-sJGfR", |
880 |
| - "colab_type": "code", |
881 |
| - "colab": {} |
882 |
| - }, |
883 |
| - "cell_type": "code", |
884 |
| - "source": [ |
885 |
| - "class BahdanauAttention(tf.keras.Model):\n", |
886 |
| - " def __init__(self, units):\n", |
887 |
| - " super(BahdanauAttention, self).__init__()\n", |
888 |
| - " self.W1 = tf.keras.layers.Dense(units)\n", |
889 |
| - " self.W2 = tf.keras.layers.Dense(units)\n", |
890 |
| - " self.V = tf.keras.layers.Dense(1)\n", |
891 |
| - " \n", |
892 |
| - " def call(self, features, hidden):\n", |
893 |
| - " hidden_with_time_axis = tf.expand_dims(hidden, 1)\n", |
894 |
| - " score = tf.nn.tanh(self.W1(features) + self.W2(hidden_with_time_axis))\n", |
895 |
| - " attention_weights = tf.nn.softmax(self.V(score), axis=1)\n", |
896 |
| - " context_vector = attention_weights * features\n", |
897 |
| - " context_vector = tf.reduce_sum(context_vector, axis=1)\n", |
898 |
| - " return context_vector, attention_weights" |
899 |
| - ], |
900 |
| - "execution_count": 0, |
901 |
| - "outputs": [] |
902 |
| - }, |
903 |
| - { |
904 |
| - "metadata": { |
905 |
| - "id": "qwH-QnmlGV6c", |
906 |
| - "colab_type": "code", |
907 |
| - "colab": {} |
908 |
| - }, |
909 |
| - "cell_type": "code", |
910 |
| - "source": [ |
911 |
| - "class Decoder(tf.keras.Model):\n", |
912 |
| - " def __init__(self, vocab_size, embedding_dim, dec_units):\n", |
913 |
| - " super(Decoder, self).__init__()\n", |
914 |
| - " self.dec_units = dec_units\n", |
915 |
| - " self.embedding = layers.Embedding(vocab_size, embedding_dim)\n", |
916 |
| - " self.gru = layers.GRU(self.dec_units)\n", |
917 |
| - " self.fc = tf.keras.layers.Dense(vocab_size)\n", |
918 |
| - " self.attention = BahdanauAttention(self.dec_units)\n", |
919 |
| - " \n", |
920 |
| - " def call(self, enc_output):\n", |
921 |
| - " results = tf.keras\n", |
922 |
| - " hidden_with_time_axis = tf.expand_dims(hidden, 1)\n", |
923 |
| - " score = tf.nn.tanh(self.W1(enc_output) + self.W2(hidden_with_time_axis))\n", |
924 |
| - " \n", |
925 |
| - " # attention_weights shape == (batch_size, max_length, 1)\n", |
926 |
| - " # we get 1 at the last axis because we are applying score to self.V\n", |
927 |
| - " attention_weights = tf.nn.softmax(self.V(score), axis=1)\n", |
928 |
| - " \n", |
929 |
| - " # context_vector shape after sum == (batch_size, hidden_size)\n", |
930 |
| - " context_vector = attention_weights * enc_output\n", |
931 |
| - " context_vector = tf.reduce_sum(context_vector, axis=1)\n", |
932 |
| - " \n", |
933 |
| - " # x shape after passing through embedding == (batch_size, 1, embedding_dim)\n", |
934 |
| - " x = self.embedding(x)\n", |
935 |
| - " \n", |
936 |
| - " # x shape after concatenation == (batch_size, 1, embedding_dim + hidden_size)\n", |
937 |
| - " x = tf.concat([tf.expand_dims(context_vector, 1), x], axis=-1)\n", |
938 |
| - " \n", |
939 |
| - " # passing the concatenated vector to the GRU\n", |
940 |
| - " output, state = self.gru(x)\n", |
941 |
| - " \n", |
942 |
| - " # output shape == (batch_size * max_length, hidden_size)\n", |
943 |
| - " output = tf.reshape(output, (-1, output.shape[2]))\n", |
944 |
| - " \n", |
945 |
| - " # output shape == (batch_size * max_length, vocab)\n", |
946 |
| - " x = self.fc(output)\n", |
947 |
| - " \n", |
948 |
| - " return x, state, attention_weights\n", |
949 |
| - " \n", |
950 |
| - " def initialize_hidden_state(self):\n", |
951 |
| - " return tf.zeros((self.batch_sz, self.dec_units))" |
952 |
| - ], |
953 |
| - "execution_count": 0, |
954 |
| - "outputs": [] |
955 |
| - }, |
956 | 860 | {
|
957 | 861 | "metadata": {
|
958 | 862 | "id": "4LfnJjm0Bm0B",
|
|
0 commit comments