Skip to content

Commit 7d6f634

Browse files
FNN example explaining variable sharing.
1 parent 0a41cf7 commit 7d6f634

File tree

2 files changed

+256
-0
lines changed

2 files changed

+256
-0
lines changed

fnn.ipynb

+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Sharing Variables\n",
8+
"\n",
9+
"What is wrong with the following code?"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 1,
15+
"metadata": {
16+
"collapsed": false,
17+
"scrolled": true
18+
},
19+
"outputs": [
20+
{
21+
"name": "stdout",
22+
"output_type": "stream",
23+
"text": [
24+
"theta1 [[ 0.44360071 -0.55896199]\n",
25+
" [-0.30600622 -0.5037573 ]\n",
26+
" [-0.53044277 0.16552085]\n",
27+
" [ 0.31216997 0.17150772]\n",
28+
" [ 0.04960573 0.1461049 ]\n",
29+
" [ 0.01075457 0.17645364]\n",
30+
" [ 0.06395692 0.14987926]\n",
31+
" [ 0.00446167 -0.56886625]\n",
32+
" [-0.07550567 -0.2945618 ]\n",
33+
" [ 0.39035973 0.01515578]]\n",
34+
"theta2 [[-0.82923883 -0.39798287]\n",
35+
" [-0.43532887 -0.27481681]\n",
36+
" [-0.06774535 0.59454221]\n",
37+
" [ 0.2501682 -0.871499 ]\n",
38+
" [-0.20009017 -0.29256272]\n",
39+
" [-0.14484291 0.09395451]\n",
40+
" [ 0.20101026 -0.46657833]\n",
41+
" [-0.69620067 -0.72480702]\n",
42+
" [-0.09735443 -0.78983605]\n",
43+
" [ 0.19394319 0.02787798]]\n"
44+
]
45+
}
46+
],
47+
"source": [
48+
"import tensorflow as tf\n",
49+
"import numpy as np\n",
50+
"from scipy.stats import norm\n",
51+
"\n",
52+
"\n",
53+
"def fnn(x, output_dim):\n",
54+
" #weights and biases\n",
55+
" w1 = tf.Variable(tf.random_normal([10, 2], stddev=0.35), name=\"weights1\")\n",
56+
" b1 = tf.Variable(tf.zeros([2]), name=\"biases1\")\n",
57+
"\n",
58+
" w2 = tf.Variable(tf.random_normal([2, output_dim], stddev=0.35), name=\"weights2\")\n",
59+
" b2 = tf.Variable(tf.zeros([2]), name=\"biases2\")\n",
60+
"\n",
61+
" # nn operators\n",
62+
" y1 = tf.nn.relu(tf.matmul(x, w1) + b1)\n",
63+
" y2 = tf.nn.sigmoid(tf.matmul(y1,w2) + b2)\n",
64+
" return y2, [w1, w2]\n",
65+
"\n",
66+
"# Defining the computational graph\n",
67+
"x1 = tf.placeholder(tf.float32, shape=(1, 10))\n",
68+
"y1, theta1 = fnn(x1, 1)\n",
69+
"\n",
70+
"# The second network has different weights and biases\n",
71+
"x2 = tf.placeholder(tf.float32, shape=(1, 10))\n",
72+
"y2, theta2 = fnn(x2, 1)\n",
73+
"\n",
74+
"# Initializing the session\n",
75+
"with tf.Session() as sess:\n",
76+
" tf.initialize_all_variables().run()\n",
77+
" # Feeding and Fetching data\n",
78+
" theta1, theta2 = sess.run([theta1, theta2], {x1: np.random.random([1, 10]), x2: np.random.random([1, 10])})\n",
79+
" print(\"theta1\", theta1[0])\n",
80+
" print(\"theta2\", theta2[0])"
81+
]
82+
},
83+
{
84+
"cell_type": "markdown",
85+
"metadata": {},
86+
"source": [
87+
"1. It is hard to add a layer to the network.\n",
88+
"2. We cannot share the weights and biases. (Each function call creates a new set of variables)\n",
89+
"\n",
90+
"Creating networks using tf.Variable can get complicated. Instead, use tf.get_variable() to create/return variables and tf.variable_scope() to manage namespaces."
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": 2,
96+
"metadata": {
97+
"collapsed": false
98+
},
99+
"outputs": [
100+
{
101+
"ename": "ValueError",
102+
"evalue": "Variable ffn/h/weights already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:\n\n File \"<ipython-input-2-0cf02566ad7f>\", line 5, in linear\n w = tf.get_variable(name='weights', shape=[x.get_shape()[1], out_dim], dtype=tf.float32, initializer=tf.random_normal_initializer())\n File \"<ipython-input-2-0cf02566ad7f>\", line 15, in <module>\n y11, theta11 = linear(x1, 10, name=\"h\", activation_fn=tf.nn.relu)\n File \"/Users/aida/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py\", line 2885, in run_code\n exec(code_obj, self.user_global_ns, self.user_ns)\n",
103+
"output_type": "error",
104+
"traceback": [
105+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
106+
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
107+
"\u001b[0;32m<ipython-input-2-0cf02566ad7f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0mx2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplaceholder\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat32\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshape\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m \u001b[0my21\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtheta21\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlinear\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"h\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mactivation_fn\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrelu\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 22\u001b[0m \u001b[0my22\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtheta22\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlinear\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"out\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mactivation_fn\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msigmoid\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
108+
"\u001b[0;32m<ipython-input-2-0cf02566ad7f>\u001b[0m in \u001b[0;36mlinear\u001b[0;34m(x, out_dim, name, activation_fn)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvariable_scope\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m#look for name/weights\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mw\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_variable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'weights'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshape\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_shape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout_dim\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat32\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minitializer\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom_normal_initializer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_variable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'biases'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshape\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mout_dim\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat32\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minitializer\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconstant_initializer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mout\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatmul\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mw\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
109+
"\u001b[0;32m/Users/aida/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py\u001b[0m in \u001b[0;36mget_variable\u001b[0;34m(name, shape, dtype, initializer, regularizer, trainable, collections, caching_device, partitioner, validate_shape, custom_getter)\u001b[0m\n\u001b[1;32m 828\u001b[0m \u001b[0mcollections\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcollections\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcaching_device\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcaching_device\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 829\u001b[0m \u001b[0mpartitioner\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpartitioner\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalidate_shape\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mvalidate_shape\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 830\u001b[0;31m custom_getter=custom_getter)\n\u001b[0m\u001b[1;32m 831\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 832\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
110+
"\u001b[0;32m/Users/aida/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py\u001b[0m in \u001b[0;36mget_variable\u001b[0;34m(self, var_store, name, shape, dtype, initializer, regularizer, trainable, collections, caching_device, partitioner, validate_shape, custom_getter)\u001b[0m\n\u001b[1;32m 671\u001b[0m \u001b[0mcollections\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcollections\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcaching_device\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcaching_device\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 672\u001b[0m \u001b[0mpartitioner\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpartitioner\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalidate_shape\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mvalidate_shape\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 673\u001b[0;31m custom_getter=custom_getter)\n\u001b[0m\u001b[1;32m 674\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 675\u001b[0m def _get_partitioned_variable(\n",
111+
"\u001b[0;32m/Users/aida/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py\u001b[0m in \u001b[0;36mget_variable\u001b[0;34m(self, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, custom_getter)\u001b[0m\n\u001b[1;32m 215\u001b[0m \u001b[0mreuse\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mreuse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrainable\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrainable\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcollections\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcollections\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 216\u001b[0m \u001b[0mcaching_device\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcaching_device\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpartitioner\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpartitioner\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 217\u001b[0;31m validate_shape=validate_shape)\n\u001b[0m\u001b[1;32m 218\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 219\u001b[0m def _get_partitioned_variable(\n",
112+
"\u001b[0;32m/Users/aida/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py\u001b[0m in \u001b[0;36m_true_getter\u001b[0;34m(name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape)\u001b[0m\n\u001b[1;32m 200\u001b[0m \u001b[0minitializer\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minitializer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mregularizer\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mregularizer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreuse\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mreuse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 201\u001b[0m \u001b[0mtrainable\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrainable\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcollections\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcollections\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 202\u001b[0;31m caching_device=caching_device, validate_shape=validate_shape)\n\u001b[0m\u001b[1;32m 203\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 204\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcustom_getter\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
113+
"\u001b[0;32m/Users/aida/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py\u001b[0m in \u001b[0;36m_get_single_variable\u001b[0;34m(self, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, validate_shape)\u001b[0m\n\u001b[1;32m 492\u001b[0m \u001b[0;34m\" Did you mean to set reuse=True in VarScope? \"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 493\u001b[0m \"Originally defined at:\\n\\n%s\" % (\n\u001b[0;32m--> 494\u001b[0;31m name, \"\".join(traceback.format_list(tb))))\n\u001b[0m\u001b[1;32m 495\u001b[0m \u001b[0mfound_var\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_vars\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 496\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mshape\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_compatible_with\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfound_var\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_shape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
114+
"\u001b[0;31mValueError\u001b[0m: Variable ffn/h/weights already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:\n\n File \"<ipython-input-2-0cf02566ad7f>\", line 5, in linear\n w = tf.get_variable(name='weights', shape=[x.get_shape()[1], out_dim], dtype=tf.float32, initializer=tf.random_normal_initializer())\n File \"<ipython-input-2-0cf02566ad7f>\", line 15, in <module>\n y11, theta11 = linear(x1, 10, name=\"h\", activation_fn=tf.nn.relu)\n File \"/Users/aida/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py\", line 2885, in run_code\n exec(code_obj, self.user_global_ns, self.user_ns)\n"
115+
]
116+
}
117+
],
118+
"source": [
119+
"# function for creating nn layers\n",
120+
"def linear(x, out_dim, name, activation_fn=None):\n",
121+
" with tf.variable_scope(name):\n",
122+
" #look for name/weights\n",
123+
" w = tf.get_variable(name='weights', shape=[x.get_shape()[1], out_dim], dtype=tf.float32, initializer=tf.random_normal_initializer())\n",
124+
" b = tf.get_variable(name='biases', shape=[out_dim], dtype=tf.float32, initializer=tf.constant_initializer(0.0))\n",
125+
" out = tf.matmul(x, w) + b\n",
126+
" if activation_fn != None:\n",
127+
" out = activation_fn(out)\n",
128+
" return out, [w, b]\n",
129+
"\n",
130+
"# Computational Graph\n",
131+
"with tf.variable_scope(\"ffn\") as scope:\n",
132+
" x1 = tf.placeholder(tf.float32, shape=(1, 10))\n",
133+
" y11, theta11 = linear(x1, 10, name=\"h\", activation_fn=tf.nn.relu)\n",
134+
" y12, theta12 = linear(y1, 1, name=\"out\", activation_fn=tf.nn.sigmoid)\n",
135+
"\n",
136+
" #scope.reuse_variables()\n",
137+
"\n",
138+
" x2 = tf.placeholder(tf.float32, shape=(1, 10))\n",
139+
" y21, theta21 = linear(x2, 10, name=\"h\", activation_fn=tf.nn.relu)\n",
140+
" y22, theta22 = linear(y1, 1, name=\"out\", activation_fn=tf.nn.sigmoid)\n",
141+
"\n",
142+
"\n",
143+
"# Initializing the session\n",
144+
"with tf.Session() as sess:\n",
145+
" print(\"session\")\n",
146+
" tf.initialize_all_variables().run()\n",
147+
" # Feeding and Fetching data\n",
148+
" theta1, theta2 = sess.run([theta12, theta22], {x1: np.random.random([1, 10]), x2: np.random.random([1, 10])})\n",
149+
" print(theta1[0])\n",
150+
" print(theta2[0])\n"
151+
]
152+
},
153+
{
154+
"cell_type": "markdown",
155+
"metadata": {},
156+
"source": [
157+
"Running the above code raises an error because the variable names are reused: \"Variable h/weights already exists, disallowed. Did you mean to set reuse=True in VarScope.\"\n",
158+
"\n",
159+
"If you need to share the variables, you need to explictly specify it using reuse_variables(). In the above code, you need to uncomment scope.reuse_variables().\n",
160+
"\n"
161+
]
162+
}
163+
],
164+
"metadata": {
165+
"kernelspec": {
166+
"display_name": "Python 3",
167+
"language": "python",
168+
"name": "python3"
169+
},
170+
"language_info": {
171+
"codemirror_mode": {
172+
"name": "ipython",
173+
"version": 3
174+
},
175+
"file_extension": ".py",
176+
"mimetype": "text/x-python",
177+
"name": "python",
178+
"nbconvert_exporter": "python",
179+
"pygments_lexer": "ipython3",
180+
"version": "3.5.1"
181+
}
182+
},
183+
"nbformat": 4,
184+
"nbformat_minor": 0
185+
}

fnn.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import tensorflow as tf
2+
import numpy as np
3+
from scipy.stats import norm
4+
5+
6+
def fnn(x, output_dim):
7+
#weights and biases
8+
w1 = tf.Variable(tf.random_normal([10, 20], stddev=0.35), name="weights1")
9+
b1 = tf.Variable(tf.zeros([20]), name="biases1")
10+
11+
w2 = tf.Variable(tf.random_normal([20, output_dim], stddev=0.35), name="weights2")
12+
b2 = tf.Variable(tf.zeros([20]), name="biases2")
13+
14+
# nn operators
15+
y1 = tf.nn.relu(tf.matmul(x, w1) + b1)
16+
y2 = tf.nn.sigmoid(tf.matmul(y1,w2) + b2)
17+
return y2, [w1, w2]
18+
19+
# Defining the computational graph
20+
x1 = tf.placeholder(tf.float32, shape=(1, 10))
21+
y1, w1 = fnn(x1, 1)
22+
23+
# The second network has different weights and biases
24+
x2 = tf.placeholder(tf.float32, shape=(1, 10))
25+
y2, w2 = fnn(x2, 1)
26+
27+
# Initializing the session
28+
with tf.Session() as sess:
29+
tf.initialize_all_variables().run()
30+
# Feeding and Fetching data
31+
theta1, theta2 = sess.run([w1, w2], {x1: np.random.random([1, 10]), x2: np.random.random([1, 10])})
32+
print(theta1)
33+
print(theta2)
34+
35+
36+
37+
# function for creating nn layers
38+
def linear(x, out_dim, name, activation_fn=None):
39+
with tf.variable_scope(name):
40+
w = tf.get_variable(name='weights', shape=[x.get_shape()[1], out_dim], dtype=tf.float32, initializer=tf.random_normal_initializer())
41+
b = tf.get_variable(name='biases', shape=[out_dim], dtype=tf.float32, initializer=tf.constant_initializer(0.0))
42+
out = tf.matmul(x, w) + b
43+
if activation_fn != None:
44+
out = activation_fn(out)
45+
return out, [w, b]
46+
47+
48+
# Computational Graph
49+
with tf.variable_scope("ffn") as scope:
50+
x1 = tf.placeholder(tf.float32, shape=(1, 10))
51+
y11, theta11 = linear(x1, 10, name="h", activation_fn=tf.nn.relu)
52+
y12, theta12 = linear(y1, 1, name="out", activation_fn=tf.nn.sigmoid)
53+
54+
scope.reuse_variables()
55+
56+
x2 = tf.placeholder(tf.float32, shape=(1, 10))
57+
y21, theta21 = linear(x2, 10, name="h", activation_fn=tf.nn.relu)
58+
y22, theta22 = linear(y1, 1, name="out", activation_fn=tf.nn.sigmoid)
59+
60+
61+
# Initializing the session
62+
with tf.Session() as sess:
63+
tf.initialize_all_variables().run()
64+
# Feeding and Fetching data
65+
theta1, theta2 = sess.run([theta12, theta22], {x1: np.random.random([1, 10]), x2: np.random.random([1, 10])})
66+
print(theta1[0])
67+
print(theta2[0])
68+
69+
70+
71+

0 commit comments

Comments
 (0)