{"action":"created","comment":{"url":"https://api.github.com/repos/keras-team/keras/pulls/comments/167417882","pull_request_review_id":95639944,"id":167417882,"diff_hunk":"@@ -0,0 +1,214 @@\n+\"\"\"\n+Train a simple CNN-Capsule Network on the CIFAR10 small images dataset.\n+\n+Without Data Augmentation:\n+It gets to 75% validation accuracy in 10 epochs,\n+and 79% after 15 epochs, and overfitting after 20 epcohs\n+\n+With Data Augmentation:\n+It gets to 75% validation accuracy in 10 epochs,\n+and 79% after 15 epochs, and 83% after 30 epcohs.\n+In my test, highest validation accuracy is 83.79% after 50 epcohs.\n+\n+This is a fast Implement, just 20s/epcoh with a gtx 1070 gpu.\n+\"\"\"\n+\n+from __future__ import print_function\n+from keras import backend as K\n+from keras.engine.topology import Layer\n+from keras.layers import Activation\n+from keras import utils\n+from keras.datasets import cifar10\n+from keras.models import Model\n+from keras.layers import *\n+from keras.preprocessing.image import ImageDataGenerator\n+\n+\n+# a squashing function. but it has litte difference from the Hinton's paper.\n+# it seems that this form of squashing performs better.\n+def squash(x, axis=-1):\n+    s_squared_norm = K.sum(K.square(x), axis, keepdims=True) + K.epsilon()\n+    scale = K.sqrt(s_squared_norm) / (0.5 + s_squared_norm)\n+    return scale * x\n+\n+\n+# define our own softmax function instead of K.softmax\n+def softmax(x, axis=-1):\n+    ex = K.exp(x - K.max(x, axis=axis, keepdims=True))\n+    return ex / K.sum(ex, axis=axis, keepdims=True)\n+\n+\n+# define the margin loss like hinge loss\n+def margin_loss(y_true, y_pred):\n+    lamb, margin = 0.5, 0.1\n+    return y_true * K.square(K.relu(1 - margin - y_pred)) + lamb * (\n+        1 - y_true) * K.square(K.relu(y_pred - margin))\n+\n+\n+class Capsule(Layer):\n+    \"\"\"\n+    A Capsule Implement with Pure Keras\n+    There are two vesions of Capsule.\n+    One is like dense layer (for the fixed-shape input),\n+    and the other is like timedistributed dense (for various length input).\n+    The input shape of Capsule must be (batch_size,\n+                                        input_num_capsule,\n+                                        input_dim_capsule\n+                                       )\n+    and the output shape is (batch_size,\n+                             num_capsule,\n+                             dim_capsule\n+                            )\n+\n+    Capsule Implement is from https://github.com/bojone/Capsule/\n+    Capsule Paper: https://arxiv.org/abs/1710.09829\n+    \"\"\"\n+    def __init__(self,\n+                 num_capsule,\n+                 dim_capsule,\n+                 routings=3,\n+                 share_weights=True,\n+                 activation='default',\n+                 **kwargs):\n+        super(Capsule, self).__init__(**kwargs)\n+        self.num_capsule = num_capsule\n+        self.dim_capsule = dim_capsule\n+        self.routings = routings\n+        self.share_weights = share_weights\n+        if activation == 'default':","path":"examples/cifar10_cnn_capsule.py","position":null,"original_position":78,"commit_id":"307de8436cc13b72681a10c83f528f66210fb34f","original_commit_id":"4f1cbb9c126fcbb8dd3cce866ddbb9e653b2d6e0","user":{"login":"bojone","id":9159613,"avatar_url":"https://avatars3.githubusercontent.com/u/9159613?v=4","gravatar_id":"","url":"https://api.github.com/users/bojone","html_url":"https://github.com/bojone","followers_url":"https://api.github.com/users/bojone/followers","following_url":"https://api.github.com/users/bojone/following{/other_user}","gists_url":"https://api.github.com/users/bojone/gists{/gist_id}","starred_url":"https://api.github.com/users/bojone/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bojone/subscriptions","organizations_url":"https://api.github.com/users/bojone/orgs","repos_url":"https://api.github.com/users/bojone/repos","events_url":"https://api.github.com/users/bojone/events{/privacy}","received_events_url":"https://api.github.com/users/bojone/received_events","type":"User","site_admin":false},"body":"default means we use squashing function as activation. I rename it now. ","created_at":"2018-02-11T02:50:57Z","updated_at":"2018-02-11T02:50:57Z","html_url":"https://github.com/keras-team/keras/pull/9193#discussion_r167417882","pull_request_url":"https://api.github.com/repos/keras-team/keras/pulls/9193","author_association":"NONE","_links":{"self":{"href":"https://api.github.com/repos/keras-team/keras/pulls/comments/167417882"},"html":{"href":"https://github.com/keras-team/keras/pull/9193#discussion_r167417882"},"pull_request":{"href":"https://api.github.com/repos/keras-team/keras/pulls/9193"}},"in_reply_to_id":167411767},"pull_request":{"url":"https://api.github.com/repos/keras-team/keras/pulls/9193","id":165287114,"html_url":"https://github.com/keras-team/keras/pull/9193","diff_url":"https://github.com/keras-team/keras/pull/9193.diff","patch_url":"https://github.com/keras-team/keras/pull/9193.patch","issue_url":"https://api.github.com/repos/keras-team/keras/issues/9193","number":9193,"state":"open","locked":false,"title":"a capsule cnn on cifar-10","user":{"login":"bojone","id":9159613,"avatar_url":"https://avatars3.githubusercontent.com/u/9159613?v=4","gravatar_id":"","url":"https://api.github.com/users/bojone","html_url":"https://github.com/bojone","followers_url":"https://api.github.com/users/bojone/followers","following_url":"https://api.github.com/users/bojone/following{/other_user}","gists_url":"https://api.github.com/users/bojone/gists{/gist_id}","starred_url":"https://api.github.com/users/bojone/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bojone/subscriptions","organizations_url":"https://api.github.com/users/bojone/orgs","repos_url":"https://api.github.com/users/bojone/repos","events_url":"https://api.github.com/users/bojone/events{/privacy}","received_events_url":"https://api.github.com/users/bojone/received_events","type":"User","site_admin":false},"body":"this is a fast capsule implement. and got the better performance than https://github.com/keras-team/keras/blob/master/examples/cifar10_cnn.py.","created_at":"2018-01-26T04:10:24Z","updated_at":"2018-02-11T02:50:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e903419cac275ea3ff9c704d58cda9574645a1fc","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[{"id":808058525,"url":"https://api.github.com/repos/keras-team/keras/labels/Reviewers%20wanted","name":"Reviewers wanted","color":"0e8a16","default":false}],"milestone":null,"commits_url":"https://api.github.com/repos/keras-team/keras/pulls/9193/commits","review_comments_url":"https://api.github.com/repos/keras-team/keras/pulls/9193/comments","review_comment_url":"https://api.github.com/repos/keras-team/keras/pulls/comments{/number}","comments_url":"https://api.github.com/repos/keras-team/keras/issues/9193/comments","statuses_url":"https://api.github.com/repos/keras-team/keras/statuses/307de8436cc13b72681a10c83f528f66210fb34f","head":{"label":"bojone:patch-1","ref":"patch-1","sha":"307de8436cc13b72681a10c83f528f66210fb34f","user":{"login":"bojone","id":9159613,"avatar_url":"https://avatars3.githubusercontent.com/u/9159613?v=4","gravatar_id":"","url":"https://api.github.com/users/bojone","html_url":"https://github.com/bojone","followers_url":"https://api.github.com/users/bojone/followers","following_url":"https://api.github.com/users/bojone/following{/other_user}","gists_url":"https://api.github.com/users/bojone/gists{/gist_id}","starred_url":"https://api.github.com/users/bojone/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bojone/subscriptions","organizations_url":"https://api.github.com/users/bojone/orgs","repos_url":"https://api.github.com/users/bojone/repos","events_url":"https://api.github.com/users/bojone/events{/privacy}","received_events_url":"https://api.github.com/users/bojone/received_events","type":"User","site_admin":false},"repo":{"id":119003538,"name":"keras","full_name":"bojone/keras","owner":{"login":"bojone","id":9159613,"avatar_url":"https://avatars3.githubusercontent.com/u/9159613?v=4","gravatar_id":"","url":"https://api.github.com/users/bojone","html_url":"https://github.com/bojone","followers_url":"https://api.github.com/users/bojone/followers","following_url":"https://api.github.com/users/bojone/following{/other_user}","gists_url":"https://api.github.com/users/bojone/gists{/gist_id}","starred_url":"https://api.github.com/users/bojone/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bojone/subscriptions","organizations_url":"https://api.github.com/users/bojone/orgs","repos_url":"https://api.github.com/users/bojone/repos","events_url":"https://api.github.com/users/bojone/events{/privacy}","received_events_url":"https://api.github.com/users/bojone/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bojone/keras","description":"Deep Learning for humans","fork":true,"url":"https://api.github.com/repos/bojone/keras","forks_url":"https://api.github.com/repos/bojone/keras/forks","keys_url":"https://api.github.com/repos/bojone/keras/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bojone/keras/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bojone/keras/teams","hooks_url":"https://api.github.com/repos/bojone/keras/hooks","issue_events_url":"https://api.github.com/repos/bojone/keras/issues/events{/number}","events_url":"https://api.github.com/repos/bojone/keras/events","assignees_url":"https://api.github.com/repos/bojone/keras/assignees{/user}","branches_url":"https://api.github.com/repos/bojone/keras/branches{/branch}","tags_url":"https://api.github.com/repos/bojone/keras/tags","blobs_url":"https://api.github.com/repos/bojone/keras/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bojone/keras/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bojone/keras/git/refs{/sha}","trees_url":"https://api.github.com/repos/bojone/keras/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bojone/keras/statuses/{sha}","languages_url":"https://api.github.com/repos/bojone/keras/languages","stargazers_url":"https://api.github.com/repos/bojone/keras/stargazers","contributors_url":"https://api.github.com/repos/bojone/keras/contributors","subscribers_url":"https://api.github.com/repos/bojone/keras/subscribers","subscription_url":"https://api.github.com/repos/bojone/keras/subscription","commits_url":"https://api.github.com/repos/bojone/keras/commits{/sha}","git_commits_url":"https://api.github.com/repos/bojone/keras/git/commits{/sha}","comments_url":"https://api.github.com/repos/bojone/keras/comments{/number}","issue_comment_url":"https://api.github.com/repos/bojone/keras/issues/comments{/number}","contents_url":"https://api.github.com/repos/bojone/keras/contents/{+path}","compare_url":"https://api.github.com/repos/bojone/keras/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bojone/keras/merges","archive_url":"https://api.github.com/repos/bojone/keras/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bojone/keras/downloads","issues_url":"https://api.github.com/repos/bojone/keras/issues{/number}","pulls_url":"https://api.github.com/repos/bojone/keras/pulls{/number}","milestones_url":"https://api.github.com/repos/bojone/keras/milestones{/number}","notifications_url":"https://api.github.com/repos/bojone/keras/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bojone/keras/labels{/name}","releases_url":"https://api.github.com/repos/bojone/keras/releases{/id}","deployments_url":"https://api.github.com/repos/bojone/keras/deployments","created_at":"2018-01-26T03:58:36Z","updated_at":"2018-01-26T03:58:39Z","pushed_at":"2018-02-11T02:49:25Z","git_url":"git://github.com/bojone/keras.git","ssh_url":"git@github.com:bojone/keras.git","clone_url":"https://github.com/bojone/keras.git","svn_url":"https://github.com/bojone/keras","homepage":"http://keras.io/","size":10516,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"keras-team:master","ref":"master","sha":"628cf08be1a3f8a4547f6321677470d52b725819","user":{"login":"keras-team","id":34455048,"avatar_url":"https://avatars0.githubusercontent.com/u/34455048?v=4","gravatar_id":"","url":"https://api.github.com/users/keras-team","html_url":"https://github.com/keras-team","followers_url":"https://api.github.com/users/keras-team/followers","following_url":"https://api.github.com/users/keras-team/following{/other_user}","gists_url":"https://api.github.com/users/keras-team/gists{/gist_id}","starred_url":"https://api.github.com/users/keras-team/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/keras-team/subscriptions","organizations_url":"https://api.github.com/users/keras-team/orgs","repos_url":"https://api.github.com/users/keras-team/repos","events_url":"https://api.github.com/users/keras-team/events{/privacy}","received_events_url":"https://api.github.com/users/keras-team/received_events","type":"Organization","site_admin":false},"repo":{"id":33015583,"name":"keras","full_name":"keras-team/keras","owner":{"login":"keras-team","id":34455048,"avatar_url":"https://avatars0.githubusercontent.com/u/34455048?v=4","gravatar_id":"","url":"https://api.github.com/users/keras-team","html_url":"https://github.com/keras-team","followers_url":"https://api.github.com/users/keras-team/followers","following_url":"https://api.github.com/users/keras-team/following{/other_user}","gists_url":"https://api.github.com/users/keras-team/gists{/gist_id}","starred_url":"https://api.github.com/users/keras-team/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/keras-team/subscriptions","organizations_url":"https://api.github.com/users/keras-team/orgs","repos_url":"https://api.github.com/users/keras-team/repos","events_url":"https://api.github.com/users/keras-team/events{/privacy}","received_events_url":"https://api.github.com/users/keras-team/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/keras-team/keras","description":"Deep Learning for humans","fork":false,"url":"https://api.github.com/repos/keras-team/keras","forks_url":"https://api.github.com/repos/keras-team/keras/forks","keys_url":"https://api.github.com/repos/keras-team/keras/keys{/key_id}","collaborators_url":"https://api.github.com/repos/keras-team/keras/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/keras-team/keras/teams","hooks_url":"https://api.github.com/repos/keras-team/keras/hooks","issue_events_url":"https://api.github.com/repos/keras-team/keras/issues/events{/number}","events_url":"https://api.github.com/repos/keras-team/keras/events","assignees_url":"https://api.github.com/repos/keras-team/keras/assignees{/user}","branches_url":"https://api.github.com/repos/keras-team/keras/branches{/branch}","tags_url":"https://api.github.com/repos/keras-team/keras/tags","blobs_url":"https://api.github.com/repos/keras-team/keras/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/keras-team/keras/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/keras-team/keras/git/refs{/sha}","trees_url":"https://api.github.com/repos/keras-team/keras/git/trees{/sha}","statuses_url":"https://api.github.com/repos/keras-team/keras/statuses/{sha}","languages_url":"https://api.github.com/repos/keras-team/keras/languages","stargazers_url":"https://api.github.com/repos/keras-team/keras/stargazers","contributors_url":"https://api.github.com/repos/keras-team/keras/contributors","subscribers_url":"https://api.github.com/repos/keras-team/keras/subscribers","subscription_url":"https://api.github.com/repos/keras-team/keras/subscription","commits_url":"https://api.github.com/repos/keras-team/keras/commits{/sha}","git_commits_url":"https://api.github.com/repos/keras-team/keras/git/commits{/sha}","comments_url":"https://api.github.com/repos/keras-team/keras/comments{/number}","issue_comment_url":"https://api.github.com/repos/keras-team/keras/issues/comments{/number}","contents_url":"https://api.github.com/repos/keras-team/keras/contents/{+path}","compare_url":"https://api.github.com/repos/keras-team/keras/compare/{base}...{head}","merges_url":"https://api.github.com/repos/keras-team/keras/merges","archive_url":"https://api.github.com/repos/keras-team/keras/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/keras-team/keras/downloads","issues_url":"https://api.github.com/repos/keras-team/keras/issues{/number}","pulls_url":"https://api.github.com/repos/keras-team/keras/pulls{/number}","milestones_url":"https://api.github.com/repos/keras-team/keras/milestones{/number}","notifications_url":"https://api.github.com/repos/keras-team/keras/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/keras-team/keras/labels{/name}","releases_url":"https://api.github.com/repos/keras-team/keras/releases{/id}","deployments_url":"https://api.github.com/repos/keras-team/keras/deployments","created_at":"2015-03-28T00:35:42Z","updated_at":"2018-02-11T02:43:37Z","pushed_at":"2018-02-11T02:49:25Z","git_url":"git://github.com/keras-team/keras.git","ssh_url":"git@github.com:keras-team/keras.git","clone_url":"https://github.com/keras-team/keras.git","svn_url":"https://github.com/keras-team/keras","homepage":"http://keras.io/","size":10638,"stargazers_count":25356,"watchers_count":25356,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":9274,"mirror_url":null,"archived":false,"open_issues_count":1030,"license":{"key":"other","name":"Other","spdx_id":null,"url":null},"forks":9274,"open_issues":1030,"watchers":25356,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/keras-team/keras/pulls/9193"},"html":{"href":"https://github.com/keras-team/keras/pull/9193"},"issue":{"href":"https://api.github.com/repos/keras-team/keras/issues/9193"},"comments":{"href":"https://api.github.com/repos/keras-team/keras/issues/9193/comments"},"review_comments":{"href":"https://api.github.com/repos/keras-team/keras/pulls/9193/comments"},"review_comment":{"href":"https://api.github.com/repos/keras-team/keras/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/keras-team/keras/pulls/9193/commits"},"statuses":{"href":"https://api.github.com/repos/keras-team/keras/statuses/307de8436cc13b72681a10c83f528f66210fb34f"}},"author_association":"NONE"}}
{"actor":{"display_login":"bojone"}}