Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
shenweichen committed Apr 10, 2020
1 parent 4816663 commit 832474d
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 71 deletions.
5 changes: 2 additions & 3 deletions deepmatch/layers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
from deepctr.layers import custom_objects
from deepctr.layers.utils import reduce_sum

from .core import PoolingLayer, SampledSoftmaxLayer, Similarity, LabelAwareAttention, CapsuleLayer,SampledSoftmaxLayerv2,EmbeddingIndex
from .core import PoolingLayer, Similarity, LabelAwareAttention, CapsuleLayer,SampledSoftmaxLayer,EmbeddingIndex
from ..utils import sampledsoftmaxloss

_custom_objects = {'PoolingLayer': PoolingLayer,
'SampledSoftmaxLayer': SampledSoftmaxLayer,
'Similarity': Similarity,
'LabelAwareAttention': LabelAwareAttention,
'CapsuleLayer': CapsuleLayer,
'reduce_sum':reduce_sum,
'SampledSoftmaxLayerv2':SampledSoftmaxLayerv2,
'SampledSoftmaxLayer':SampledSoftmaxLayer,
'sampledsoftmaxloss':sampledsoftmaxloss,
'EmbeddingIndex':EmbeddingIndex
}
Expand Down
50 changes: 3 additions & 47 deletions deepmatch/layers/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,55 +44,11 @@ def get_config(self, ):


class SampledSoftmaxLayer(Layer):
def __init__(self, item_embedding, num_sampled=5, **kwargs):
self.num_sampled = num_sampled
self.target_song_size = item_embedding.input_dim
self.item_embedding = item_embedding
super(SampledSoftmaxLayer, self).__init__(**kwargs)

def build(self, input_shape):
self.zero_bias = self.add_weight(shape=[self.target_song_size],
initializer=Zeros,
dtype=tf.float32,
trainable=False,
name="bias")
if not self.item_embedding.built:
self.item_embedding.build([])
self.trainable_weights.append(self.item_embedding.embeddings)
super(SampledSoftmaxLayer, self).build(input_shape)

def call(self, inputs_with_label_idx, training=None, **kwargs):
"""
The first input should be the model as it were, and the second the
target (i.e., a repeat of the training data) to compute the labels
argument
"""
inputs, label_idx = inputs_with_label_idx

loss = tf.nn.sampled_softmax_loss(weights=self.item_embedding.embeddings,
biases=self.zero_bias,
labels=label_idx,
inputs=inputs,
num_sampled=self.num_sampled,
num_classes=self.target_song_size
)
return tf.expand_dims(loss, axis=1)

def compute_output_shape(self, input_shape):
return (None, 1)

def get_config(self, ):
config = {'item_embedding': self.item_embedding, 'num_sampled': self.num_sampled}
base_config = super(SampledSoftmaxLayer, self).get_config()
return dict(list(base_config.items()) + list(config.items()))


class SampledSoftmaxLayerv2(Layer):
def __init__(self, num_sampled=5, **kwargs):
self.num_sampled = num_sampled
# self.target_song_size = item_embedding.input_dim
# self.item_embedding = item_embedding
super(SampledSoftmaxLayerv2, self).__init__(**kwargs)
super(SampledSoftmaxLayer, self).__init__(**kwargs)

def build(self, input_shape):
self.size = input_shape[0][0]
Expand All @@ -104,7 +60,7 @@ def build(self, input_shape):
# if not self.item_embedding.built:
# self.item_embedding.build([])
# self.trainable_weights.append(self.item_embedding.embeddings)
super(SampledSoftmaxLayerv2, self).build(input_shape)
super(SampledSoftmaxLayer, self).build(input_shape)

def call(self, inputs_with_label_idx, training=None, **kwargs):
"""
Expand All @@ -128,7 +84,7 @@ def compute_output_shape(self, input_shape):

def get_config(self, ):
config = {'num_sampled': self.num_sampled}
base_config = super(SampledSoftmaxLayerv2, self).get_config()
base_config = super(SampledSoftmaxLayer, self).get_config()
return dict(list(base_config.items()) + list(config.items()))


Expand Down
4 changes: 2 additions & 2 deletions deepmatch/models/dssm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from ..layers.core import Similarity


def DSSM(user_feature_columns, item_feature_columns, user_dnn_hidden_units=(64, 16),
item_dnn_hidden_units=(64, 16),
def DSSM(user_feature_columns, item_feature_columns, user_dnn_hidden_units=(64, 32),
item_dnn_hidden_units=(64, 32),
dnn_activation='tanh', dnn_use_bn=False,
l2_reg_dnn=0, l2_reg_embedding=1e-6, dnn_dropout=0, init_std=0.0001, seed=1024, metric='cos'):
"""Instantiates the Deep Structured Semantic Model architecture.
Expand Down
10 changes: 5 additions & 5 deletions deepmatch/models/mind.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
combined_dnn_input
from deepctr.layers.core import DNN
from deepctr.layers.utils import NoMask
from tensorflow.python.keras.layers import Concatenate,Input,Lambda
from tensorflow.python.keras.layers import Concatenate
from tensorflow.python.keras.models import Model

from deepmatch.utils import get_item_embedding,get_item_embeddingv2
from deepmatch.utils import get_item_embedding
from ..inputs import create_embedding_matrix
from ..layers.core import CapsuleLayer, SampledSoftmaxLayer, PoolingLayer, LabelAwareAttention,SampledSoftmaxLayerv2,EmbeddingIndex
from ..layers.core import CapsuleLayer, PoolingLayer, LabelAwareAttention,SampledSoftmaxLayer,EmbeddingIndex


def shape_target(target_emb_tmp, target_emb_size):
Expand Down Expand Up @@ -143,14 +143,14 @@ def MIND(user_feature_columns, item_feature_columns, num_sampled=5, k_max=2, p=1
else:
user_embedding_final = LabelAwareAttention(k_max=k_max, pow_p=p, )((user_embeddings, target_emb))

output = SampledSoftmaxLayerv2( num_sampled=num_sampled)(
output = SampledSoftmaxLayer(num_sampled=num_sampled)(
inputs=(pooling_item_embedding_weight,user_embedding_final, item_features[item_feature_name]))
model = Model(inputs=inputs_list + item_inputs_list, outputs=output)

model.__setattr__("user_input", inputs_list)
model.__setattr__("user_embedding", user_embeddings)

model.__setattr__("item_input", item_inputs_list)
model.__setattr__("item_embedding", get_item_embeddingv2(pooling_item_embedding_weight, item_features[item_feature_name]))
model.__setattr__("item_embedding", get_item_embedding(pooling_item_embedding_weight, item_features[item_feature_name]))

return model
2 changes: 1 addition & 1 deletion deepmatch/models/ncf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

def NCF(user_feature_columns, item_feature_columns, user_gmf_embedding_dim=20, item_gmf_embedding_dim=20,
user_mlp_embedding_dim=20, item_mlp_embedding_dim=20, dnn_use_bn=False,
dnn_hidden_units=(64, 16), dnn_activation='relu', l2_reg_dnn=0, l2_reg_embedding=1e-6, dnn_dropout=0,
dnn_hidden_units=(64, 32), dnn_activation='relu', l2_reg_dnn=0, l2_reg_embedding=1e-6, dnn_dropout=0,
init_std=0.0001, seed=1024):
"""Instantiates the NCF Model architecture.
Expand Down
10 changes: 5 additions & 5 deletions deepmatch/models/youtubednn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
from deepctr.layers.utils import NoMask
from tensorflow.python.keras.models import Model

from deepmatch.utils import get_item_embedding, get_item_embeddingv2
from deepmatch.utils import get_item_embedding
from deepmatch.layers import PoolingLayer
from ..inputs import input_from_feature_columns
from ..layers.core import SampledSoftmaxLayer, SampledSoftmaxLayerv2,EmbeddingIndex
from ..layers.core import SampledSoftmaxLayer,EmbeddingIndex


def YoutubeDNN(user_feature_columns, item_feature_columns, num_sampled=5,
user_dnn_hidden_units=(64, 16),
user_dnn_hidden_units=(64, 32),
dnn_activation='relu', dnn_use_bn=False,
l2_reg_dnn=0, l2_reg_embedding=1e-6, dnn_dropout=0, init_std=0.0001, seed=1024, ):
"""Instantiates the YoutubeDNN Model architecture.
Expand Down Expand Up @@ -66,14 +66,14 @@ def YoutubeDNN(user_feature_columns, item_feature_columns, num_sampled=5,

pooling_item_embedding_weight = PoolingLayer()([item_embedding_weight])

output = SampledSoftmaxLayerv2(num_sampled=num_sampled)(
output = SampledSoftmaxLayer(num_sampled=num_sampled)(
inputs=(pooling_item_embedding_weight, user_dnn_out, item_features[item_feature_name]))
model = Model(inputs=user_inputs_list + item_inputs_list , outputs=output)

model.__setattr__("user_input", user_inputs_list)
model.__setattr__("user_embedding", user_dnn_out)

model.__setattr__("item_input", item_inputs_list)
model.__setattr__("item_embedding", get_item_embeddingv2(pooling_item_embedding_weight, item_features[item_feature_name]))
model.__setattr__("item_embedding", get_item_embedding(pooling_item_embedding_weight, item_features[item_feature_name]))

return model
7 changes: 1 addition & 6 deletions deepmatch/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ def recall_N(y_true, y_pred, N=50):
def sampledsoftmaxloss(y_true, y_pred):
return K.mean(y_pred)


def get_item_embedding(item_embedding_layer, item_input_layer):
return Lambda(lambda x: tf.squeeze(tf.gather(item_embedding_layer.embeddings, x), axis=1))(
item_input_layer)

def get_item_embeddingv2(item_embedding, item_input_layer):
def get_item_embedding(item_embedding, item_input_layer):
return Lambda(lambda x: tf.squeeze(tf.gather(item_embedding, x), axis=1))(
item_input_layer)

Expand Down
2 changes: 1 addition & 1 deletion docs/source/History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# History
- 04/10/2020 : DeepMatch first version is released on [PyPi](https://pypi.org/project/deepmatch/)
- 04/10/2020 : [v0.1.2](https://github.com/shenweichen/DeepMatch/releases/tag/v0.1.2) released.Support [saving and loading model](./FAQ.html#save-or-load-weights-models).
- 04/06/2020 : DeepMatch first version is released on [PyPi](https://pypi.org/project/deepmatch/)
2 changes: 2 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ You can read the latest code at https://github.com/shenweichen/DeepMatch
News
-----

04/10/2020 : Support `saving and loading model <./FAQ.html#save-or-load-weights-models>`_ . `Changelog <https://github.com/shenweichen/DeepMatch/releases/tag/v0.1.2>`_

04/06/2020 : DeepMatch first version .

DisscussionGroup
Expand Down
2 changes: 1 addition & 1 deletion examples/run_dssm_negsampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

# 2.count #unique features for each sparse field and generate feature config for sequence feature

embedding_dim = 16
embedding_dim = 8

user_feature_columns = [SparseFeat('user_id', feature_max_idx['user_id'], embedding_dim),
SparseFeat("gender", feature_max_idx['gender'], embedding_dim),
Expand Down

0 comments on commit 832474d

Please sign in to comment.