Skip to content

Commit 732d8a1

Browse files
author
浅梦
authored
v0.1.1
v0.1.1
2 parents c90bffd + 83e4442 commit 732d8a1

File tree

10 files changed

+113
-39
lines changed

10 files changed

+113
-39
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug(问题描述)**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce(复现步骤)**
14+
Steps to reproduce the behavior:
15+
1. Go to '...'
16+
2. Click on '....'
17+
3. Scroll down to '....'
18+
4. See error
19+
20+
**Operating environment(运行环境):**
21+
- python version [e.g. 3.4, 3.6]
22+
- tensorflow version [e.g. 1.4.0, 1.12.0]
23+
- deepmatch version [e.g. 0.1.1,]
24+
25+
**Additional context**
26+
Add any other context about the problem here.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: enhancement&feature request
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

.github/ISSUE_TEMPLATE/question.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Question
3+
about: Ask any question ~
4+
title: ''
5+
labels: question
6+
assignees: ''
7+
8+
---
9+
Please refer to the [FAQ](https://deepmatch.readthedocs.io/en/latest/FAQ.html) in doc and search for the [related issues](https://github.com/shenweichen/DeepCTR/issues) before you ask the question.
10+
11+
**Describe the question(问题描述)**
12+
A clear and concise description of what the question is.
13+
14+
**Additional context**
15+
Add any other context about the problem here.
16+
17+
**Operating environment(运行环境):**
18+
- python version [e.g. 3.6]
19+
- tensorflow version [e.g. 1.4.0,]
20+
- deepmatch version [e.g. 0.1.1,]

CONTRIBUTING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This project is under development and we need developers to participate in.
2+
3+
If you
4+
5+
- familiar with and interested in matching algorithms
6+
- familiar with tensorflow
7+
- have spare time to learn and develop
8+
- familiar with git
9+
10+
please send a brief introduction of your background and experience to [email protected], welcome to join us!

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
[![Disscussion](https://img.shields.io/badge/chat-wechat-brightgreen?style=flat)](./README.md#disscussiongroup)
1313
[![License](https://img.shields.io/github/license/shenweichen/deepmatch.svg)](https://github.com/shenweichen/deepmatch/blob/master/LICENSE)
1414

15-
DeepMatch is a deep matching model library for recommendations, advertising, and search. It's easy to **train models** and to **export representation vectors** for user and item which can be used for **ANN search**.You can use any complex model with `model.fit()`and `model.predict()` .
15+
DeepMatch is a deep matching model library for recommendations & advertising. It's easy to **train models** and to **export representation vectors** for user and item which can be used for **ANN search**.You can use any complex model with `model.fit()`and `model.predict()` .
1616

1717
Let's [**Get Started!**](https://deepmatch.readthedocs.io/en/latest/Quick-Start.html)
1818

deepmatch/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .utils import check_version
22

3-
__version__ = '0.1.0'
3+
__version__ = '0.1.1'
44
check_version(__version__)

deepmatch/layers/core.py

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -153,53 +153,51 @@ def compute_output_shape(self, input_shape):
153153

154154

155155
class CapsuleLayer(Layer):
156-
def __init__(self, input_units, out_units, max_len, k_max, iteration=3,
157-
weight_initializer=RandomNormal(stddev=1.0), **kwargs):
158-
self.input_units = input_units # E1
159-
self.out_units = out_units # E2
156+
def __init__(self, input_units, out_units, max_len, k_max, iteration_times=3,
157+
initializer=RandomNormal(stddev=1.0), **kwargs):
158+
self.input_units = input_units
159+
self.out_units = out_units
160160
self.max_len = max_len
161161
self.k_max = k_max
162-
self.iteration = iteration
163-
self.weight_initializer = weight_initializer
162+
self.iteration_times = iteration_times
163+
self.initializer = initializer
164164
super(CapsuleLayer, self).__init__(**kwargs)
165165

166166
def build(self, input_shape):
167-
self.B_matrix = self.add_weight(shape=[1, self.k_max, self.max_len], initializer=self.weight_initializer,
168-
trainable=False, name="B", dtype=tf.float32) # [1,K,H]
169-
self.S_matrix = self.add_weight(shape=[self.input_units, self.out_units], initializer=self.weight_initializer,
170-
name="S", dtype=tf.float32)
167+
self.routing_logits = self.add_weight(shape=[1, self.k_max, self.max_len], initializer=self.initializer,
168+
trainable=False, name="B", dtype=tf.float32)
169+
self.bilinear_mapping_matrix = self.add_weight(shape=[self.input_units, self.out_units],
170+
initializer=self.initializer,
171+
name="S", dtype=tf.float32)
171172
super(CapsuleLayer, self).build(input_shape)
172173

173-
def call(self, inputs, **kwargs): # seq_len:[B,1]
174-
low_capsule, seq_len = inputs
175-
B = tf.shape(low_capsule)[0]
176-
seq_len_tile = tf.tile(seq_len, [1, self.k_max]) # [B,K]
177-
178-
for i in range(self.iteration):
179-
mask = tf.sequence_mask(seq_len_tile, self.max_len) # [B,K,H]
180-
pad = tf.ones_like(mask, dtype=tf.float32) * (-2 ** 16 + 1) # [B,K,H]
181-
B_tile = tf.tile(self.B_matrix, [B, 1, 1]) # [B,K,H]
182-
B_mask = tf.where(mask, B_tile, pad)
183-
W = tf.nn.softmax(B_mask) # [B,K,H]
184-
low_capsule_new = tf.tensordot(low_capsule, self.S_matrix, axes=1) # [B,H,E2]
185-
high_capsule_tmp = tf.matmul(W, low_capsule_new) # [B,K,E2]
186-
high_capsule = squash(high_capsule_tmp) # [B,K,E2]
187-
188-
# ([B,K,E2], [B,H,E2]->[B,E2,H])->[B,K,H]->[1,K,H]
189-
B_delta = tf.reduce_sum(
190-
tf.matmul(high_capsule, tf.transpose(low_capsule_new, perm=[0, 2, 1])),
174+
def call(self, inputs, **kwargs):
175+
behavior_embddings, seq_len = inputs
176+
batch_size = tf.shape(behavior_embddings)[0]
177+
seq_len_tile = tf.tile(seq_len, [1, self.k_max])
178+
179+
for i in range(self.iteration_times):
180+
mask = tf.sequence_mask(seq_len_tile, self.max_len)
181+
pad = tf.ones_like(mask, dtype=tf.float32) * (-2 ** 32 + 1)
182+
routing_logits_with_padding = tf.where(mask, tf.tile(self.routing_logits, [batch_size, 1, 1]), pad)
183+
weight = tf.nn.softmax(routing_logits_with_padding)
184+
behavior_embdding_mapping = tf.tensordot(behavior_embddings, self.bilinear_mapping_matrix, axes=1)
185+
Z = tf.matmul(weight, behavior_embdding_mapping)
186+
interet_capsules = squash(Z)
187+
delta_routing_logits = tf.reduce_sum(
188+
tf.matmul(interet_capsules, tf.transpose(behavior_embdding_mapping, perm=[0, 2, 1])),
191189
axis=0, keep_dims=True
192-
) # [1,K,H]
193-
self.B_matrix.assign_add(B_delta)
194-
high_capsule = tf.reshape(high_capsule, [-1, self.k_max, self.out_units])
195-
return high_capsule
190+
)
191+
self.routing_logits.assign_add(delta_routing_logits)
192+
interet_capsules = tf.reshape(interet_capsules, [-1, self.k_max, self.out_units])
193+
return interet_capsules
196194

197195
def compute_output_shape(self, input_shape):
198196
return (None, self.k_max, self.out_units)
199197

200198

201199
def squash(inputs):
202200
vec_squared_norm = tf.reduce_sum(tf.square(inputs), axis=-1, keep_dims=True)
203-
scalar_factor = vec_squared_norm / (1 + vec_squared_norm) / tf.sqrt(vec_squared_norm + 1e-9)
204-
vec_squashed = scalar_factor * inputs # element-wise
201+
scalar_factor = vec_squared_norm / (1 + vec_squared_norm) / tf.sqrt(vec_squared_norm + 1e-8)
202+
vec_squashed = scalar_factor * inputs
205203
return vec_squashed

deepmatch/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import tensorflow as tf
2222
from tensorflow.python.keras import backend as K
23-
from tensorflow.python.keras._impl.keras.layers import Lambda
23+
from tensorflow.python.keras.layers import Lambda
2424

2525
def recall_N(y_true, y_pred, N=50):
2626
return len(set(y_pred[:N]) & set(y_true)) * 1.0 / len(y_true)

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# The short X.Y version
2727
version = ''
2828
# The full version, including alpha/beta/rc tags
29-
release = '0.1.0'
29+
release = '0.1.1'
3030

3131

3232
# -- General configuration ---------------------------------------------------

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setuptools.setup(
1111
name="deepmatch",
12-
version="0.1.0",
12+
version="0.1.1",
1313
author="Weichen Shen",
1414
author_email="[email protected]",
1515
description="Deep matching model library for recommendations, advertising, and search. It's easy to train models and to **export representation vectors** for user and item which can be used for **ANN search**.",

0 commit comments

Comments
 (0)