{"id":18568635,"url":"https://github.com/choznerol/tensorflow-lanet5-implementation","last_synced_at":"2026-05-18T06:39:51.143Z","repository":{"id":112055429,"uuid":"81986504","full_name":"choznerol/TensorFlow-LaNet5-implementation","owner":"choznerol","description":null,"archived":false,"fork":false,"pushed_at":"2017-02-20T15:30:35.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-11T18:37:09.987Z","etag":null,"topics":["cnn","deep-learning","tensorflow"],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/choznerol.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-02-14T20:35:21.000Z","updated_at":"2018-12-16T10:22:48.000Z","dependencies_parsed_at":"2023-05-04T11:33:27.548Z","dependency_job_id":null,"html_url":"https://github.com/choznerol/TensorFlow-LaNet5-implementation","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/choznerol/TensorFlow-LaNet5-implementation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choznerol%2FTensorFlow-LaNet5-implementation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choznerol%2FTensorFlow-LaNet5-implementation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choznerol%2FTensorFlow-LaNet5-implementation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choznerol%2FTensorFlow-LaNet5-implementation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/choznerol","download_url":"https://codeload.github.com/choznerol/TensorFlow-LaNet5-implementation/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choznerol%2FTensorFlow-LaNet5-implementation/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33167942,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-18T05:43:36.989Z","status":"ssl_error","status_checked_at":"2026-05-18T05:43:19.133Z","response_time":71,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cnn","deep-learning","tensorflow"],"created_at":"2024-11-06T22:29:53.060Z","updated_at":"2026-05-18T06:39:51.109Z","avatar_url":"https://github.com/choznerol.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"\nConvolutional Neuron Network LeNet-5 implemented by Google TensorFlow\n=============\n\n------------\n\n\u003ca src=\"http://yann.lecun.com/exdb/lenet/index.html\"\u003e\nLeNet-5\u003c/a\u003e is a classic CNN model which achieves 0.95% error rate on MNIST.\n\n\u003cimg src=\"http://yann.lecun.com/exdb/lenet/gifs/asamples.gif\"\u003e\n\nThis TensorFlow implementation is modified from a TensorFlow \u003ca src=\"https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/udacity/4_convolutions.ipynb\"\u003eexample\u003c/a\u003e. The final test accuracy is 90.4% (further turing should be able to improve this result)\n\n## Step 1: Import modules and load data\n\n\n```python\n# These are all the modules we'll be using later. Make sure you can import them\n# before proceeding further.\nfrom __future__ import print_function\nimport numpy as np\nimport tensorflow as tf\nfrom six.moves import cPickle as pickle\nfrom six.moves import range\nfrom IPython.display import display, HTML\n```\n\n\n```python\npickle_file = 'notMNIST.pickle'\n\nwith open(pickle_file, 'rb') as f:\n  save = pickle.load(f)\n  train_dataset = save['train_dataset']\n  train_labels = save['train_labels']\n  valid_dataset = save['valid_dataset']\n  valid_labels = save['valid_labels']\n  test_dataset = save['test_dataset']\n  test_labels = save['test_labels']\n  del save  # hint to help gc free up memory\n  print('Training set', train_dataset.shape, train_labels.shape)\n  print('Validation set', valid_dataset.shape, valid_labels.shape)\n  print('Test set', test_dataset.shape, test_labels.shape)\n```\n\n    Training set (200000, 28, 28) (200000,)\n    Validation set (10000, 28, 28) (10000,)\n    Test set (10000, 28, 28) (10000,)\n\n\n## Step 2: Reformat into a TensorFlow-friendly shape:\n- convolutions need the image data formatted as a cube (width by height by #channels)\n- labels as float 1-hot encodings.\n\n\n```python\nimage_size = 28\nnum_labels = 10\nnum_channels = 1 # grayscale\n\nimport numpy as np\n\ndef reformat(dataset, labels):\n  dataset = dataset.reshape(\n    (-1, image_size, image_size, num_channels)).astype(np.float32)\n  labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)\n  return dataset, labels\ntrain_dataset, train_labels = reformat(train_dataset, train_labels)\nvalid_dataset, valid_labels = reformat(valid_dataset, valid_labels)\ntest_dataset, test_labels = reformat(test_dataset, test_labels)\nprint('Training set', train_dataset.shape, train_labels.shape)\nprint('Validation set', valid_dataset.shape, valid_labels.shape)\nprint('Test set', test_dataset.shape, test_labels.shape)\n```\n\n    Training set (200000, 28, 28, 1) (200000, 10)\n    Validation set (10000, 28, 28, 1) (10000, 10)\n    Test set (10000, 28, 28, 1) (10000, 10)\n\n\n\n```python\ndef accuracy(predictions, labels):\n  return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))\n          / predictions.shape[0])\n```\n\n## Step3: Define a data flow graph to representing a TensorFlow computation\n\nThe model structure of LeNet-5:\n\u003cimg src=\"https://www.researchgate.net/profile/Haohan_Wang/publication/282997080/figure/fig10/AS:305939199610894@1449952997905/Figure-10-Architecture-of-LeNet-5-one-of-the-first-initial-architectures-of-CNN.png\"\u003e\n\n\n```python\nbatch_size = 16\npatch_size = 5\nC1_depth = 6\nC3_depth = 16\nnum_hidden = 64\n\ngraph = tf.Graph()\n\nwith graph.as_default():\n\n  # Input data.\n  tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size, image_size, num_channels))\n  tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))\n  tf_valid_dataset = tf.constant(valid_dataset) ## the shape is (10000, 28, 28)\n  tf_test_dataset = tf.constant(test_dataset)   ## the shape is (10000, 28, 28)\n  \n  # Variables.\n  C1_filter = tf.Variable(tf.truncated_normal([patch_size, patch_size, num_channels, C1_depth], stddev=0.1))\n  C1_biases = tf.Variable(tf.zeros([C1_depth]))\n  C3_filter = tf.Variable(tf.truncated_normal([patch_size, patch_size, C1_depth, C3_depth], stddev=0.1))\n  C3_biases = tf.Variable(tf.constant(1.0, shape=[C3_depth]))\n  C5_weights = tf.Variable(tf.truncated_normal([400, 120], stddev=0.1))\n  C5_biases = tf.Variable(tf.constant(1.0, shape=[120]))\n  F6_weights = tf.Variable(tf.truncated_normal([120, 84], stddev=0.1))\n  F6_biases = tf.Variable(tf.constant(1.0, shape=[84]))\n  OUTPUT_weights = tf.Variable(tf.truncated_normal([84, num_labels], stddev=0.1))\n  OUTPUT_biases = tf.Variable(tf.constant(1.0, shape=[num_labels]))\n  \n    \n  # Model.\n    ## LeNet-5 : \n    ## 1@32x32  --conv 5x5--\u003e  6@28x28  --max pool 2x2--\u003e  6@14x14  --conv 5x5--\u003e  16@10x10  --max pool 2x2--\u003e 16@5x5\n    ## --full cont--\u003e 120 --full cont--\u003e 84 --full cont--\u003e 10\n  def model(data):\n    print('         [batch, height, width, channel]')\n    print('data:   ', data.get_shape().as_list())\n    \n    C1 = tf.nn.conv2d(data, C1_filter, [1, 1, 1, 1], padding='SAME')  ## C1: 6@28x28\n    C1 = tf.nn.relu(C1 + C1_biases)\n    print('C1:     ', C1.get_shape().as_list())\n    \n    S2 = tf.nn.max_pool(C1, [1,2,2,1], [1,2,2,1], padding='VALID')    ## S2: 6@14x14\n    print('S2:     ', S2.get_shape().as_list())\n    \n    C3 = tf.nn.conv2d(S2, C3_filter, [1, 1, 1, 1], padding='VALID')   ## C3: 16@10x10\n    C3 = tf.nn.relu(C3 + C3_biases)\n    print('C3:     ', C3.get_shape().as_list())\n    \n    S4 = tf.nn.max_pool(C3, [1,2,2,1], [1,2,2,1], padding='VALID')    ## S4: 16@5x5\n    print('S4:     ', S4.get_shape().as_list())\n    \n    shape = S4.get_shape().as_list()\n    reshape = tf.reshape(S4, [shape[0], shape[1] * shape[2] * shape[3]])\n    print('reshape:', reshape.get_shape().as_list())\n    \n    C5 = tf.nn.relu(tf.matmul(reshape, C5_weights) + C5_biases)\n    print('C5:     ', C5.get_shape().as_list())\n    \n    F6 = tf.nn.relu(tf.matmul(C5, F6_weights) + F6_biases)\n    print('F6:     ', F6.get_shape().as_list())\n    \n    OUTPUT = tf.matmul(F6, OUTPUT_weights) + OUTPUT_biases\n    print('OUTPUT: ', OUTPUT.get_shape().as_list(), '\\n\\n')\n    \n    return OUTPUT\n  \n  print('TRAINING')    \n  # Training computation.\n  logits = model(tf_train_dataset)\n  loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels))\n    \n  # Optimizer.\n  optimizer = tf.train.GradientDescentOptimizer(0.05).minimize(loss)\n  \n  # Predictions for the training, validation, and test data.\n\n  train_prediction = tf.nn.softmax(logits)    \n  print('VALIDATION')\n  valid_prediction = tf.nn.softmax(model(tf_valid_dataset))\n  print('TESTING')\n  test_prediction = tf.nn.softmax(model(tf_test_dataset))\n```\n\n    TRAINING\n             [batch, height, width, channel]\n    data:    [16, 28, 28, 1]\n    C1:      [16, 28, 28, 6]\n    S2:      [16, 14, 14, 6]\n    C3:      [16, 10, 10, 16]\n    S4:      [16, 5, 5, 16]\n    reshape: [16, 400]\n    C5:      [16, 120]\n    F6:      [16, 84]\n    OUTPUT:  [16, 10] \n    \n    \n    VALIDATION\n             [batch, height, width, channel]\n    data:    [10000, 28, 28, 1]\n    C1:      [10000, 28, 28, 6]\n    S2:      [10000, 14, 14, 6]\n    C3:      [10000, 10, 10, 16]\n    S4:      [10000, 5, 5, 16]\n    reshape: [10000, 400]\n    C5:      [10000, 120]\n    F6:      [10000, 84]\n    OUTPUT:  [10000, 10] \n    \n    \n    TESTING\n             [batch, height, width, channel]\n    data:    [10000, 28, 28, 1]\n    C1:      [10000, 28, 28, 6]\n    S2:      [10000, 14, 14, 6]\n    C3:      [10000, 10, 10, 16]\n    S4:      [10000, 5, 5, 16]\n    reshape: [10000, 400]\n    C5:      [10000, 120]\n    F6:      [10000, 84]\n    OUTPUT:  [10000, 10] \n    \n    \n\n\n### Step 4: Run a TensorFlow session (`tf.session`) to train, validate and test the model\n\n\n```python\nnum_steps = 1001\n\nwith tf.Session(graph=graph) as session:\n  tf.initialize_all_variables().run()\n  print('\\t',     'Minibatch\\t', 'Minibatch\\t',  'Validation')\n  print('Step\\t', 'Loss\\t\\t',      'Accuracy\\t',  'Accuracy')\n  for step in range(num_steps):\n    offset = (step * batch_size) % (train_labels.shape[0] - batch_size)\n    batch_data = train_dataset[offset:(offset + batch_size), :, :, :]\n    batch_labels = train_labels[offset:(offset + batch_size), :]\n    feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}\n    _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict)\n    if (step % 50 == 0):\n      print('%d\\t %f\\t %.1f%%\\t\\t %.1f%%\\t' % (\n            step,\n            l,\n            accuracy(predictions, batch_labels),\n            accuracy(valid_prediction.eval(), valid_labels)\n        ))\n  print('*** TEST ACCURACY: %.1f%% ***' % accuracy(test_prediction.eval(), test_labels))\n```\n\n    \t Minibatch\t Minibatch\t Validation\n    Step\t Loss\t\t Accuracy\t Accuracy\n    0\t 4.374419\t 6.2%\t\t 10.0%\t\n    50\t 2.229358\t 12.5%\t\t 21.1%\t\n    100\t 1.112568\t 50.0%\t\t 53.2%\t\n    150\t 0.665704\t 75.0%\t\t 68.5%\t\n    200\t 0.652371\t 81.2%\t\t 73.8%\t\n    250\t 1.317888\t 62.5%\t\t 74.5%\t\n    300\t 0.861638\t 68.8%\t\t 78.5%\t\n    350\t 0.964509\t 68.8%\t\t 78.8%\t\n    400\t 1.044873\t 75.0%\t\t 78.5%\t\n    450\t 0.537551\t 81.2%\t\t 79.9%\t\n    500\t 0.424862\t 87.5%\t\t 80.5%\t\n    550\t 0.235435\t 93.8%\t\t 81.0%\t\n    600\t 0.509160\t 81.2%\t\t 81.3%\t\n    650\t 0.416043\t 87.5%\t\t 82.3%\t\n    700\t 0.589775\t 81.2%\t\t 81.6%\t\n    750\t 0.274672\t 100.0%\t\t 82.5%\t\n    800\t 0.554814\t 87.5%\t\t 83.0%\t\n    850\t 0.633812\t 87.5%\t\t 82.7%\t\n    900\t 0.525710\t 87.5%\t\t 81.9%\t\n    950\t 0.155540\t 93.8%\t\t 83.7%\t\n    1000\t 0.205820\t 100.0%\t\t 83.5%\t\n    ***Test accuracy: 90.4%\n\n\n\n\n\n\n\n\n\n## Appendix: Quick reference for some TensorFlow APIs\n\nThe convolutional model above uses convolutions with stride 2 to reduce the dimensionality. Replace the strides by a max pooling operation (`nn.max_pool()`) of stride 2 and kernel size 2.\n\n    ##\n    ## tf.nn.conv2d( \n    ##               input,            // [batch, in_height, in_width, in_channels] \n    ##               filter/kernel,    // [filter_height, filter_width, in_channels, out_channels]([16,16,1,16])\n    ##               strides,          // [1, stride, stride, 1]（一步的大小）\n    ##               padding           // 'VALID'(smaller output) of 'SAME'(auto zero padding!)\n    ##              ) \n    ##              =\u003e 4d-tensor       // A deeper feature map as next input\n    ##\n    ## tf.nn.max_pool(\n    ##               value,            // A 4-D Tensor with shape [batch, height, width, channels]\n    ##               ksize,            // The size of the window for each dimension of the input tensor.\n    ##               strides,          // The stride of the sliding window for each dimension of the input tensor.\n    ##               padding,          // 'VALID' or 'SAME'\n    ##              )\n    ##              =\u003e 4d-Tensor\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchoznerol%2Ftensorflow-lanet5-implementation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchoznerol%2Ftensorflow-lanet5-implementation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchoznerol%2Ftensorflow-lanet5-implementation/lists"}