{"action":"created","comment":{"url":"https://api.github.com/repos/fchollet/keras/pulls/comments/52108083","id":52108083,"diff_hunk":"@@ -0,0 +1,123 @@\n+'''Train a Siamese MLP on pairs of digits from the MNIST dataset.\n+\n+It follows Hadsell-et-al.'06 [1] by computing the Euclidean distance on the\n+output of the shared network and by optimizing the contrastive loss (see paper\n+for mode details).\n+\n+[1] \"Dimensionality Reduction by Learning an Invariant Mapping\"\n+    http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf\n+\n+Run on GPU: THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python mnist_siamese_graph.py\n+\n+Get to 99.5% test accuracy after 20 epochs.\n+3 seconds per epoch on a Titan X GPU\n+'''\n+from __future__ import absolute_import\n+from __future__ import print_function\n+import numpy as np\n+np.random.seed(1337)  # for reproducibility\n+\n+import random\n+from keras.datasets import mnist\n+from keras.models import Sequential, Graph\n+from keras.layers.core import *\n+from keras.optimizers import SGD, RMSprop\n+from keras import backend as K\n+\n+\n+def euclidean_distance(inputs):\n+    assert len(inputs) == 2, \\\n+        'Euclidean distance needs 2 inputs, %d given' % len(inputs)\n+    u, v = inputs.values()\n+    return K.sqrt((K.square(u - v)).sum(axis=1, keepdims=True))","path":"examples/mnist_siamese_graph.py","position":32,"original_position":32,"commit_id":"652f2eb56dfd8b5f8549d06d66bae86bf98454f3","original_commit_id":"652f2eb56dfd8b5f8549d06d66bae86bf98454f3","user":{"login":"fchollet","id":710255,"avatar_url":"https://avatars.githubusercontent.com/u/710255?v=3","gravatar_id":"","url":"https://api.github.com/users/fchollet","html_url":"https://github.com/fchollet","followers_url":"https://api.github.com/users/fchollet/followers","following_url":"https://api.github.com/users/fchollet/following{/other_user}","gists_url":"https://api.github.com/users/fchollet/gists{/gist_id}","starred_url":"https://api.github.com/users/fchollet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fchollet/subscriptions","organizations_url":"https://api.github.com/users/fchollet/orgs","repos_url":"https://api.github.com/users/fchollet/repos","events_url":"https://api.github.com/users/fchollet/events{/privacy}","received_events_url":"https://api.github.com/users/fchollet/received_events","type":"User","site_admin":false},"body":"One comment (this is already merged): for compatibility with TensorFlow, you need to use the Keras backend for every logical step, including the final `sum`: `K.sum(...)`.","created_at":"2016-02-06T21:56:02Z","updated_at":"2016-02-06T21:56:02Z","html_url":"https://github.com/fchollet/keras/pull/1658#discussion_r52108083","pull_request_url":"https://api.github.com/repos/fchollet/keras/pulls/1658","_links":{"self":{"href":"https://api.github.com/repos/fchollet/keras/pulls/comments/52108083"},"html":{"href":"https://github.com/fchollet/keras/pull/1658#discussion_r52108083"},"pull_request":{"href":"https://api.github.com/repos/fchollet/keras/pulls/1658"}}},"pull_request":{"url":"https://api.github.com/repos/fchollet/keras/pulls/1658","id":58530517,"html_url":"https://github.com/fchollet/keras/pull/1658","diff_url":"https://github.com/fchollet/keras/pull/1658.diff","patch_url":"https://github.com/fchollet/keras/pull/1658.patch","issue_url":"https://api.github.com/repos/fchollet/keras/issues/1658","number":1658,"state":"closed","locked":false,"title":"add siamese example","user":{"login":"mmmikael","id":85865,"avatar_url":"https://avatars.githubusercontent.com/u/85865?v=3","gravatar_id":"","url":"https://api.github.com/users/mmmikael","html_url":"https://github.com/mmmikael","followers_url":"https://api.github.com/users/mmmikael/followers","following_url":"https://api.github.com/users/mmmikael/following{/other_user}","gists_url":"https://api.github.com/users/mmmikael/gists{/gist_id}","starred_url":"https://api.github.com/users/mmmikael/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mmmikael/subscriptions","organizations_url":"https://api.github.com/users/mmmikael/orgs","repos_url":"https://api.github.com/users/mmmikael/repos","events_url":"https://api.github.com/users/mmmikael/events{/privacy}","received_events_url":"https://api.github.com/users/mmmikael/received_events","type":"User","site_admin":false},"body":"This is a simple example of a Siamese network on MNIST datset:\r\n* uses a graph model\r\n* takes pairs of digits as input\r\n\r\nI wrote more or less the same example [with a sequential model](https://gist.github.com/mmmikael/0a3d4fae965bdbec1f9d) but I think the network definition with a graph is easier to understand:\r\n```python\r\ng = Graph()\r\ng.add_input(name='input_a', input_shape=(in_dim,))\r\ng.add_input(name='input_b', input_shape=(in_dim,))\r\ng.add_shared_node(base_network, name='shared', inputs=['input_a', 'input_b'],\r\n                  merge_mode='join')\r\ng.add_node(Lambda(euclidean_distance), name='d', input='shared')\r\ng.add_output(name='output', input='d')\r\n```\r\ncompared to:\r\n```python\r\ninput1 = Sequential()\r\ninput2 = Sequential()\r\ninput1.add(Layer(input_shape=(in_dim,)))\r\ninput2.add(Layer(input_shape=(in_dim,)))\r\nadd_shared_layer(base_network, [input1, input2])\r\nlambda_merge = LambdaMerge([input1, input2], euclidean_distance)\r\nmodel = Sequential()\r\nmodel.add(lambda_merge)\r\n``` ","created_at":"2016-02-06T21:14:14Z","updated_at":"2016-02-06T21:56:02Z","closed_at":"2016-02-06T21:50:43Z","merged_at":"2016-02-06T21:50:43Z","merge_commit_sha":"6c14c5b7a0532e52bbacf5d142643329aedeeb94","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/fchollet/keras/pulls/1658/commits","review_comments_url":"https://api.github.com/repos/fchollet/keras/pulls/1658/comments","review_comment_url":"https://api.github.com/repos/fchollet/keras/pulls/comments{/number}","comments_url":"https://api.github.com/repos/fchollet/keras/issues/1658/comments","statuses_url":"https://api.github.com/repos/fchollet/keras/statuses/652f2eb56dfd8b5f8549d06d66bae86bf98454f3","head":{"label":"contextvision:master","ref":"master","sha":"652f2eb56dfd8b5f8549d06d66bae86bf98454f3","user":{"login":"contextvision","id":12169801,"avatar_url":"https://avatars.githubusercontent.com/u/12169801?v=3","gravatar_id":"","url":"https://api.github.com/users/contextvision","html_url":"https://github.com/contextvision","followers_url":"https://api.github.com/users/contextvision/followers","following_url":"https://api.github.com/users/contextvision/following{/other_user}","gists_url":"https://api.github.com/users/contextvision/gists{/gist_id}","starred_url":"https://api.github.com/users/contextvision/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/contextvision/subscriptions","organizations_url":"https://api.github.com/users/contextvision/orgs","repos_url":"https://api.github.com/users/contextvision/repos","events_url":"https://api.github.com/users/contextvision/events{/privacy}","received_events_url":"https://api.github.com/users/contextvision/received_events","type":"Organization","site_admin":false},"repo":{"id":45238132,"name":"keras","full_name":"contextvision/keras","owner":{"login":"contextvision","id":12169801,"avatar_url":"https://avatars.githubusercontent.com/u/12169801?v=3","gravatar_id":"","url":"https://api.github.com/users/contextvision","html_url":"https://github.com/contextvision","followers_url":"https://api.github.com/users/contextvision/followers","following_url":"https://api.github.com/users/contextvision/following{/other_user}","gists_url":"https://api.github.com/users/contextvision/gists{/gist_id}","starred_url":"https://api.github.com/users/contextvision/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/contextvision/subscriptions","organizations_url":"https://api.github.com/users/contextvision/orgs","repos_url":"https://api.github.com/users/contextvision/repos","events_url":"https://api.github.com/users/contextvision/events{/privacy}","received_events_url":"https://api.github.com/users/contextvision/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/contextvision/keras","description":"Theano-based Deep Learning library (convnets, recurrent neural networks, and more).","fork":true,"url":"https://api.github.com/repos/contextvision/keras","forks_url":"https://api.github.com/repos/contextvision/keras/forks","keys_url":"https://api.github.com/repos/contextvision/keras/keys{/key_id}","collaborators_url":"https://api.github.com/repos/contextvision/keras/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/contextvision/keras/teams","hooks_url":"https://api.github.com/repos/contextvision/keras/hooks","issue_events_url":"https://api.github.com/repos/contextvision/keras/issues/events{/number}","events_url":"https://api.github.com/repos/contextvision/keras/events","assignees_url":"https://api.github.com/repos/contextvision/keras/assignees{/user}","branches_url":"https://api.github.com/repos/contextvision/keras/branches{/branch}","tags_url":"https://api.github.com/repos/contextvision/keras/tags","blobs_url":"https://api.github.com/repos/contextvision/keras/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/contextvision/keras/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/contextvision/keras/git/refs{/sha}","trees_url":"https://api.github.com/repos/contextvision/keras/git/trees{/sha}","statuses_url":"https://api.github.com/repos/contextvision/keras/statuses/{sha}","languages_url":"https://api.github.com/repos/contextvision/keras/languages","stargazers_url":"https://api.github.com/repos/contextvision/keras/stargazers","contributors_url":"https://api.github.com/repos/contextvision/keras/contributors","subscribers_url":"https://api.github.com/repos/contextvision/keras/subscribers","subscription_url":"https://api.github.com/repos/contextvision/keras/subscription","commits_url":"https://api.github.com/repos/contextvision/keras/commits{/sha}","git_commits_url":"https://api.github.com/repos/contextvision/keras/git/commits{/sha}","comments_url":"https://api.github.com/repos/contextvision/keras/comments{/number}","issue_comment_url":"https://api.github.com/repos/contextvision/keras/issues/comments{/number}","contents_url":"https://api.github.com/repos/contextvision/keras/contents/{+path}","compare_url":"https://api.github.com/repos/contextvision/keras/compare/{base}...{head}","merges_url":"https://api.github.com/repos/contextvision/keras/merges","archive_url":"https://api.github.com/repos/contextvision/keras/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/contextvision/keras/downloads","issues_url":"https://api.github.com/repos/contextvision/keras/issues{/number}","pulls_url":"https://api.github.com/repos/contextvision/keras/pulls{/number}","milestones_url":"https://api.github.com/repos/contextvision/keras/milestones{/number}","notifications_url":"https://api.github.com/repos/contextvision/keras/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/contextvision/keras/labels{/name}","releases_url":"https://api.github.com/repos/contextvision/keras/releases{/id}","deployments_url":"https://api.github.com/repos/contextvision/keras/deployments","created_at":"2015-10-30T08:24:17Z","updated_at":"2016-02-06T21:07:27Z","pushed_at":"2016-02-06T21:06:57Z","git_url":"git://github.com/contextvision/keras.git","ssh_url":"git@github.com:contextvision/keras.git","clone_url":"https://github.com/contextvision/keras.git","svn_url":"https://github.com/contextvision/keras","homepage":"http://keras.io/","size":2077,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"fchollet:master","ref":"master","sha":"359f91ff6c189f625a875e37592cd84f3f554d28","user":{"login":"fchollet","id":710255,"avatar_url":"https://avatars.githubusercontent.com/u/710255?v=3","gravatar_id":"","url":"https://api.github.com/users/fchollet","html_url":"https://github.com/fchollet","followers_url":"https://api.github.com/users/fchollet/followers","following_url":"https://api.github.com/users/fchollet/following{/other_user}","gists_url":"https://api.github.com/users/fchollet/gists{/gist_id}","starred_url":"https://api.github.com/users/fchollet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fchollet/subscriptions","organizations_url":"https://api.github.com/users/fchollet/orgs","repos_url":"https://api.github.com/users/fchollet/repos","events_url":"https://api.github.com/users/fchollet/events{/privacy}","received_events_url":"https://api.github.com/users/fchollet/received_events","type":"User","site_admin":false},"repo":{"id":33015583,"name":"keras","full_name":"fchollet/keras","owner":{"login":"fchollet","id":710255,"avatar_url":"https://avatars.githubusercontent.com/u/710255?v=3","gravatar_id":"","url":"https://api.github.com/users/fchollet","html_url":"https://github.com/fchollet","followers_url":"https://api.github.com/users/fchollet/followers","following_url":"https://api.github.com/users/fchollet/following{/other_user}","gists_url":"https://api.github.com/users/fchollet/gists{/gist_id}","starred_url":"https://api.github.com/users/fchollet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fchollet/subscriptions","organizations_url":"https://api.github.com/users/fchollet/orgs","repos_url":"https://api.github.com/users/fchollet/repos","events_url":"https://api.github.com/users/fchollet/events{/privacy}","received_events_url":"https://api.github.com/users/fchollet/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/fchollet/keras","description":"Deep Learning library for Python. Convnets, recurrent neural networks, and more. Runs on Theano and TensorFlow.","fork":false,"url":"https://api.github.com/repos/fchollet/keras","forks_url":"https://api.github.com/repos/fchollet/keras/forks","keys_url":"https://api.github.com/repos/fchollet/keras/keys{/key_id}","collaborators_url":"https://api.github.com/repos/fchollet/keras/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/fchollet/keras/teams","hooks_url":"https://api.github.com/repos/fchollet/keras/hooks","issue_events_url":"https://api.github.com/repos/fchollet/keras/issues/events{/number}","events_url":"https://api.github.com/repos/fchollet/keras/events","assignees_url":"https://api.github.com/repos/fchollet/keras/assignees{/user}","branches_url":"https://api.github.com/repos/fchollet/keras/branches{/branch}","tags_url":"https://api.github.com/repos/fchollet/keras/tags","blobs_url":"https://api.github.com/repos/fchollet/keras/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/fchollet/keras/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/fchollet/keras/git/refs{/sha}","trees_url":"https://api.github.com/repos/fchollet/keras/git/trees{/sha}","statuses_url":"https://api.github.com/repos/fchollet/keras/statuses/{sha}","languages_url":"https://api.github.com/repos/fchollet/keras/languages","stargazers_url":"https://api.github.com/repos/fchollet/keras/stargazers","contributors_url":"https://api.github.com/repos/fchollet/keras/contributors","subscribers_url":"https://api.github.com/repos/fchollet/keras/subscribers","subscription_url":"https://api.github.com/repos/fchollet/keras/subscription","commits_url":"https://api.github.com/repos/fchollet/keras/commits{/sha}","git_commits_url":"https://api.github.com/repos/fchollet/keras/git/commits{/sha}","comments_url":"https://api.github.com/repos/fchollet/keras/comments{/number}","issue_comment_url":"https://api.github.com/repos/fchollet/keras/issues/comments{/number}","contents_url":"https://api.github.com/repos/fchollet/keras/contents/{+path}","compare_url":"https://api.github.com/repos/fchollet/keras/compare/{base}...{head}","merges_url":"https://api.github.com/repos/fchollet/keras/merges","archive_url":"https://api.github.com/repos/fchollet/keras/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/fchollet/keras/downloads","issues_url":"https://api.github.com/repos/fchollet/keras/issues{/number}","pulls_url":"https://api.github.com/repos/fchollet/keras/pulls{/number}","milestones_url":"https://api.github.com/repos/fchollet/keras/milestones{/number}","notifications_url":"https://api.github.com/repos/fchollet/keras/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/fchollet/keras/labels{/name}","releases_url":"https://api.github.com/repos/fchollet/keras/releases{/id}","deployments_url":"https://api.github.com/repos/fchollet/keras/deployments","created_at":"2015-03-28T00:35:42Z","updated_at":"2016-02-06T20:48:31Z","pushed_at":"2016-02-06T21:50:41Z","git_url":"git://github.com/fchollet/keras.git","ssh_url":"git@github.com:fchollet/keras.git","clone_url":"https://github.com/fchollet/keras.git","svn_url":"https://github.com/fchollet/keras","homepage":"http://keras.io/","size":3767,"stargazers_count":4308,"watchers_count":4308,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1038,"mirror_url":null,"open_issues_count":432,"forks":1038,"open_issues":432,"watchers":4308,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/fchollet/keras/pulls/1658"},"html":{"href":"https://github.com/fchollet/keras/pull/1658"},"issue":{"href":"https://api.github.com/repos/fchollet/keras/issues/1658"},"comments":{"href":"https://api.github.com/repos/fchollet/keras/issues/1658/comments"},"review_comments":{"href":"https://api.github.com/repos/fchollet/keras/pulls/1658/comments"},"review_comment":{"href":"https://api.github.com/repos/fchollet/keras/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/fchollet/keras/pulls/1658/commits"},"statuses":{"href":"https://api.github.com/repos/fchollet/keras/statuses/652f2eb56dfd8b5f8549d06d66bae86bf98454f3"}}}}