Skip to content

Fixed ValueError in ch10_rnn/Concept02_rnn.ipynb #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 33 additions & 52 deletions ch10_rnn/Concept02_rnn.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,29 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"# Ch `10`: Concept `02`"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"## Recurrent Neural Network"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"Import the relevant libraries:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
Expand All @@ -47,22 +34,15 @@
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"Define the RNN model:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"class SeriesPredictor:\n",
Expand Down Expand Up @@ -92,7 +72,7 @@
" :param W: matrix of fully-connected output layer weights\n",
" :param b: vector of fully-connected output layer biases\n",
" \"\"\"\n",
" cell = rnn.BasicLSTMCell(self.hidden_dim)\n",
" cell = rnn.BasicLSTMCell(self.hidden_dim, reuse=tf.get_variable_scope().reuse)\n",
" outputs, states = tf.nn.dynamic_rnn(cell, self.x, dtype=tf.float32)\n",
" num_examples = tf.shape(self.x)[0]\n",
" W_repeated = tf.tile(tf.expand_dims(self.W_out, 0), [num_examples, 1, 1])\n",
Expand Down Expand Up @@ -123,48 +103,42 @@
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"Now, we'll train a series predictor. Let's say we have a sequence of numbers `[a, b, c, d]` that we want to transform into `[a, a+b, b+c, c+d]`. We'll give the RNN a couple examples in the training data. Let's see how well it learns this intended transformation:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 92.1852\n",
"100 61.1175\n",
"200 27.0341\n",
"300 13.9523\n",
"400 9.39037\n",
"500 7.08643\n",
"600 5.50997\n",
"700 4.12571\n",
"800 3.12016\n",
"900 2.42311\n",
"0 103.46295\n",
"100 63.418705\n",
"200 23.072838\n",
"300 11.47684\n",
"400 7.195353\n",
"500 4.4564924\n",
"600 2.8910196\n",
"700 1.948163\n",
"800 1.3193887\n",
"900 0.88628125\n",
"Model saved to model.ckpt\n",
"INFO:tensorflow:Restoring parameters from ./model.ckpt\n",
"\n",
"Lets run some tests!\n",
"\n",
"When the input is [[1], [2], [3], [4]]\n",
"The ground truth output should be [[1], [3], [5], [7]]\n",
"And the model thinks it is [ 0.96018004 2.76944828 5.35826826 7.3706851 ]\n",
"And the model thinks it is [0.86705637 2.7930977 5.307706 7.302184 ]\n",
"\n",
"When the input is [[4], [5], [6], [7]]\n",
"The ground truth output should be [[4], [9], [11], [13]]\n",
"And the model thinks it is [ 4.17302942 9.161376 11.13204765 11.64120388]\n",
"And the model thinks it is [ 4.0726233 9.083956 11.937489 12.943668 ]\n",
"\n"
]
}
Expand Down Expand Up @@ -193,6 +167,13 @@
" print(\"The ground truth output should be {}\".format(actual_y[i]))\n",
" print(\"And the model thinks it is {}\\n\".format(pred_y[i]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -211,7 +192,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
"version": "3.6.5"
}
},
"nbformat": 4,
Expand Down