资源算法Ladder Network

Ladder Network

2019-09-19 | |  82 |   0 |   0

Semi-Supervised Learning with Ladder Networks in Keras

This 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_ by A Rasmus, H Valpola, M Honkala,M Berglund, and T Raiko

The model achives 98% test accuracy on MNIST with just 100 labeled examples.

The code has only works tensorflow backend.

Requirements

  • Python 2

  • Tensorflow ( 1.4.0 )

  • numpy

  • keras ( 2.1.4 )

Note that other versions of tensorflow/keras should also work.

How to use

Load the dataset

from keras.datasets import mnistimport kerasimport random# get the dataset(x_train, y_train), (x_test, y_test) = mnist.load_data()x_train = x_train.reshape(60000, 28*28).astype('float32')/255.0x_test = x_test.reshape(10000, 28*28).astype('float32')/255.0y_train = keras.utils.to_categorical( y_train )y_test = keras.utils.to_categorical( y_test )# only select 100 training samples idxs_annot = range( x_train.shape[0])random.seed(0)random.shuffle( idxs_annot )idxs_annot = idxs_annot[ :100 ]x_train_unlabeled = x_trainx_train_labeled = x_train[ idxs_annot ]y_train_labeled = y_train[ idxs_annot  ]

Repeat the labeled dataset to match the shapes

n_rep = x_train_unlabeled.shape[0] / x_train_labeled.shape[0]x_train_labeled_rep = np.concatenate([x_train_labeled]*n_rep)y_train_labeled_rep = np.concatenate([y_train_labeled]*n_rep)

Initialize the model

from ladder_net import get_ladder_network_fcinp_size = 28*28 # size of mnist dataset n_classes = 10model = get_ladder_network_fc( layer_sizes = [ inp_size , 1000, 500, 250, 250, 250, n_classes ]  )

Train the model

model.fit([ x_train_labeled_rep , x_train_unlabeled   ] , y_train_labeled_rep , epochs=100)

Get the test accuracy

from sklearn.metrics import accuracy_scorey_test_pr = model.test_model.predict(x_test , batch_size=100 )print "test accuracy" , accuracy_score(y_test.argmax(-1) , y_test_pr.argmax(-1)  )


上一篇:Character-level CNN Text Classification

下一篇:pyTorch_NCE

用户评价
全部评价

热门资源

  • Keras-ResNeXt

    Keras ResNeXt Implementation of ResNeXt models...

  • seetafaceJNI

    项目介绍 基于中科院seetaface2进行封装的JAVA...

  • spark-corenlp

    This package wraps Stanford CoreNLP annotators ...

  • capsnet-with-caps...

    CapsNet with capsule-wise convolution Project ...

  • inferno-boilerplate

    This is a very basic boilerplate example for pe...