diff --git a/14_recurrent_neural_networks.ipynb b/14_recurrent_neural_networks.ipynb index ba49bcf..d84171e 100644 --- a/14_recurrent_neural_networks.ipynb +++ b/14_recurrent_neural_networks.ipynb @@ -162,6 +162,13 @@ "## Using `static_rnn()`" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Note**: `tf.contrib.rnn` was partially moved to the core API in TensorFlow 1.2. Most of the `*Cell` and `*Wrapper` classes are now available in `tf.nn.rnn_cell`, and the `tf.contrib.rnn.static_rnn()` function is available as `tf.nn.static_rnn()`." + ] + }, { "cell_type": "code", "execution_count": 7, @@ -183,9 +190,9 @@ "X0 = tf.placeholder(tf.float32, [None, n_inputs])\n", "X1 = tf.placeholder(tf.float32, [None, n_inputs])\n", "\n", - "basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)\n", - "output_seqs, states = tf.contrib.rnn.static_rnn(basic_cell, [X0, X1],\n", - " dtype=tf.float32)\n", + "basic_cell = tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons)\n", + "output_seqs, states = tf.nn.static_rnn(basic_cell, [X0, X1],\n", + " dtype=tf.float32)\n", "Y0, Y1 = output_seqs" ] }, @@ -277,9 +284,9 @@ "X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])\n", "X_seqs = tf.unstack(tf.transpose(X, perm=[1, 0, 2]))\n", "\n", - "basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)\n", - "output_seqs, states = tf.contrib.rnn.static_rnn(basic_cell, X_seqs,\n", - " dtype=tf.float32)\n", + "basic_cell = tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons)\n", + "output_seqs, states = tf.nn.static_rnn(basic_cell, X_seqs,\n", + " dtype=tf.float32)\n", "outputs = tf.transpose(tf.stack(output_seqs), perm=[1, 0, 2])" ] }, @@ -357,7 +364,7 @@ "\n", "X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])\n", "\n", - "basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)\n", + "basic_cell = tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons)\n", "outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32)" ] }, @@ -426,7 +433,7 @@ "reset_graph()\n", "\n", "X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])\n", - "basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)" + "basic_cell = tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons)" ] }, { @@ -529,7 +536,7 @@ "X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])\n", "y = tf.placeholder(tf.int32, [None])\n", "\n", - "basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)\n", + "basic_cell = tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons)\n", "outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32)\n", "\n", "logits = tf.layers.dense(states, n_outputs)\n", @@ -544,16 +551,26 @@ "init = tf.global_variables_initializer()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Warning**: `tf.examples.tutorials.mnist` is deprecated. We will use `tf.keras.datasets.mnist` instead." + ] + }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ - "from tensorflow.examples.tutorials.mnist import input_data\n", - "mnist = input_data.read_data_sets(\"/tmp/data/\")\n", - "X_test = mnist.test.images.reshape((-1, n_steps, n_inputs))\n", - "y_test = mnist.test.labels" + "(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()\n", + "X_train = X_train.astype(np.float32).reshape(-1, 28*28) / 255.0\n", + "X_test = X_test.astype(np.float32).reshape(-1, 28*28) / 255.0\n", + "y_train = y_train.astype(np.int32)\n", + "y_test = y_test.astype(np.int32)\n", + "X_valid, X_train = X_train[:5000], X_train[5000:]\n", + "y_valid, y_train = y_train[:5000], y_train[5000:]" ] }, { @@ -561,6 +578,29 @@ "execution_count": 36, "metadata": {}, "outputs": [], + "source": [ + "def shuffle_batch(X, y, batch_size):\n", + " rnd_idx = np.random.permutation(len(X))\n", + " n_batches = len(X) // batch_size\n", + " for batch_idx in np.array_split(rnd_idx, n_batches):\n", + " X_batch, y_batch = X[batch_idx], y[batch_idx]\n", + " yield X_batch, y_batch" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [], + "source": [ + "X_test = X_test.reshape((-1, n_steps, n_inputs))" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], "source": [ "n_epochs = 100\n", "batch_size = 150\n", @@ -568,13 +608,12 @@ "with tf.Session() as sess:\n", " init.run()\n", " for epoch in range(n_epochs):\n", - " for iteration in range(mnist.train.num_examples // batch_size):\n", - " X_batch, y_batch = mnist.train.next_batch(batch_size)\n", + " for X_batch, y_batch in shuffle_batch(X_train, y_train, batch_size):\n", " X_batch = X_batch.reshape((-1, n_steps, n_inputs))\n", " sess.run(training_op, feed_dict={X: X_batch, y: y_batch})\n", - " acc_train = accuracy.eval(feed_dict={X: X_batch, y: y_batch})\n", + " acc_batch = accuracy.eval(feed_dict={X: X_batch, y: y_batch})\n", " acc_test = accuracy.eval(feed_dict={X: X_test, y: y_test})\n", - " print(epoch, \"Train accuracy:\", acc_train, \"Test accuracy:\", acc_test)" + " print(epoch, \"Last batch accuracy:\", acc_batch, \"Test accuracy:\", acc_test)" ] }, { @@ -586,7 +625,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 39, "metadata": {}, "outputs": [], "source": [ @@ -604,23 +643,23 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "n_neurons = 100\n", "n_layers = 3\n", "\n", - "layers = [tf.contrib.rnn.BasicRNNCell(num_units=n_neurons,\n", + "layers = [tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons,\n", " activation=tf.nn.relu)\n", " for layer in range(n_layers)]\n", - "multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers)\n", + "multi_layer_cell = tf.nn.rnn_cell.MultiRNNCell(layers)\n", "outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32)" ] }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ @@ -638,7 +677,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 42, "metadata": {}, "outputs": [], "source": [ @@ -648,13 +687,12 @@ "with tf.Session() as sess:\n", " init.run()\n", " for epoch in range(n_epochs):\n", - " for iteration in range(mnist.train.num_examples // batch_size):\n", - " X_batch, y_batch = mnist.train.next_batch(batch_size)\n", + " for X_batch, y_batch in shuffle_batch(X_train, y_train, batch_size):\n", " X_batch = X_batch.reshape((-1, n_steps, n_inputs))\n", " sess.run(training_op, feed_dict={X: X_batch, y: y_batch})\n", - " acc_train = accuracy.eval(feed_dict={X: X_batch, y: y_batch})\n", + " acc_batch = accuracy.eval(feed_dict={X: X_batch, y: y_batch})\n", " acc_test = accuracy.eval(feed_dict={X: X_test, y: y_test})\n", - " print(epoch, \"Train accuracy:\", acc_train, \"Test accuracy:\", acc_test)" + " print(epoch, \"Last batch accuracy:\", acc_batch, \"Test accuracy:\", acc_test)" ] }, { @@ -666,7 +704,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 43, "metadata": {}, "outputs": [], "source": [ @@ -685,7 +723,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 44, "metadata": {}, "outputs": [], "source": [ @@ -718,7 +756,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 45, "metadata": {}, "outputs": [], "source": [ @@ -727,7 +765,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 46, "metadata": {}, "outputs": [], "source": [ @@ -750,7 +788,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 47, "metadata": {}, "outputs": [], "source": [ @@ -764,7 +802,7 @@ "X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])\n", "y = tf.placeholder(tf.float32, [None, n_steps, n_outputs])\n", "\n", - "cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, activation=tf.nn.relu)\n", + "cell = tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons, activation=tf.nn.relu)\n", "outputs, states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)" ] }, @@ -777,7 +815,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ @@ -794,18 +832,18 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "cell = tf.contrib.rnn.OutputProjectionWrapper(\n", - " tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, activation=tf.nn.relu),\n", + " tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons, activation=tf.nn.relu),\n", " output_size=n_outputs)" ] }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 51, "metadata": {}, "outputs": [], "source": [ @@ -814,7 +852,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 52, "metadata": {}, "outputs": [], "source": [ @@ -829,7 +867,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 53, "metadata": {}, "outputs": [], "source": [ @@ -838,7 +876,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 54, "metadata": {}, "outputs": [], "source": [ @@ -859,7 +897,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 55, "metadata": {}, "outputs": [], "source": [ @@ -872,7 +910,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 56, "metadata": {}, "outputs": [], "source": [ @@ -881,7 +919,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ @@ -905,7 +943,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 58, "metadata": {}, "outputs": [], "source": [ @@ -921,17 +959,17 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 59, "metadata": {}, "outputs": [], "source": [ - "cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, activation=tf.nn.relu)\n", + "cell = tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons, activation=tf.nn.relu)\n", "rnn_outputs, states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)" ] }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 60, "metadata": {}, "outputs": [], "source": [ @@ -941,7 +979,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 61, "metadata": {}, "outputs": [], "source": [ @@ -952,7 +990,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 62, "metadata": {}, "outputs": [], "source": [ @@ -966,7 +1004,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 63, "metadata": {}, "outputs": [], "source": [ @@ -990,7 +1028,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 64, "metadata": {}, "outputs": [], "source": [ @@ -999,7 +1037,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 65, "metadata": {}, "outputs": [], "source": [ @@ -1022,7 +1060,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 66, "metadata": {}, "outputs": [], "source": [ @@ -1038,7 +1076,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 67, "metadata": {}, "outputs": [], "source": [ @@ -1052,7 +1090,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 68, "metadata": {}, "outputs": [], "source": [ @@ -1102,7 +1140,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 69, "metadata": {}, "outputs": [], "source": [ @@ -1116,22 +1154,22 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 70, "metadata": {}, "outputs": [], "source": [ "n_neurons = 100\n", "n_layers = 3\n", "\n", - "layers = [tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)\n", + "layers = [tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons)\n", " for layer in range(n_layers)]\n", - "multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers)\n", + "multi_layer_cell = tf.nn.rnn_cell.MultiRNNCell(layers)\n", "outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32)" ] }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 71, "metadata": {}, "outputs": [], "source": [ @@ -1140,7 +1178,7 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 72, "metadata": {}, "outputs": [], "source": [ @@ -1149,7 +1187,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 73, "metadata": {}, "outputs": [], "source": [ @@ -1160,7 +1198,7 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": 74, "metadata": {}, "outputs": [], "source": [ @@ -1183,15 +1221,15 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 75, "metadata": {}, "outputs": [], "source": [ "with tf.device(\"/gpu:0\"): # BAD! This is ignored.\n", - " layer1 = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)\n", + " layer1 = tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons)\n", "\n", "with tf.device(\"/gpu:1\"): # BAD! Ignored again.\n", - " layer2 = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)" + " layer2 = tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons)" ] }, { @@ -1203,13 +1241,13 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 76, "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf\n", "\n", - "class DeviceCellWrapper(tf.contrib.rnn.RNNCell):\n", + "class DeviceCellWrapper(tf.nn.rnn_cell.RNNCell):\n", " def __init__(self, device, cell):\n", " self._cell = cell\n", " self._device = device\n", @@ -1229,7 +1267,7 @@ }, { "cell_type": "code", - "execution_count": 74, + "execution_count": 77, "metadata": {}, "outputs": [], "source": [ @@ -1244,14 +1282,14 @@ }, { "cell_type": "code", - "execution_count": 75, + "execution_count": 78, "metadata": {}, "outputs": [], "source": [ "devices = [\"/cpu:0\", \"/cpu:0\", \"/cpu:0\"] # replace with [\"/gpu:0\", \"/gpu:1\", \"/gpu:2\"] if you have 3 GPUs\n", - "cells = [DeviceCellWrapper(dev,tf.contrib.rnn.BasicRNNCell(num_units=n_neurons))\n", + "cells = [DeviceCellWrapper(dev,tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons))\n", " for dev in devices]\n", - "multi_layer_cell = tf.contrib.rnn.MultiRNNCell(cells)\n", + "multi_layer_cell = tf.nn.rnn_cell.MultiRNNCell(cells)\n", "outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32)" ] }, @@ -1264,7 +1302,7 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": 79, "metadata": {}, "outputs": [], "source": [ @@ -1273,7 +1311,7 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 80, "metadata": { "scrolled": true }, @@ -1293,7 +1331,7 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": 81, "metadata": {}, "outputs": [], "source": [ @@ -1308,7 +1346,7 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": 82, "metadata": {}, "outputs": [], "source": [ @@ -1325,22 +1363,22 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 83, "metadata": {}, "outputs": [], "source": [ "keep_prob = tf.placeholder_with_default(1.0, shape=())\n", - "cells = [tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)\n", + "cells = [tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons)\n", " for layer in range(n_layers)]\n", - "cells_drop = [tf.contrib.rnn.DropoutWrapper(cell, input_keep_prob=keep_prob)\n", + "cells_drop = [tf.nn.rnn_cell.DropoutWrapper(cell, input_keep_prob=keep_prob)\n", " for cell in cells]\n", - "multi_layer_cell = tf.contrib.rnn.MultiRNNCell(cells_drop)\n", + "multi_layer_cell = tf.nn.rnn_cell.MultiRNNCell(cells_drop)\n", "rnn_outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32)" ] }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 84, "metadata": {}, "outputs": [], "source": [ @@ -1360,7 +1398,7 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": 85, "metadata": {}, "outputs": [], "source": [ @@ -1383,7 +1421,7 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 86, "metadata": {}, "outputs": [], "source": [ @@ -1396,7 +1434,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 87, "metadata": {}, "outputs": [], "source": [ @@ -1426,18 +1464,18 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": 88, "metadata": {}, "outputs": [], "source": [ "reset_graph()\n", "\n", - "lstm_cell = tf.contrib.rnn.BasicLSTMCell(num_units=n_neurons)" + "lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=n_neurons)" ] }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 89, "metadata": {}, "outputs": [], "source": [ @@ -1452,9 +1490,9 @@ "X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])\n", "y = tf.placeholder(tf.int32, [None])\n", "\n", - "lstm_cells = [tf.contrib.rnn.BasicLSTMCell(num_units=n_neurons)\n", + "lstm_cells = [tf.nn.rnn_cell.BasicLSTMCell(num_units=n_neurons)\n", " for layer in range(n_layers)]\n", - "multi_cell = tf.contrib.rnn.MultiRNNCell(lstm_cells)\n", + "multi_cell = tf.nn.rnn_cell.MultiRNNCell(lstm_cells)\n", "outputs, states = tf.nn.dynamic_rnn(multi_cell, X, dtype=tf.float32)\n", "top_layer_h_state = states[-1][1]\n", "logits = tf.layers.dense(top_layer_h_state, n_outputs, name=\"softmax\")\n", @@ -1470,7 +1508,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": 90, "metadata": {}, "outputs": [], "source": [ @@ -1479,7 +1517,7 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": 91, "metadata": {}, "outputs": [], "source": [ @@ -1488,7 +1526,7 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": 92, "metadata": { "scrolled": true }, @@ -1500,31 +1538,30 @@ "with tf.Session() as sess:\n", " init.run()\n", " for epoch in range(n_epochs):\n", - " for iteration in range(mnist.train.num_examples // batch_size):\n", - " X_batch, y_batch = mnist.train.next_batch(batch_size)\n", - " X_batch = X_batch.reshape((batch_size, n_steps, n_inputs))\n", + " for X_batch, y_batch in shuffle_batch(X_train, y_train, batch_size):\n", + " X_batch = X_batch.reshape((-1, n_steps, n_inputs))\n", " sess.run(training_op, feed_dict={X: X_batch, y: y_batch})\n", - " acc_train = accuracy.eval(feed_dict={X: X_batch, y: y_batch})\n", + " acc_batch = accuracy.eval(feed_dict={X: X_batch, y: y_batch})\n", " acc_test = accuracy.eval(feed_dict={X: X_test, y: y_test})\n", - " print(\"Epoch\", epoch, \"Train accuracy =\", acc_train, \"Test accuracy =\", acc_test)" + " print(epoch, \"Last batch accuracy:\", acc_batch, \"Test accuracy:\", acc_test)" ] }, { "cell_type": "code", - "execution_count": 90, + "execution_count": 93, "metadata": {}, "outputs": [], "source": [ - "lstm_cell = tf.contrib.rnn.LSTMCell(num_units=n_neurons, use_peepholes=True)" + "lstm_cell = tf.nn.rnn_cell.LSTMCell(num_units=n_neurons, use_peepholes=True)" ] }, { "cell_type": "code", - "execution_count": 91, + "execution_count": 94, "metadata": {}, "outputs": [], "source": [ - "gru_cell = tf.contrib.rnn.GRUCell(num_units=n_neurons)" + "gru_cell = tf.nn.rnn_cell.GRUCell(num_units=n_neurons)" ] }, { @@ -1550,7 +1587,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 95, "metadata": {}, "outputs": [], "source": [ @@ -1589,7 +1626,7 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 96, "metadata": {}, "outputs": [], "source": [ @@ -1598,7 +1635,7 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 97, "metadata": {}, "outputs": [], "source": [ @@ -1614,7 +1651,7 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": 98, "metadata": {}, "outputs": [], "source": [ @@ -1630,7 +1667,7 @@ }, { "cell_type": "code", - "execution_count": 96, + "execution_count": 99, "metadata": {}, "outputs": [], "source": [ @@ -1639,7 +1676,7 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 100, "metadata": {}, "outputs": [], "source": [ @@ -1648,7 +1685,7 @@ }, { "cell_type": "code", - "execution_count": 98, + "execution_count": 101, "metadata": {}, "outputs": [], "source": [ @@ -1664,7 +1701,7 @@ }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 102, "metadata": {}, "outputs": [], "source": [ @@ -1697,7 +1734,7 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": 103, "metadata": {}, "outputs": [], "source": [ @@ -1706,7 +1743,7 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": 104, "metadata": {}, "outputs": [], "source": [ @@ -1716,7 +1753,7 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 105, "metadata": {}, "outputs": [], "source": [ @@ -1725,7 +1762,7 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": 106, "metadata": {}, "outputs": [], "source": [ @@ -1741,7 +1778,7 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 107, "metadata": {}, "outputs": [], "source": [ @@ -1763,7 +1800,7 @@ }, { "cell_type": "code", - "execution_count": 105, + "execution_count": 108, "metadata": {}, "outputs": [], "source": [ @@ -1776,7 +1813,7 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": 109, "metadata": {}, "outputs": [], "source": [ @@ -1790,7 +1827,7 @@ }, { "cell_type": "code", - "execution_count": 107, + "execution_count": 110, "metadata": {}, "outputs": [], "source": [ @@ -1800,7 +1837,7 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": 111, "metadata": {}, "outputs": [], "source": [ @@ -1840,7 +1877,7 @@ }, { "cell_type": "code", - "execution_count": 109, + "execution_count": 112, "metadata": {}, "outputs": [], "source": [ @@ -1892,7 +1929,7 @@ }, { "cell_type": "code", - "execution_count": 110, + "execution_count": 113, "metadata": {}, "outputs": [], "source": [ @@ -1908,7 +1945,7 @@ }, { "cell_type": "code", - "execution_count": 111, + "execution_count": 114, "metadata": {}, "outputs": [], "source": [ @@ -1928,7 +1965,7 @@ }, { "cell_type": "code", - "execution_count": 112, + "execution_count": 115, "metadata": {}, "outputs": [], "source": [ @@ -1957,7 +1994,7 @@ }, { "cell_type": "code", - "execution_count": 113, + "execution_count": 116, "metadata": {}, "outputs": [], "source": [ @@ -1981,9 +2018,9 @@ "encoder_inputs = tf.unstack(tf.transpose(X)) # list of 1D tensors\n", "decoder_inputs = tf.unstack(tf.transpose(Y_input)) # list of 1D tensors\n", "\n", - "lstm_cells = [tf.contrib.rnn.BasicLSTMCell(num_units=n_neurons)\n", + "lstm_cells = [tf.nn.rnn_cell.BasicLSTMCell(num_units=n_neurons)\n", " for layer in range(n_layers)]\n", - "cell = tf.contrib.rnn.MultiRNNCell(lstm_cells)\n", + "cell = tf.nn.rnn_cell.MultiRNNCell(lstm_cells)\n", "\n", "output_seqs, states = tf.contrib.legacy_seq2seq.embedding_rnn_seq2seq(\n", " encoder_inputs,\n", @@ -1998,7 +2035,7 @@ }, { "cell_type": "code", - "execution_count": 114, + "execution_count": 117, "metadata": {}, "outputs": [], "source": [ @@ -2052,7 +2089,7 @@ }, { "cell_type": "code", - "execution_count": 115, + "execution_count": 118, "metadata": {}, "outputs": [], "source": [ @@ -2097,7 +2134,7 @@ }, { "cell_type": "code", - "execution_count": 116, + "execution_count": 119, "metadata": {}, "outputs": [], "source": [ @@ -2114,7 +2151,7 @@ }, { "cell_type": "code", - "execution_count": 117, + "execution_count": 120, "metadata": {}, "outputs": [], "source": [ @@ -2131,7 +2168,7 @@ }, { "cell_type": "code", - "execution_count": 118, + "execution_count": 121, "metadata": {}, "outputs": [], "source": [ @@ -2152,7 +2189,7 @@ }, { "cell_type": "code", - "execution_count": 119, + "execution_count": 122, "metadata": {}, "outputs": [], "source": [ @@ -2169,7 +2206,7 @@ }, { "cell_type": "code", - "execution_count": 120, + "execution_count": 123, "metadata": {}, "outputs": [], "source": [ @@ -2183,7 +2220,7 @@ }, { "cell_type": "code", - "execution_count": 121, + "execution_count": 124, "metadata": {}, "outputs": [], "source": [ @@ -2199,7 +2236,7 @@ }, { "cell_type": "code", - "execution_count": 122, + "execution_count": 125, "metadata": {}, "outputs": [], "source": [ @@ -2221,7 +2258,7 @@ }, { "cell_type": "code", - "execution_count": 123, + "execution_count": 126, "metadata": {}, "outputs": [], "source": [ @@ -2237,7 +2274,7 @@ }, { "cell_type": "code", - "execution_count": 124, + "execution_count": 127, "metadata": {}, "outputs": [], "source": [ @@ -2253,7 +2290,7 @@ }, { "cell_type": "code", - "execution_count": 125, + "execution_count": 128, "metadata": {}, "outputs": [], "source": [ @@ -2269,7 +2306,7 @@ }, { "cell_type": "code", - "execution_count": 126, + "execution_count": 129, "metadata": {}, "outputs": [], "source": [ @@ -2287,7 +2324,7 @@ }, { "cell_type": "code", - "execution_count": 127, + "execution_count": 130, "metadata": {}, "outputs": [], "source": [ @@ -2305,7 +2342,7 @@ "seq_length = tf.placeholder(tf.int32, [None], name=\"seq_length\")\n", "y = tf.placeholder(tf.float32, [None, 1], name=\"y\")\n", "\n", - "gru_cell = tf.contrib.rnn.GRUCell(num_units=n_neurons)\n", + "gru_cell = tf.nn.rnn_cell.GRUCell(num_units=n_neurons)\n", "outputs, states = tf.nn.dynamic_rnn(gru_cell, X, dtype=tf.float32,\n", " sequence_length=seq_length)\n", "\n", @@ -2336,7 +2373,7 @@ }, { "cell_type": "code", - "execution_count": 128, + "execution_count": 131, "metadata": {}, "outputs": [], "source": [ @@ -2345,7 +2382,7 @@ }, { "cell_type": "code", - "execution_count": 129, + "execution_count": 132, "metadata": {}, "outputs": [], "source": [ @@ -2378,7 +2415,7 @@ }, { "cell_type": "code", - "execution_count": 130, + "execution_count": 133, "metadata": {}, "outputs": [], "source": [