Skip to content

Instantly share code, notes, and snippets.

@Irio
Created July 7, 2019 08:27
Show Gist options
  • Save Irio/d00b9661023923be7c963395483dfd73 to your computer and use it in GitHub Desktop.
Save Irio/d00b9661023923be7c963395483dfd73 to your computer and use it in GitHub Desktop.
Elman network
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Elman network\n",
"\n",
"**Elman, J. L. (1990). Finding Structure in Time. Cognitive Science, 14(2), 179–211.**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import tensorflow as tf\n",
"\n",
"np.random.seed(0)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"m = 2\n",
"n_0 = 3\n",
"n_1 = 4\n",
"n_2 = 5\n",
"\n",
"parameters = {\n",
" \"w_x\": np.random.randn(n_1, n_0),\n",
" \"w_c\": np.random.randn(n_1, n_1),\n",
" \"a_1\": np.random.randn(n_1, m),\n",
" \"w_1\": np.random.randn(n_1, n_0 + n_1),\n",
" \"b_1\": np.random.randn(n_1, 1),\n",
" \"w_2\": np.random.randn(n_2, n_1),\n",
" \"b_2\": np.random.randn(n_2, 1),\n",
"}\n",
"\n",
"for key in parameters.keys():\n",
" parameters[key] = tf.constant(parameters[key], dtype=np.float)\n",
"\n",
"x_val = np.random.randn(n_0, m)\n",
"y_val = np.random.randn(n_2, m)\n",
"\n",
"# Input units\n",
"\n",
"x = tf.placeholder(tf.float32, shape=(n_0, m), name=\"x\")\n",
"y = tf.placeholder(tf.float32, shape=(n_2, m), name=\"y\")\n",
"w_x = tf.Variable(parameters[\"w_x\"], name=\"w_x\")\n",
"\n",
"# Context units\n",
"\n",
"w_c = tf.Variable(parameters[\"w_c\"], trainable=False, name=\"w_c\")\n",
"a_1 = tf.Variable(parameters[\"a_1\"], name=\"a_1\")\n",
"\n",
"# First layer\n",
"\n",
"a_0 = tf.concat([x, a_1], 0)\n",
"\n",
"# Hidden units\n",
"\n",
"w_1 = tf.concat([w_x, w_c], 1, name=\"w_1\")\n",
"b_1 = tf.Variable(parameters[\"b_1\"], name=\"b_1\")\n",
"z_1 = tf.add(tf.matmul(w_1, a_0), b_1, name=\"z_1\")\n",
"a_1 = tf.assign(a_1, tf.sigmoid(z_1))\n",
"\n",
"# Output units\n",
"\n",
"w_2 = tf.Variable(parameters[\"w_2\"], name=\"w_2\")\n",
"b_2 = tf.Variable(parameters[\"b_2\"], name=\"b_2\")\n",
"z_2 = tf.add(tf.matmul(w_2, a_1), b_2, name=\"z_2\")\n",
"a_2 = tf.sigmoid(z_2, name=\"a_2\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING: Logging before flag parsing goes to stderr.\n",
"W0707 08:26:40.687984 140685639608128 deprecation.py:323] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/losses/losses_impl.py:121: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use tf.where in 2.0, which has the same broadcast rule as np.where\n"
]
}
],
"source": [
"learning_rate = 1e-3\n",
"\n",
"# Loss & Training\n",
"\n",
"prediction = tf.cast(tf.math.round(a_2), tf.float32)\n",
"loss = tf.losses.mean_squared_error(y, a_2)\n",
"train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"with tf.Session() as sess:\n",
" sess.run(tf.global_variables_initializer())\n",
" \n",
" writer = tf.summary.FileWriter(\"graph\", sess.graph)\n",
" sess.run(train_step, {x: x_val, y: y_val})\n",
" writer.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@Irio
Copy link
Author

Irio commented Jul 7, 2019

graph

@Irio
Copy link
Author

Irio commented Jul 9, 2019

After changes suggested by jdehesa:

# Input units

x = tf.placeholder(tf.float32, shape=(n_0, m), name="x")
y = tf.placeholder(tf.float32, shape=(n_2, m), name="y")
w_x = tf.Variable(parameters["w_x"], name="w_x")

# Context units

w_c = tf.Variable(parameters["w_c"], name="w_c")
a_c = tf.Variable(parameters["a_c"], trainable=False, name="a_c")

# First layer

a_0 = tf.concat([x, a_c], 0, name="a_0")

# Hidden units

w_1 = tf.concat([w_x, w_c], 1, name="w_1")
b_1 = tf.Variable(parameters["b_1"], name="b_1")
z_1 = tf.add(tf.matmul(w_1, a_0), b_1, name="z_1")
a_1 = tf.sigmoid(z_1)
with tf.control_dependencies([tf.assign(a_c, a_1)]):
    a_1 = tf.identity(a_1)

# Output units

w_2 = tf.Variable(parameters["w_2"], name="w_2")
b_2 = tf.Variable(parameters["b_2"], name="b_2")
z_2 = tf.add(tf.matmul(w_2, a_1), b_2, name="z_2")
a_2 = tf.sigmoid(z_2, name="a_2")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment