{"action":"created","comment":{"url":"https://api.github.com/repos/propelml/propel/pulls/comments/168940600","pull_request_review_id":97392676,"id":168940600,"diff_hunk":"@@ -157,84 +163,32 @@ export function ones(shape: types.Shape,\n   return ops.ones(shape, opts);\n }\n \n-/** A collection of named Tensors. Used with OptimizerSGD.\n- * Iterate over it like this:\n- *\n- *    import { Params } from \"propel\";\n- *    let params = new Params()\n- *    params.randn(\"A\", [2]);\n- *    params.zeros(\"B\", [2, 2]);\n- *    params.forEach((tensor, name) => {\n- *      console.log(name);\n- *      console.log(tensor);\n- *    });\n- */\n-export class Params {\n-  // Note TS doesn't allow extending Map:\n-  // https://github.com/Microsoft/TypeScript/issues/10853\n-  private store = new Map<string, Tensor>();\n-\n-  has(name: string): boolean {\n-    return this.store.has(name);\n-  }\n-\n-  get(name: string): Tensor {\n-    return this.store.get(name);\n-  }\n-\n-  set(name: string, t: Tensor): Tensor {\n-    this.store.set(name, t);\n-    return t;\n-  }\n+interface SGDOpts {\n+  lr: number;\n+  momentum?: number; // TODO currently unused.\n+}\n \n-  forEach(cb): void {\n-    this.store.forEach(cb);\n-  }\n+import { gradParams2 } from \"./backprop\";\n \n-  /** If the given name does not exist in the parameters object, this\n-   * initializes a new random normal tensor. If the name does exist\n-   * in the parameters object, this just returns that stored tensor.\n-   */\n-  randn(name: string, shape: types.Shape,\n-        { device = \"CPU:0\", scale = 0.1 } = {}): Tensor {\n-    if (!(shape instanceof Array)) {\n-      throw new Error(\"Randn takes a shape as an argument\");\n-    }\n-    if (this.has(name)) {\n-      return this.get(name);\n-    }\n-    // Initialize.\n-    let t = randn(shape).mul(scale);\n-    if (device && device !== \"CPU:0\") {\n-      t = new Tensor(bo.copyToDevice(t.basic, device));\n-    }\n-    this.set(name, t);\n-    return t;\n-  }\n+export function sgd(loss: Tensor, params: Params, opts: SGDOpts): void {\n+  // In sgd() we assume the loss is a function of the params already.\n+  assert(loss.rank === 0);\n+  gc((keep) => {\n+    const grads = gradParams2(loss, params);\n \n-  /** If the given name does not exist in the parameters object, this\n-   * initializes a new tensor with zero values. If the name does exist\n-   * in the parameters object, this just returns that stored tensor.\n-   */\n-  zeros(name: string, shape: types.Shape, dtype: types.DType = \"float32\",\n-        device = \"CPU:0\"): Tensor {\n-    if (!(shape instanceof Array)) {\n-      throw new Error(\"Zeros takes a shape as an argument\");\n-    }\n-    if (this.has(name)) {\n-      return this.get(name);\n-    }\n-    // Initialize.\n-    let t = zeros(shape);\n-    if (device && device !== \"CPU:0\") {\n-      t = new Tensor(bo.copyToDevice(t.basic, device));\n+    for (const name of Object.keys(grads)) {\n+      const g = grads[name];\n+      const p = params.get(name);\n+      // p -= g * lr\n+      p.assign(p.sub(g.mul(opts.lr)));\n+      keep(p);","path":"src/api.ts","position":114,"original_position":114,"commit_id":"872c8fc45bf35ab439d032afbe95e56512006053","original_commit_id":"872c8fc45bf35ab439d032afbe95e56512006053","user":{"login":"ry","id":80,"avatar_url":"https://avatars1.githubusercontent.com/u/80?v=4","gravatar_id":"","url":"https://api.github.com/users/ry","html_url":"https://github.com/ry","followers_url":"https://api.github.com/users/ry/followers","following_url":"https://api.github.com/users/ry/following{/other_user}","gists_url":"https://api.github.com/users/ry/gists{/gist_id}","starred_url":"https://api.github.com/users/ry/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ry/subscriptions","organizations_url":"https://api.github.com/users/ry/orgs","repos_url":"https://api.github.com/users/ry/repos","events_url":"https://api.github.com/users/ry/events{/privacy}","received_events_url":"https://api.github.com/users/ry/received_events","type":"User","site_admin":false},"body":"Good point - removed.","created_at":"2018-02-18T05:48:08Z","updated_at":"2018-02-18T05:48:08Z","html_url":"https://github.com/propelml/propel/pull/270#discussion_r168940600","pull_request_url":"https://api.github.com/repos/propelml/propel/pulls/270","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/propelml/propel/pulls/comments/168940600"},"html":{"href":"https://github.com/propelml/propel/pull/270#discussion_r168940600"},"pull_request":{"href":"https://api.github.com/repos/propelml/propel/pulls/270"}},"in_reply_to_id":168940400},"pull_request":{"url":"https://api.github.com/repos/propelml/propel/pulls/270","id":169797711,"html_url":"https://github.com/propelml/propel/pull/270","diff_url":"https://github.com/propelml/propel/pull/270.diff","patch_url":"https://github.com/propelml/propel/pull/270.patch","issue_url":"https://api.github.com/repos/propelml/propel/issues/270","number":270,"state":"open","locked":false,"title":"API improvements (rescale, params.init, params.scope, sgd)","user":{"login":"ry","id":80,"avatar_url":"https://avatars1.githubusercontent.com/u/80?v=4","gravatar_id":"","url":"https://api.github.com/users/ry","html_url":"https://github.com/ry","followers_url":"https://api.github.com/users/ry/followers","following_url":"https://api.github.com/users/ry/following{/other_user}","gists_url":"https://api.github.com/users/ry/gists{/gist_id}","starred_url":"https://api.github.com/users/ry/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ry/subscriptions","organizations_url":"https://api.github.com/users/ry/orgs","repos_url":"https://api.github.com/users/ry/repos","events_url":"https://api.github.com/users/ry/events{/privacy}","received_events_url":"https://api.github.com/users/ry/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-02-17T19:29:41Z","updated_at":"2018-02-18T05:48:08Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7929270319ff6bc5cd4c668422af5961ea56b8b0","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/propelml/propel/pulls/270/commits","review_comments_url":"https://api.github.com/repos/propelml/propel/pulls/270/comments","review_comment_url":"https://api.github.com/repos/propelml/propel/pulls/comments{/number}","comments_url":"https://api.github.com/repos/propelml/propel/issues/270/comments","statuses_url":"https://api.github.com/repos/propelml/propel/statuses/872c8fc45bf35ab439d032afbe95e56512006053","head":{"label":"propelml:r/rescale","ref":"r/rescale","sha":"872c8fc45bf35ab439d032afbe95e56512006053","user":{"login":"propelml","id":33702266,"avatar_url":"https://avatars1.githubusercontent.com/u/33702266?v=4","gravatar_id":"","url":"https://api.github.com/users/propelml","html_url":"https://github.com/propelml","followers_url":"https://api.github.com/users/propelml/followers","following_url":"https://api.github.com/users/propelml/following{/other_user}","gists_url":"https://api.github.com/users/propelml/gists{/gist_id}","starred_url":"https://api.github.com/users/propelml/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/propelml/subscriptions","organizations_url":"https://api.github.com/users/propelml/orgs","repos_url":"https://api.github.com/users/propelml/repos","events_url":"https://api.github.com/users/propelml/events{/privacy}","received_events_url":"https://api.github.com/users/propelml/received_events","type":"Organization","site_admin":false},"repo":{"id":110989998,"name":"propel","full_name":"propelml/propel","owner":{"login":"propelml","id":33702266,"avatar_url":"https://avatars1.githubusercontent.com/u/33702266?v=4","gravatar_id":"","url":"https://api.github.com/users/propelml","html_url":"https://github.com/propelml","followers_url":"https://api.github.com/users/propelml/followers","following_url":"https://api.github.com/users/propelml/following{/other_user}","gists_url":"https://api.github.com/users/propelml/gists{/gist_id}","starred_url":"https://api.github.com/users/propelml/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/propelml/subscriptions","organizations_url":"https://api.github.com/users/propelml/orgs","repos_url":"https://api.github.com/users/propelml/repos","events_url":"https://api.github.com/users/propelml/events{/privacy}","received_events_url":"https://api.github.com/users/propelml/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/propelml/propel","description":"Differential Programming in JavaScript.","fork":false,"url":"https://api.github.com/repos/propelml/propel","forks_url":"https://api.github.com/repos/propelml/propel/forks","keys_url":"https://api.github.com/repos/propelml/propel/keys{/key_id}","collaborators_url":"https://api.github.com/repos/propelml/propel/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/propelml/propel/teams","hooks_url":"https://api.github.com/repos/propelml/propel/hooks","issue_events_url":"https://api.github.com/repos/propelml/propel/issues/events{/number}","events_url":"https://api.github.com/repos/propelml/propel/events","assignees_url":"https://api.github.com/repos/propelml/propel/assignees{/user}","branches_url":"https://api.github.com/repos/propelml/propel/branches{/branch}","tags_url":"https://api.github.com/repos/propelml/propel/tags","blobs_url":"https://api.github.com/repos/propelml/propel/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/propelml/propel/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/propelml/propel/git/refs{/sha}","trees_url":"https://api.github.com/repos/propelml/propel/git/trees{/sha}","statuses_url":"https://api.github.com/repos/propelml/propel/statuses/{sha}","languages_url":"https://api.github.com/repos/propelml/propel/languages","stargazers_url":"https://api.github.com/repos/propelml/propel/stargazers","contributors_url":"https://api.github.com/repos/propelml/propel/contributors","subscribers_url":"https://api.github.com/repos/propelml/propel/subscribers","subscription_url":"https://api.github.com/repos/propelml/propel/subscription","commits_url":"https://api.github.com/repos/propelml/propel/commits{/sha}","git_commits_url":"https://api.github.com/repos/propelml/propel/git/commits{/sha}","comments_url":"https://api.github.com/repos/propelml/propel/comments{/number}","issue_comment_url":"https://api.github.com/repos/propelml/propel/issues/comments{/number}","contents_url":"https://api.github.com/repos/propelml/propel/contents/{+path}","compare_url":"https://api.github.com/repos/propelml/propel/compare/{base}...{head}","merges_url":"https://api.github.com/repos/propelml/propel/merges","archive_url":"https://api.github.com/repos/propelml/propel/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/propelml/propel/downloads","issues_url":"https://api.github.com/repos/propelml/propel/issues{/number}","pulls_url":"https://api.github.com/repos/propelml/propel/pulls{/number}","milestones_url":"https://api.github.com/repos/propelml/propel/milestones{/number}","notifications_url":"https://api.github.com/repos/propelml/propel/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/propelml/propel/labels{/name}","releases_url":"https://api.github.com/repos/propelml/propel/releases{/id}","deployments_url":"https://api.github.com/repos/propelml/propel/deployments","created_at":"2017-11-16T15:42:09Z","updated_at":"2018-02-18T04:57:46Z","pushed_at":"2018-02-18T05:48:08Z","git_url":"git://github.com/propelml/propel.git","ssh_url":"git@github.com:propelml/propel.git","clone_url":"https://github.com/propelml/propel.git","svn_url":"https://github.com/propelml/propel","homepage":"http://propelml.org/","size":6068,"stargazers_count":403,"watchers_count":403,"language":"TypeScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":17,"mirror_url":null,"archived":false,"open_issues_count":34,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":17,"open_issues":34,"watchers":403,"default_branch":"master"}},"base":{"label":"propelml:master","ref":"master","sha":"2f1b58c0ef6386b669cda0729e96083515f9a37b","user":{"login":"propelml","id":33702266,"avatar_url":"https://avatars1.githubusercontent.com/u/33702266?v=4","gravatar_id":"","url":"https://api.github.com/users/propelml","html_url":"https://github.com/propelml","followers_url":"https://api.github.com/users/propelml/followers","following_url":"https://api.github.com/users/propelml/following{/other_user}","gists_url":"https://api.github.com/users/propelml/gists{/gist_id}","starred_url":"https://api.github.com/users/propelml/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/propelml/subscriptions","organizations_url":"https://api.github.com/users/propelml/orgs","repos_url":"https://api.github.com/users/propelml/repos","events_url":"https://api.github.com/users/propelml/events{/privacy}","received_events_url":"https://api.github.com/users/propelml/received_events","type":"Organization","site_admin":false},"repo":{"id":110989998,"name":"propel","full_name":"propelml/propel","owner":{"login":"propelml","id":33702266,"avatar_url":"https://avatars1.githubusercontent.com/u/33702266?v=4","gravatar_id":"","url":"https://api.github.com/users/propelml","html_url":"https://github.com/propelml","followers_url":"https://api.github.com/users/propelml/followers","following_url":"https://api.github.com/users/propelml/following{/other_user}","gists_url":"https://api.github.com/users/propelml/gists{/gist_id}","starred_url":"https://api.github.com/users/propelml/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/propelml/subscriptions","organizations_url":"https://api.github.com/users/propelml/orgs","repos_url":"https://api.github.com/users/propelml/repos","events_url":"https://api.github.com/users/propelml/events{/privacy}","received_events_url":"https://api.github.com/users/propelml/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/propelml/propel","description":"Differential Programming in JavaScript.","fork":false,"url":"https://api.github.com/repos/propelml/propel","forks_url":"https://api.github.com/repos/propelml/propel/forks","keys_url":"https://api.github.com/repos/propelml/propel/keys{/key_id}","collaborators_url":"https://api.github.com/repos/propelml/propel/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/propelml/propel/teams","hooks_url":"https://api.github.com/repos/propelml/propel/hooks","issue_events_url":"https://api.github.com/repos/propelml/propel/issues/events{/number}","events_url":"https://api.github.com/repos/propelml/propel/events","assignees_url":"https://api.github.com/repos/propelml/propel/assignees{/user}","branches_url":"https://api.github.com/repos/propelml/propel/branches{/branch}","tags_url":"https://api.github.com/repos/propelml/propel/tags","blobs_url":"https://api.github.com/repos/propelml/propel/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/propelml/propel/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/propelml/propel/git/refs{/sha}","trees_url":"https://api.github.com/repos/propelml/propel/git/trees{/sha}","statuses_url":"https://api.github.com/repos/propelml/propel/statuses/{sha}","languages_url":"https://api.github.com/repos/propelml/propel/languages","stargazers_url":"https://api.github.com/repos/propelml/propel/stargazers","contributors_url":"https://api.github.com/repos/propelml/propel/contributors","subscribers_url":"https://api.github.com/repos/propelml/propel/subscribers","subscription_url":"https://api.github.com/repos/propelml/propel/subscription","commits_url":"https://api.github.com/repos/propelml/propel/commits{/sha}","git_commits_url":"https://api.github.com/repos/propelml/propel/git/commits{/sha}","comments_url":"https://api.github.com/repos/propelml/propel/comments{/number}","issue_comment_url":"https://api.github.com/repos/propelml/propel/issues/comments{/number}","contents_url":"https://api.github.com/repos/propelml/propel/contents/{+path}","compare_url":"https://api.github.com/repos/propelml/propel/compare/{base}...{head}","merges_url":"https://api.github.com/repos/propelml/propel/merges","archive_url":"https://api.github.com/repos/propelml/propel/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/propelml/propel/downloads","issues_url":"https://api.github.com/repos/propelml/propel/issues{/number}","pulls_url":"https://api.github.com/repos/propelml/propel/pulls{/number}","milestones_url":"https://api.github.com/repos/propelml/propel/milestones{/number}","notifications_url":"https://api.github.com/repos/propelml/propel/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/propelml/propel/labels{/name}","releases_url":"https://api.github.com/repos/propelml/propel/releases{/id}","deployments_url":"https://api.github.com/repos/propelml/propel/deployments","created_at":"2017-11-16T15:42:09Z","updated_at":"2018-02-18T04:57:46Z","pushed_at":"2018-02-18T05:48:08Z","git_url":"git://github.com/propelml/propel.git","ssh_url":"git@github.com:propelml/propel.git","clone_url":"https://github.com/propelml/propel.git","svn_url":"https://github.com/propelml/propel","homepage":"http://propelml.org/","size":6068,"stargazers_count":403,"watchers_count":403,"language":"TypeScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":17,"mirror_url":null,"archived":false,"open_issues_count":34,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":17,"open_issues":34,"watchers":403,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/propelml/propel/pulls/270"},"html":{"href":"https://github.com/propelml/propel/pull/270"},"issue":{"href":"https://api.github.com/repos/propelml/propel/issues/270"},"comments":{"href":"https://api.github.com/repos/propelml/propel/issues/270/comments"},"review_comments":{"href":"https://api.github.com/repos/propelml/propel/pulls/270/comments"},"review_comment":{"href":"https://api.github.com/repos/propelml/propel/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/propelml/propel/pulls/270/commits"},"statuses":{"href":"https://api.github.com/repos/propelml/propel/statuses/872c8fc45bf35ab439d032afbe95e56512006053"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"ry"}}