Module Expr Str ImportFrom alias ImportFrom alias Import alias identifier Expr Call AttributeLoad AttributeLoad NameLoad attr attr Num Import alias ImportFrom alias ImportFrom alias alias ImportFrom alias alias alias alias ImportFrom alias alias ImportFrom alias identifier FunctionDef arguments args NameParam defaults body Assign TupleStore NameStore NameStore NameLoad Return Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr BinOpSub NameLoad NameLoad keyword Num keyword NameLoad decorator_list FunctionDef arguments args NameParam defaults body Assign TupleStore NameStore NameStore NameLoad Return NameLoad decorator_list FunctionDef arguments args NameParam NameParam defaults body Expr Str Assign NameStore Num Return Call AttributeLoad NameLoad attr BinOpAdd BinOpMult NameLoad Call AttributeLoad NameLoad attr NameLoad BinOpMult BinOpSub Num NameLoad Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr BinOpSub NameLoad NameLoad Num decorator_list FunctionDef arguments args NameParam NameParam defaults body Expr Str Assign NameStore ListLoad Assign NameStore ListLoad Assign NameStore BinOpSub Call NameLoad ListComp Call NameLoad SubscriptLoad NameLoad Index NameLoad comprehension NameStore Call NameLoad Num Num For NameStore Call NameLoad Num body For NameStore Call NameLoad NameLoad body Assign TupleStore NameStore NameStore TupleLoad SubscriptLoad SubscriptLoad NameLoad Index NameLoad Index NameLoad SubscriptLoad SubscriptLoad NameLoad Index NameLoad Index BinOpAdd NameLoad Num AugAssignAdd NameStore ListLoad ListLoad SubscriptLoad NameLoad Index NameLoad SubscriptLoad NameLoad Index NameLoad Assign NameStore Call AttributeLoad NameLoad attr Num Num Assign NameStore BinOpMod BinOpAdd NameLoad NameLoad Num Assign TupleStore NameStore NameStore TupleLoad SubscriptLoad SubscriptLoad NameLoad Index NameLoad Index NameLoad SubscriptLoad SubscriptLoad NameLoad Index NameLoad Index NameLoad AugAssignAdd NameStore ListLoad ListLoad SubscriptLoad NameLoad Index NameLoad SubscriptLoad NameLoad Index NameLoad AugAssignAdd NameStore ListLoad Num Num Return TupleLoad Call AttributeLoad NameLoad attr NameLoad Call AttributeLoad NameLoad attr NameLoad decorator_list FunctionDef arguments args NameParam defaults body Expr Str Assign NameStore Call NameLoad Expr Call AttributeLoad NameLoad attr Call NameLoad Num keyword TupleLoad NameLoad keyword Str Expr Call AttributeLoad NameLoad attr Call NameLoad Num Expr Call AttributeLoad NameLoad attr Call NameLoad Num keyword Str Expr Call AttributeLoad NameLoad attr Call NameLoad Num Expr Call AttributeLoad NameLoad attr Call NameLoad Num keyword Str Return NameLoad decorator_list FunctionDef arguments args NameParam NameParam defaults body Expr Str Return Call AttributeLoad SubscriptLoad NameLoad Index CompareLt Call AttributeLoad NameLoad attr Num attr decorator_list Assign TupleStore TupleStore NameStore NameStore TupleStore NameStore NameStore Call AttributeLoad NameLoad attr Assign NameStore Call AttributeLoad NameLoad attr Num Num Assign NameStore Call AttributeLoad NameLoad attr Num Num Assign NameStore Call AttributeLoad NameLoad attr Str Assign NameStore Call AttributeLoad NameLoad attr Str AugAssignDiv NameStore Num AugAssignDiv NameStore Num Assign NameStore Num Assign NameStore Num Assign NameStore ListComp SubscriptLoad Call AttributeLoad NameLoad attr CompareEq NameLoad NameLoad Index Num comprehension NameStore Call NameLoad Num Assign TupleStore NameStore NameStore Call NameLoad NameLoad NameLoad Assign NameStore ListComp SubscriptLoad Call AttributeLoad NameLoad attr CompareEq NameLoad NameLoad Index Num comprehension NameStore Call NameLoad Num Assign TupleStore NameStore NameStore Call NameLoad NameLoad NameLoad Assign NameStore Call NameLoad NameLoad Assign NameStore Call NameLoad keyword TupleLoad NameLoad Assign NameStore Call NameLoad keyword TupleLoad NameLoad Assign NameStore Call NameLoad NameLoad Assign NameStore Call NameLoad NameLoad Assign NameStore Call Call NameLoad NameLoad keyword NameLoad ListLoad NameLoad NameLoad Assign NameStore Call NameLoad keyword ListLoad NameLoad NameLoad keyword NameLoad Assign NameStore Call NameLoad Expr Call AttributeLoad NameLoad attr keyword NameLoad keyword NameLoad Expr Call AttributeLoad NameLoad attr ListLoad SubscriptLoad NameLoad ExtSlice Slice Index Num SubscriptLoad NameLoad ExtSlice Slice Index Num NameLoad keyword TupleLoad ListLoad SubscriptLoad NameLoad ExtSlice Slice Index Num SubscriptLoad NameLoad ExtSlice Slice Index Num NameLoad keyword Num keyword NameLoad Assign NameStore Call AttributeLoad NameLoad attr ListLoad SubscriptLoad NameLoad ExtSlice Slice Index Num SubscriptLoad NameLoad ExtSlice Slice Index Num Assign NameStore Call NameLoad NameLoad NameLoad Assign NameStore Call AttributeLoad NameLoad attr ListLoad SubscriptLoad NameLoad ExtSlice Slice Index Num SubscriptLoad NameLoad ExtSlice Slice Index Num Assign NameStore Call NameLoad NameLoad NameLoad Expr Call NameLoad BinOpMod Str BinOpMult Num NameLoad Expr Call NameLoad BinOpMod Str BinOpMult Num NameLoad
N/A N/A Train a Siamese MLP on pairs of digits from the MNIST dataset.

It follows Hadsell-et-al.'06 [1] by computing the Euclidean distance on the
output of the shared network and by optimizing the contrastive loss (see paper
for mode details).

[1] "Dimensionality Reduction by Learning an Invariant Mapping"
    http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf

Gets to 99.5% test accuracy after 20 epochs.
3 seconds per epoch on a Titan X GPU
 __future__ absolute_import __future__ print_function N/A numpy np N/A N/A N/A N/A np random seed 1337 N/A random keras.datasets mnist keras.models Sequential Model keras.layers Dense Dropout Input Lambda keras.optimizers SGD RMSprop keras backend K euclidean_distance N/A N/A vects N/A N/A N/A N/A x y vects N/A N/A N/A K sqrt N/A N/A K sum N/A N/A K square N/A x y axis 1 keepdims True N/A eucl_dist_output_shape N/A N/A shapes N/A N/A N/A N/A shape1 shape2 shapes N/A shape1 N/A contrastive_loss N/A N/A y_true y_pred N/A N/A N/A Contrastive loss from Hadsell-et-al.'06
    http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
     N/A margin 1 N/A N/A N/A K mean N/A N/A y_true N/A N/A K square y_pred N/A N/A 1 y_true N/A N/A K square N/A N/A K maximum N/A margin y_pred 0 N/A create_pairs N/A N/A x digit_indices N/A N/A N/A Positive and negative pair creation.
    Alternates between positive and negative pairs.
     N/A pairs N/A N/A labels N/A N/A n N/A N/A min N/A N/A len N/A digit_indices N/A d N/A d N/A range 10 1 N/A d N/A range 10 N/A N/A i N/A range n N/A N/A N/A z1 z2 N/A N/A N/A digit_indices N/A d N/A i N/A N/A digit_indices N/A d N/A N/A i 1 N/A pairs N/A N/A N/A x N/A z1 N/A x N/A z2 N/A inc N/A N/A random randrange 1 10 N/A dn N/A N/A d inc 10 N/A N/A z1 z2 N/A N/A N/A digit_indices N/A d N/A i N/A N/A digit_indices N/A dn N/A i N/A pairs N/A N/A N/A x N/A z1 N/A x N/A z2 N/A labels N/A 1 0 N/A N/A N/A N/A np array pairs N/A N/A np array labels N/A create_base_network N/A N/A input_dim N/A N/A N/A Base network to be shared (eq. to feature extraction).
     N/A seq N/A Sequential N/A N/A N/A seq add N/A Dense 128 input_shape N/A input_dim activation relu N/A N/A N/A seq add N/A Dropout 0.1 N/A N/A N/A seq add N/A Dense 128 activation relu N/A N/A N/A seq add N/A Dropout 0.1 N/A N/A N/A seq add N/A Dense 128 activation relu N/A seq N/A compute_accuracy N/A N/A predictions labels N/A N/A N/A Compute classification accuracy with a fixed threshold on distances.
     N/A N/A N/A N/A labels N/A N/A N/A N/A predictions ravel 0.5 mean N/A N/A N/A N/A X_train y_train N/A X_test y_test N/A N/A mnist load_data N/A X_train N/A N/A X_train reshape 60000 784 N/A X_test N/A N/A X_test reshape 10000 784 N/A X_train N/A N/A X_train astype float32 N/A X_test N/A N/A X_test astype float32 N/A X_train 255 N/A X_test 255 N/A input_dim 784 N/A nb_epoch 20 N/A digit_indices N/A N/A N/A N/A np where N/A y_train i N/A 0 N/A i N/A range 10 N/A N/A tr_pairs tr_y N/A create_pairs X_train digit_indices N/A digit_indices N/A N/A N/A N/A np where N/A y_test i N/A 0 N/A i N/A range 10 N/A N/A te_pairs te_y N/A create_pairs X_test digit_indices N/A base_network N/A create_base_network input_dim N/A input_a N/A Input shape N/A input_dim N/A input_b N/A Input shape N/A input_dim N/A processed_a N/A base_network input_a N/A processed_b N/A base_network input_b N/A distance N/A N/A Lambda euclidean_distance output_shape eucl_dist_output_shape N/A processed_a processed_b N/A model N/A Model input N/A input_a input_b output distance N/A rms N/A RMSprop N/A N/A N/A model compile loss contrastive_loss optimizer rms N/A N/A N/A model fit N/A N/A tr_pairs N/A N/A N/A 0 N/A tr_pairs N/A N/A N/A 1 tr_y validation_data N/A N/A N/A te_pairs N/A N/A N/A 0 N/A te_pairs N/A N/A N/A 1 te_y batch_size 128 nb_epoch nb_epoch N/A pred N/A N/A model predict N/A N/A tr_pairs N/A N/A N/A 0 N/A tr_pairs N/A N/A N/A 1 N/A tr_acc N/A compute_accuracy pred tr_y N/A pred N/A N/A model predict N/A N/A te_pairs N/A N/A N/A 0 N/A te_pairs N/A N/A N/A 1 N/A te_acc N/A compute_accuracy pred te_y N/A N/A print N/A * Accuracy on training set: %0.2f%% N/A 100 tr_acc N/A N/A print N/A * Accuracy on test set: %0.2f%% N/A 100 te_acc