{"action":"created","issue":{"url":"https://api.github.com/repos/rstudio/keras/issues/480","repository_url":"https://api.github.com/repos/rstudio/keras","labels_url":"https://api.github.com/repos/rstudio/keras/issues/480/labels{/name}","comments_url":"https://api.github.com/repos/rstudio/keras/issues/480/comments","events_url":"https://api.github.com/repos/rstudio/keras/issues/480/events","html_url":"https://github.com/rstudio/keras/issues/480","id":344202601,"node_id":"MDU6SXNzdWUzNDQyMDI2MDE=","number":480,"title":"Compute lag in custom loss function","user":{"login":"Vidaringa","id":20194883,"node_id":"MDQ6VXNlcjIwMTk0ODgz","avatar_url":"https://avatars2.githubusercontent.com/u/20194883?v=4","gravatar_id":"","url":"https://api.github.com/users/Vidaringa","html_url":"https://github.com/Vidaringa","followers_url":"https://api.github.com/users/Vidaringa/followers","following_url":"https://api.github.com/users/Vidaringa/following{/other_user}","gists_url":"https://api.github.com/users/Vidaringa/gists{/gist_id}","starred_url":"https://api.github.com/users/Vidaringa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Vidaringa/subscriptions","organizations_url":"https://api.github.com/users/Vidaringa/orgs","repos_url":"https://api.github.com/users/Vidaringa/repos","events_url":"https://api.github.com/users/Vidaringa/events{/privacy}","received_events_url":"https://api.github.com/users/Vidaringa/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-07-24T21:02:27Z","updated_at":"2018-07-25T07:51:55Z","closed_at":null,"author_association":"NONE","body":"I wan't to make a custom loss function where I compute the lag of y_pred or y_true. From here: [#257](https://github.com/rstudio/keras/issues/257) I read that I can't convert a tensor to an R object, which I would have to do to use the lag function in R. Taking a look at the backend functions I can't see anyone which compute the lag for me (e.g. k_lag).\r\n\r\nIs there any way for me to compute the lag of y_true or y_pred?"},"comment":{"url":"https://api.github.com/repos/rstudio/keras/issues/comments/407666690","html_url":"https://github.com/rstudio/keras/issues/480#issuecomment-407666690","issue_url":"https://api.github.com/repos/rstudio/keras/issues/480","id":407666690,"node_id":"MDEyOklzc3VlQ29tbWVudDQwNzY2NjY5MA==","user":{"login":"skeydan","id":469371,"node_id":"MDQ6VXNlcjQ2OTM3MQ==","avatar_url":"https://avatars1.githubusercontent.com/u/469371?v=4","gravatar_id":"","url":"https://api.github.com/users/skeydan","html_url":"https://github.com/skeydan","followers_url":"https://api.github.com/users/skeydan/followers","following_url":"https://api.github.com/users/skeydan/following{/other_user}","gists_url":"https://api.github.com/users/skeydan/gists{/gist_id}","starred_url":"https://api.github.com/users/skeydan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/skeydan/subscriptions","organizations_url":"https://api.github.com/users/skeydan/orgs","repos_url":"https://api.github.com/users/skeydan/repos","events_url":"https://api.github.com/users/skeydan/events{/privacy}","received_events_url":"https://api.github.com/users/skeydan/received_events","type":"User","site_admin":false},"created_at":"2018-07-25T07:51:55Z","updated_at":"2018-07-25T07:51:55Z","author_association":"COLLABORATOR","body":"Hi, not entirely sure what you want to do, but assuming you want to compare the respective lags (diffs from one point in time to the next) between y_true and y_pred, you could do something like this (simple example with just 2 axes, a batch axis and a feature axis):\r\n\r\n```\r\nlibrary(keras)\r\n\r\nloss <- function(y_true, y_pred) {\r\n  lag_y_true <- y_true[ , 2:dim(y_true)[2]] - y_true[ , 1:(dim(y_true)[2] - 1)]\r\n  lag_y_pred <- y_pred[ , 2:dim(y_true)[2]] - y_pred[ , 1:(dim(y_true)[2] - 1)]\r\n # assuming you want to subtract the lags and average them, but could do other things too \r\n  k_mean(lag_y_pred - lag_y_true)\r\n}\r\n\r\n# test\r\nsess <- k_get_session()\r\nys <- k_constant(c(1,2,3,4,5,6), shape = c(2,3))\r\nyhats <- k_constant(c(1,3,5,7,9,11), shape = c(2,3))\r\nsess$run(ys)\r\nsess$run(yhats)\r\nsess$run(loss(ys, yhats))\r\n```"}}
{"actor":{"display_login":"skeydan"}}