summaryrefslogtreecommitdiffstats
path: root/sample-apps/blog-recommendation/src/main/application/searchdefinitions/blog_post.sd
blob: 6aec8961475457416798131976a8dc08e01e9c69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
search blog_post {
    document blog_post {
        field date_gmt type string {
            indexing: summary
        }
        field language type string {
            indexing: summary
        }

        field author type string {
            indexing: summary
        }

        field url type string {
            indexing: summary
        }

        field title type string {
            indexing: summary | index
        }

	    field blog type string {
	        indexing: summary
	    }

	    field post_id type string {
	        indexing: summary
	    }

        field tags type array<string> {
            indexing: summary
        }

        field blogname type string {
            indexing: summary
        }

        field content type string {
            indexing: summary | index
        }

        field categories type array<string> {
            indexing: summary | index
        }

        field user_item_cf type tensor {
            indexing: summary | attribute
            attribute: tensor(user_item_cf[10])
        }

        field has_user_item_cf type byte {
            indexing: summary | attribute
            attribute: fast-search
        }

    }

    rank-profile tensor {
        first-phase {
            expression {
                sum(query(user_item_cf) * attribute(user_item_cf))
            }
        }
    }

    constant W_hidden {
        file: constants/W_hidden.json
        type: tensor(input[20],hidden[40])
    }

    constant b_hidden {
        file: constants/b_hidden.json
        type: tensor(hidden[40])
    }

    constant W_final {
        file: constants/W_final.json
        type: tensor(hidden[40], final[1])
    }

    constant b_final {
        file: constants/b_final.json
        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 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
        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))
            }
        }

        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))
            }
        }

        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))
            }
        }

        first-phase {
            expression: sum(query(user_item_cf) * attribute(user_item_cf))
        }

        second-phase {
            rerank-count: 200
            expression: sum(final_layer)
        }

    }

}