Skip to content

Commit 064a9e3

Browse files
committed
Fixed ValueError: Attempt to have a second RNNCell use the weights of a variable scope that already has weights: 'rnn/basic_lstm_cell'
1 parent 7a1e030 commit 064a9e3

File tree

1 file changed

+33
-50
lines changed

1 file changed

+33
-50
lines changed

ch10_rnn/Concept02_rnn.ipynb

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,39 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"metadata": {
6-
"deletable": true,
7-
"editable": true
8-
},
5+
"metadata": {},
96
"source": [
107
"# Ch `10`: Concept `02`"
118
]
129
},
1310
{
1411
"cell_type": "markdown",
15-
"metadata": {
16-
"deletable": true,
17-
"editable": true
18-
},
12+
"metadata": {},
1913
"source": [
2014
"## Recurrent Neural Network"
2115
]
2216
},
2317
{
2418
"cell_type": "markdown",
25-
"metadata": {
26-
"deletable": true,
27-
"editable": true
28-
},
19+
"metadata": {},
2920
"source": [
3021
"Import the relevant libraries:"
3122
]
3223
},
3324
{
3425
"cell_type": "code",
3526
"execution_count": 1,
36-
"metadata": {
37-
"collapsed": false,
38-
"deletable": true,
39-
"editable": true
40-
},
41-
"outputs": [],
27+
"metadata": {},
28+
"outputs": [
29+
{
30+
"name": "stderr",
31+
"output_type": "stream",
32+
"text": [
33+
"/Users/anastasiia/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
34+
" from ._conv import register_converters as _register_converters\n"
35+
]
36+
}
37+
],
4238
"source": [
4339
"import numpy as np\n",
4440
"import tensorflow as tf\n",
@@ -47,22 +43,15 @@
4743
},
4844
{
4945
"cell_type": "markdown",
50-
"metadata": {
51-
"deletable": true,
52-
"editable": true
53-
},
46+
"metadata": {},
5447
"source": [
5548
"Define the RNN model:"
5649
]
5750
},
5851
{
5952
"cell_type": "code",
6053
"execution_count": 2,
61-
"metadata": {
62-
"collapsed": true,
63-
"deletable": true,
64-
"editable": true
65-
},
54+
"metadata": {},
6655
"outputs": [],
6756
"source": [
6857
"class SeriesPredictor:\n",
@@ -92,7 +81,7 @@
9281
" :param W: matrix of fully-connected output layer weights\n",
9382
" :param b: vector of fully-connected output layer biases\n",
9483
" \"\"\"\n",
95-
" cell = rnn.BasicLSTMCell(self.hidden_dim)\n",
84+
" cell = rnn.BasicLSTMCell(self.hidden_dim, reuse=tf.get_variable_scope().reuse)\n",
9685
" outputs, states = tf.nn.dynamic_rnn(cell, self.x, dtype=tf.float32)\n",
9786
" num_examples = tf.shape(self.x)[0]\n",
9887
" W_repeated = tf.tile(tf.expand_dims(self.W_out, 0), [num_examples, 1, 1])\n",
@@ -123,48 +112,42 @@
123112
},
124113
{
125114
"cell_type": "markdown",
126-
"metadata": {
127-
"deletable": true,
128-
"editable": true
129-
},
115+
"metadata": {},
130116
"source": [
131117
"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:"
132118
]
133119
},
134120
{
135121
"cell_type": "code",
136122
"execution_count": 3,
137-
"metadata": {
138-
"collapsed": false,
139-
"deletable": true,
140-
"editable": true
141-
},
123+
"metadata": {},
142124
"outputs": [
143125
{
144126
"name": "stdout",
145127
"output_type": "stream",
146128
"text": [
147-
"0 92.1852\n",
148-
"100 61.1175\n",
149-
"200 27.0341\n",
150-
"300 13.9523\n",
151-
"400 9.39037\n",
152-
"500 7.08643\n",
153-
"600 5.50997\n",
154-
"700 4.12571\n",
155-
"800 3.12016\n",
156-
"900 2.42311\n",
129+
"0 96.78678\n",
130+
"100 61.329662\n",
131+
"200 18.419907\n",
132+
"300 7.646343\n",
133+
"400 4.7979555\n",
134+
"500 3.2019987\n",
135+
"600 2.2661102\n",
136+
"700 1.6707231\n",
137+
"800 1.2424115\n",
138+
"900 0.9125628\n",
157139
"Model saved to model.ckpt\n",
140+
"INFO:tensorflow:Restoring parameters from ./model.ckpt\n",
158141
"\n",
159142
"Lets run some tests!\n",
160143
"\n",
161144
"When the input is [[1], [2], [3], [4]]\n",
162145
"The ground truth output should be [[1], [3], [5], [7]]\n",
163-
"And the model thinks it is [ 0.96018004 2.76944828 5.35826826 7.3706851 ]\n",
146+
"And the model thinks it is [1.037468 2.519481 4.514736 6.729595]\n",
164147
"\n",
165148
"When the input is [[4], [5], [6], [7]]\n",
166149
"The ground truth output should be [[4], [9], [11], [13]]\n",
167-
"And the model thinks it is [ 4.17302942 9.161376 11.13204765 11.64120388]\n",
150+
"And the model thinks it is [ 4.5689063 9.189994 11.679442 12.760409 ]\n",
168151
"\n"
169152
]
170153
}
@@ -211,7 +194,7 @@
211194
"name": "python",
212195
"nbconvert_exporter": "python",
213196
"pygments_lexer": "ipython3",
214-
"version": "3.5.2"
197+
"version": "3.6.5"
215198
}
216199
},
217200
"nbformat": 4,

0 commit comments

Comments
 (0)