{"id":13482973,"url":"https://github.com/divamgupta/ladder_network_keras","last_synced_at":"2025-09-01T00:34:46.908Z","repository":{"id":38011082,"uuid":"172127300","full_name":"divamgupta/ladder_network_keras","owner":"divamgupta","description":"Semi-Supervised Learning with Ladder Networks in Keras. Get 98% test accuracy on MNIST with just 100 labeled examples ! ","archived":false,"fork":false,"pushed_at":"2021-04-30T09:58:08.000Z","size":8,"stargazers_count":101,"open_issues_count":6,"forks_count":34,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-27T13:39:22.987Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","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/divamgupta.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}},"created_at":"2019-02-22T20:01:18.000Z","updated_at":"2024-06-20T12:34:14.000Z","dependencies_parsed_at":"2022-08-08T22:45:58.162Z","dependency_job_id":null,"html_url":"https://github.com/divamgupta/ladder_network_keras","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divamgupta%2Fladder_network_keras","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divamgupta%2Fladder_network_keras/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divamgupta%2Fladder_network_keras/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divamgupta%2Fladder_network_keras/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/divamgupta","download_url":"https://codeload.github.com/divamgupta/ladder_network_keras/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253770422,"owners_count":21961769,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-07-31T17:01:07.168Z","updated_at":"2025-05-12T15:54:49.584Z","avatar_url":"https://github.com/divamgupta.png","language":"Python","funding_links":[],"categories":["Models/Projects","Researchers","Table of Contents","Deep Residual Learning"],"sub_categories":["Miscellaneous","Implementations"],"readme":"# Semi-Supervised Learning with Ladder Networks in \u003cu\u003eKeras\u003c/u\u003e\n\nThis is an implementation of Ladder Network in Keras. Ladder network is a model for semi-supervised learning. Refer to the paper titled [_Semi-Supervised Learning with Ladder Networks_](http://arxiv.org/abs/1507.02672) by A Rasmus, H Valpola, M Honkala,M Berglund, and T Raiko\n\nThis implementation was used in the official code of our paper  [Unsupervised Clustering using Pseudo-semi-supervised Learning\n](https://openreview.net/pdf?id=rJlnxkSYPS). The code can be found [here](https://github.com/divamgupta/deep_clustering_kingdra) and the blog post can be found [here](https://divamgupta.com/unsupervised-learning/2020/10/31/pseudo-semi-supervised-learning-for-unsupervised-clustering.html)\n\nThe model achives **98%** test accuracy on MNIST with just **100 labeled examples**. \n\nThe code only works with Tensorflow backend.\n\n\n\n## Requirements\n\n- Python 2.7+/3.6+\n- Tensorflow (1.4.0)\n- numpy\n- keras (2.1.4) \n\nNote that other versions of tensorflow/keras should also work.\n\n## How to use\n\nLoad the dataset\n\n```python\nfrom keras.datasets import mnist\nimport keras\nimport random\n\n# get the dataset\n(x_train, y_train), (x_test, y_test) = mnist.load_data()\n\nx_train = x_train.reshape(60000, 28*28).astype('float32')/255.0\nx_test = x_test.reshape(10000, 28*28).astype('float32')/255.0\n\ny_train = keras.utils.to_categorical( y_train )\ny_test = keras.utils.to_categorical( y_test )\n\n# only select 100 training samples \nidxs_annot = range( x_train.shape[0])\nrandom.seed(0)\nrandom.shuffle( idxs_annot )\nidxs_annot = idxs_annot[ :100 ]\n\nx_train_unlabeled = x_train\nx_train_labeled = x_train[ idxs_annot ]\ny_train_labeled = y_train[ idxs_annot  ]\n\n```\n\n\n\nRepeat the labeled dataset to match the shapes\n\n```python\nn_rep = x_train_unlabeled.shape[0] / x_train_labeled.shape[0]\nx_train_labeled_rep = np.concatenate([x_train_labeled]*n_rep)\ny_train_labeled_rep = np.concatenate([y_train_labeled]*n_rep)\n```\n\n\n\nInitialize the model\n\n```python\nfrom ladder_net import get_ladder_network_fc\ninp_size = 28*28 # size of mnist dataset \nn_classes = 10\nmodel = get_ladder_network_fc( layer_sizes = [ inp_size , 1000, 500, 250, 250, 250, n_classes ]  )\n```\n\n\n\nTrain the model\n\n```python\nmodel.fit([ x_train_labeled_rep , x_train_unlabeled   ] , y_train_labeled_rep , epochs=100)\n```\n\n\n\nGet the test accuracy \n\n```python\nfrom sklearn.metrics import accuracy_score\ny_test_pr = model.test_model.predict(x_test , batch_size=100 )\n\nprint \"test accuracy\" , accuracy_score(y_test.argmax(-1) , y_test_pr.argmax(-1)  )\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdivamgupta%2Fladder_network_keras","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdivamgupta%2Fladder_network_keras","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdivamgupta%2Fladder_network_keras/lists"}