Skip to content

Instantly share code, notes, and snippets.

@rezapci
Created September 9, 2019 21:54
Show Gist options
  • Save rezapci/0d4fb510aa55f58a7b960e255658e150 to your computer and use it in GitHub Desktop.
Save rezapci/0d4fb510aa55f58a7b960e255658e150 to your computer and use it in GitHub Desktop.
neural_network.py.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-09-09T21:54:32.444Z"
},
"trusted": true
},
"cell_type": "code",
"source": "import numpy as np\nimport tensorflow as tf\nfrom utils import get_mnist_data\n\n\ndef single_layer_net(x):\n\n input_dim = 784\n n_classes = 10\n\n with tf.variable_scope('single_layer_net'):\n\n W = tf.Variable(initial_value=tf.random_normal(shape=[input_dim, n_classes]), name='weights')\n b = tf.Variable(initial_value=tf.zeros(shape=[n_classes]), name='biases')\n\n logits = tf.matmul(x, W) + b\n\n y = tf.nn.softmax(logits)\n\n return y\n\n\ndef multi_layer_net(x):\n\n input_dim = 784\n hidden_dim = 100\n n_classes = 10\n\n with tf.variable_scope('multi_layer_net'):\n\n W_1 = tf.Variable(initial_value=tf.random_normal(shape=[input_dim, hidden_dim]), name='l1_weights')\n b_1 = tf.Variable(initial_value=tf.zeros(shape=[hidden_dim]), name='l1_biases')\n\n hidden_1 = tf.nn.relu(tf.matmul(x, W_1) + b_1)\n\n W_2 = tf.Variable(initial_value=tf.random_normal(shape=[hidden_dim, n_classes]), name='l2_weights')\n b_2 = tf.Variable(initial_value=tf.zeros(shape=[n_classes]), name='l2_biases')\n\n logits = tf.matmul(hidden_1, W_2) + b_2\n\n y = tf.nn.softmax(logits)\n\n return y\n\n\nif __name__ == '__main__':\n\n # Load MNIST data\n mnist = get_mnist_data('/tmp/mnist', verbose=True)\n\n # Placeholders\n x = tf.placeholder(dtype=tf.float32, shape=[None, 784]) # input placeholder\n\n # Placeholder for targets\n targets = tf.placeholder(dtype=tf.float32, shape=[None, 10])\n\n # Define model output\n y = multi_layer_net(x)\n\n # Define loss function\n loss = tf.reduce_mean(-tf.reduce_sum(targets * tf.log(y + np.finfo('float32').eps), axis=1))\n\n # Define train step\n train_step = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)\n\n init_op = tf.global_variables_initializer()\n\n # Define metrics\n correct_predictions = tf.equal(tf.argmax(y, axis=1), tf.argmax(targets, axis=1))\n accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32))\n\n with tf.Session() as sess:\n\n # Initialize all variables\n sess.run(init_op)\n\n # Training parameters\n training_epochs = 100\n batch_size = 128\n\n # Number of batches to process to see whole dataset\n batches_each_epoch = mnist.train.num_examples // batch_size\n\n for epoch in range(training_epochs):\n\n # During training measure accuracy on validation set to have an idea of what's happening\n val_accuracy = sess.run(fetches=accuracy,\n feed_dict={x: mnist.validation.images, targets: mnist.validation.labels})\n print('Epoch: {:06d} - VAL accuracy: {:.03f}'.format(epoch, val_accuracy))\n\n for _ in range(batches_each_epoch):\n\n # Load a batch of training data\n x_batch, target_batch = mnist.train.next_batch(batch_size)\n\n # Actually run one training step here\n sess.run(fetches=[train_step],\n feed_dict={x: x_batch, targets: target_batch})\n\n # Eventually evaluate on whole test set when training ends\n test_accuracy = sess.run(fetches=accuracy,\n feed_dict={x: mnist.test.images, targets: mnist.test.labels})\n print('*' * 50)\n print('Training ended. TEST accuracy: {:.03f}'.format(test_accuracy))",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"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.7.3"
},
"latex_envs": {
"eqNumInitial": 1,
"eqLabelWithNumbers": true,
"current_citInitial": 1,
"cite_by": "apalike",
"bibliofile": "biblio.bib",
"LaTeX_envs_menu_present": true,
"labels_anchors": false,
"latex_user_defs": false,
"user_envs_cfg": false,
"report_style_numbering": false,
"autoclose": false,
"autocomplete": true,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
}
},
"nbTranslate": {
"hotkey": "alt-t",
"sourceLang": "en",
"targetLang": "fr",
"displayLangs": [
"*"
],
"langInMainMenu": true,
"useGoogleTranslate": true
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"gist": {
"id": "",
"data": {
"description": "neural_network.py.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment