diff options
author | Lester Solbakken <lesters@yahoo-inc.com> | 2017-07-04 16:58:43 +0200 |
---|---|---|
committer | Lester Solbakken <lesters@yahoo-inc.com> | 2017-07-04 16:58:43 +0200 |
commit | 8a1482eb4fce4b1b6aa6b372cdae6219eccbbd2f (patch) | |
tree | f92fe18160edc7ec44a1715618a31f50c9770ebc /sample-apps | |
parent | 57e2d94aee61a61470eed2c736aefb4edcce5f2d (diff) |
Update blog recommendation sample app
Diffstat (limited to 'sample-apps')
6 files changed, 17 insertions, 83 deletions
diff --git a/sample-apps/blog-recommendation/README.md b/sample-apps/blog-recommendation/README.md index 705c4e6879c..232678f677b 100644 --- a/sample-apps/blog-recommendation/README.md +++ b/sample-apps/blog-recommendation/README.md @@ -1 +1 @@ -Blog Recommendation +# Blog Recommendation diff --git a/sample-apps/blog-recommendation/src/main/application/constants/input_transform_1.json b/sample-apps/blog-recommendation/src/main/application/constants/input_transform_1.json deleted file mode 100644 index c28dbf49ddf..00000000000 --- a/sample-apps/blog-recommendation/src/main/application/constants/input_transform_1.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "cells": [ - { "address": { "user_item_cf": "0", "input": "10"}, "value": 1.0 }, - { "address": { "user_item_cf": "1", "input": "11"}, "value": 1.0 }, - { "address": { "user_item_cf": "2", "input": "12"}, "value": 1.0 }, - { "address": { "user_item_cf": "3", "input": "13"}, "value": 1.0 }, - { "address": { "user_item_cf": "4", "input": "14"}, "value": 1.0 }, - { "address": { "user_item_cf": "5", "input": "15"}, "value": 1.0 }, - { "address": { "user_item_cf": "6", "input": "16"}, "value": 1.0 }, - { "address": { "user_item_cf": "7", "input": "17"}, "value": 1.0 }, - { "address": { "user_item_cf": "8", "input": "18"}, "value": 1.0 }, - { "address": { "user_item_cf": "9", "input": "19"}, "value": 1.0 } - ] -} diff --git a/sample-apps/blog-recommendation/src/main/application/constants/input_transform_2.json b/sample-apps/blog-recommendation/src/main/application/constants/input_transform_2.json deleted file mode 100644 index e4d5d88baf8..00000000000 --- a/sample-apps/blog-recommendation/src/main/application/constants/input_transform_2.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "cells": [ - { "address": { "user_item_cf": "0", "input": "0"}, "value": 1.0 }, - { "address": { "user_item_cf": "1", "input": "1"}, "value": 1.0 }, - { "address": { "user_item_cf": "2", "input": "2"}, "value": 1.0 }, - { "address": { "user_item_cf": "3", "input": "3"}, "value": 1.0 }, - { "address": { "user_item_cf": "4", "input": "4"}, "value": 1.0 }, - { "address": { "user_item_cf": "5", "input": "5"}, "value": 1.0 }, - { "address": { "user_item_cf": "6", "input": "6"}, "value": 1.0 }, - { "address": { "user_item_cf": "7", "input": "7"}, "value": 1.0 }, - { "address": { "user_item_cf": "8", "input": "8"}, "value": 1.0 }, - { "address": { "user_item_cf": "9", "input": "9"}, "value": 1.0 } - ] -} diff --git a/sample-apps/blog-recommendation/src/main/application/searchdefinitions/blog_post.sd b/sample-apps/blog-recommendation/src/main/application/searchdefinitions/blog_post.sd index 6aec8961475..74918b89460 100644 --- a/sample-apps/blog-recommendation/src/main/application/searchdefinitions/blog_post.sd +++ b/sample-apps/blog-recommendation/src/main/application/searchdefinitions/blog_post.sd @@ -44,7 +44,7 @@ search blog_post { indexing: summary | index } - field user_item_cf type tensor { + field user_item_cf type tensor(user_item_cf[10]) { indexing: summary | attribute attribute: tensor(user_item_cf[10]) } @@ -84,75 +84,38 @@ search blog_post { type: tensor(final[1]) } - constant input_transform_1 { - file: constants/input_transform_1.json - type: tensor(user_item_cf[10], input[20]) - } - - constant input_transform_2 { - file: constants/input_transform_2.json - type: tensor(user_item_cf[10], input[20]) - } - + # rank profile with neural network model as second phase rank-profile nn_tensor { - # Why no work? - macro inline matmul(x, y, dim) { - expression: sum(x * y, dim) - } - - macro matmul_user_item_cf(x, y) { - expression: sum(x * y, user_item_cf) - } - - macro matmul_hidden(x, y) { - expression: sum(x * y, hidden) - } - - macro matmul_input(x, y) { - expression: sum(x * y, input) - } - - macro add(x,y) { - expression: x + y - } - - # The input to the neural network is the concatenation of the document and query vectors + # The input to the neural network is the + # concatenation of the document and query vectors. macro nn_input() { - expression { - matmul_user_item_cf(attribute(user_item_cf), constant(input_transform_1)) - + - matmul_user_item_cf(query(user_item_cf), constant(input_transform_2)) - } + expression: concat(attribute(user_item_cf), query(user_item_cf), input) } + # Computes the hidden layer macro hidden_layer() { - expression { - relu(add(matmul_input(nn_input, constant(W_hidden)), constant(b_hidden))) - - # The '+' causes an error. Why? - # relu(matmul(nn_input, constant(W_hidden), "input") + constant(b_hidden)) - } + expression: relu(sum(nn_input * constant(W_hidden), input) + constant(b_hidden)) } + # Computes the output layer macro final_layer() { - expression{ - sigmoid(add(matmul_hidden(hidden_layer, constant(W_final)), constant(b_final))) - - # Same as above - # sigmoid(matmul(hidden_layer, constant(W_final), "hidden") + constant(b_final)) - } + expression: sigmoid(sum(hidden_layer * constant(W_final), hidden) + constant(b_final)) } + + # First-phase ranking: + # Dot-product between user and document latent factors first-phase { expression: sum(query(user_item_cf) * attribute(user_item_cf)) } + # Second-phase ranking: + # Neural network model based on the user and latent factors second-phase { rerank-count: 200 expression: sum(final_layer) } - } } diff --git a/sample-apps/blog-recommendation/src/main/application/searchdefinitions/user.sd b/sample-apps/blog-recommendation/src/main/application/searchdefinitions/user.sd index 3f545ef406c..e7f3e89ac9a 100644 --- a/sample-apps/blog-recommendation/src/main/application/searchdefinitions/user.sd +++ b/sample-apps/blog-recommendation/src/main/application/searchdefinitions/user.sd @@ -11,8 +11,7 @@ search user { indexing: summary | attribute } - # see http://vespa.corp.yahoo.com/6/documentation/reference/search-definitions.html#tensor-type-spec - field user_item_cf type tensor { + field user_item_cf type tensor(user_item_cf[10]) { indexing: summary | attribute attribute: tensor(user_item_cf[10]) } diff --git a/sample-apps/blog-recommendation/src/main/java/com/yahoo/example/BlogTensorSearcher.java b/sample-apps/blog-recommendation/src/main/java/com/yahoo/example/BlogTensorSearcher.java index 44fdcef583b..086a43c6cd6 100644 --- a/sample-apps/blog-recommendation/src/main/java/com/yahoo/example/BlogTensorSearcher.java +++ b/sample-apps/blog-recommendation/src/main/java/com/yahoo/example/BlogTensorSearcher.java @@ -28,7 +28,7 @@ public class BlogTensorSearcher extends Searcher { // Modify the query by restricting to blog_posts... query.getModel().setRestrict("blog_post"); - // ... that has a tensor field fed and does not contains already read items. + // ... that has a tensor field fed and does not contain already read items. NotItem notItem = new NotItem(); notItem.addItem(new IntItem(1, "has_user_item_cf")); for (String item : getReadItems(query)) { |