Skip to content

Commit 5be225c

Browse files
author
Weichen Shen
committed
Add model test files
1 parent fc16f4e commit 5be225c

18 files changed

+520
-5
lines changed

.travis.yml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
sudo: required
2+
dist: trusty
3+
language: python
4+
5+
python:
6+
#- "3.4"
7+
#- "3.5"
8+
- "3.6"
9+
10+
env:
11+
- TF_VERSION=1.4.0
12+
- TF_VERSION=1.5.0
13+
- TF_VERSION=1.5.1
14+
- TF_VERSION=1.6.0
15+
#- TF_VERSION=1.7.0
16+
17+
matrix:
18+
allow_failures:
19+
- python: "3.4"
20+
- python: "3.5"
21+
- env: TF_VERSION=1.7.0
22+
- env: TF_VERSION=1.5.0
23+
fast_finish: true
24+
25+
26+
cache: pip
27+
# command to install dependencies
28+
install:
29+
- pip install -q pytest-cov==2.4.0
30+
#>=2.4.0,<2.6
31+
- pip install -q python-coveralls
32+
- pip install -q h5py
33+
- pip install -q tensorflow==$TF_VERSION
34+
- pip install -e .
35+
# command to run tests
36+
script:
37+
- pytest --cov=deepctr
38+
39+
notifications:
40+
recipients:
41+
42+
43+
on_success: change
44+
on_failure: always
45+
46+
after_success:
47+
coveralls

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# DeepCTR
22
![dep1](https://img.shields.io/badge/Tensorflow-1.4/1.5/1.6-blue.svg
33
)
4-
[![Python Versions](https://img.shields.io/pypi/pyversions/deepctr.svg)](https://pypi.org/project/deepctr)
5-
[![Documentation Status](https://readthedocs.org/projects/deepctr-doc/badge/?version=latest)](https://deepctr-doc.readthedocs.io/)
4+
[![Python Versions](https://img.shields.io/badge/python-3.6-blue.svg)](https://pypi.org/project/deepctr)
5+
[![PyPI Version](https://img.shields.io/pypi/v/deepctr.svg)](https://pypi.org/project/deepctr)
66
[![GitHub Issues](https://img.shields.io/github/issues/shenweichen/deepctr.svg
77
)](https://github.com/shenweichen/deepctr/issues)
88
[![License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://github.com/shenweichen/deepctr/blob/master/LICENSE)
9-
[![PyPI Version](https://img.shields.io/pypi/v/deepctr.svg)](https://pypi.org/project/deepctr)
9+
10+
[![Documentation Status](https://readthedocs.org/projects/deepctr-doc/badge/?version=latest)](https://deepctr-doc.readthedocs.io/)
11+
[![Build Status](https://travis-ci.com/shenweichen/DeepCTR.svg?branch=master)](https://travis-ci.com/shenweichen/DeepCTR)
12+
[![Coverage Status](https://coveralls.io/repos/github/shenweichen/DeepCTR/badge.svg)](https://coveralls.io/github/shenweichen/DeepCTR)
13+
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/c5e1deb657a94e40aa1db080cb0df91e)](https://www.codacy.com/app/wcshen1994/DeepCTR?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=shenweichen/DeepCTR&amp;utm_campaign=Badge_Grade)
1014

1115
DeepCTR is a **Easy-to-use**,**Modular** and **Extendible** package of deep-learning based CTR models along with lots of core components layer which can be used to build your own custom model easily.You can use any complex model with `model.fit()`and`model.predict()` just like any other keras model.And the layers are compatible with tensorflow.Through `pip install deepctr` get the package and [**Get Started!**](https://deepctr-doc.readthedocs.io/en/latest/Quick-Start.html)
1216

docs/source/Demo.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ In this demo,we simply normailize the integer feature between 0 and 1,you
1616
can try other transformation technique like log normalization or discretization.
1717

1818
This example shows how to use *DeepFM* to solve a simple binary classification task. You can get the demo data
19-
`criteo_sample.txt <https://github.com/shenweichen/DeepCTR/tree/master/demo
19+
`criteo_sample.txt <https://github.com/shenweichen/DeepCTR/tree/master/examples
2020
/criteo_sample.txt>`_ and run the following codes.
2121

2222
.. code-block:: python
@@ -73,7 +73,7 @@ Here is a small fraction of data include only sparse field.
7373
:scale: 70 %
7474

7575
This example shows how to use *DeepFM* to solve a simple binary regression task. You can get the demo data
76-
`movielens_sample.txt <https://github.com/shenweichen/DeepCTR/tree/master/demo
76+
`movielens_sample.txt <https://github.com/shenweichen/DeepCTR/tree/master/examples
7777
/movielens_sample.txt>`_ and run the following codes.
7878

7979
.. code-block:: python
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

setup.cfg

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[metadata]
2+
desciption-file = README.md
3+
4+
#[coverage:run]
5+
#branch = True
6+
7+
[coverage:report]
8+
exclude_lines =
9+
# Have to re-enable the standard pragma
10+
pragma: no cover
11+
# Don't complain about missing debug-only code:
12+
def __repr__
13+
if self\.debug
14+
15+
# Don't complain if tests don't hit defensive assertion code:
16+
raise ValueError
17+
raise AssertionError
18+
raise NotImplementedError
19+
20+
# Don't complain if non-runnable code isn't run:
21+
if 0:
22+
if False:
23+
if __name__ == .__main__.:

tests/AFM_test.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import numpy as np
2+
from tensorflow.python.keras.models import save_model, load_model
3+
from deepctr.models import AFM
4+
from deepctr.utils import custom_objects
5+
6+
7+
def test_AFM():
8+
name = "AFM"
9+
10+
sample_size = 64
11+
feature_dim_dict = {'sparse': {'sparse_1': 2, 'sparse_2': 5,
12+
'sparse_3': 10}, 'dense': ['dense_1', 'dense_2', 'dense_3']}
13+
sparse_input = [np.random.randint(0, dim, sample_size)
14+
for dim in feature_dim_dict['sparse'].values()]
15+
dense_input = [np.random.random(sample_size)
16+
for name in feature_dim_dict['dense']]
17+
y = np.random.randint(0, 2, sample_size)
18+
x = sparse_input + dense_input
19+
20+
model = AFM(feature_dim_dict, use_attention=True, keep_prob=0.5,)
21+
model.compile('adam', 'binary_crossentropy',
22+
metrics=['binary_crossentropy'])
23+
model.fit(x, y, batch_size=100, epochs=1, validation_split=0.5)
24+
print(name+" test train valid pass!")
25+
model.save_weights(name + '_weights.h5')
26+
model.load_weights(name + '_weights.h5')
27+
print(name+" test save load weight pass!")
28+
save_model(model, name + '.h5')
29+
model = load_model(name + '.h5', custom_objects)
30+
print(name + " test save load model pass!")
31+
32+
print(name + " test pass!")
33+
34+
35+
if __name__ == "__main__":
36+
test_AFM()

tests/DCN_test.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import numpy as np
2+
from deepctr.models import DCN
3+
from deepctr.utils import custom_objects
4+
from tensorflow.python.keras.models import save_model, load_model
5+
6+
7+
def test_DCN():
8+
name = "DCN"
9+
10+
sample_size = 64
11+
feature_dim_dict = {'sparse': {'sparse_1': 2, 'sparse_2': 5,
12+
'sparse_3': 10}, 'dense': ['dense_1', 'dense_2', 'dense_3']}
13+
sparse_input = [np.random.randint(0, dim, sample_size)
14+
for dim in feature_dim_dict['sparse'].values()]
15+
dense_input = [np.random.random(sample_size)
16+
for name in feature_dim_dict['dense']]
17+
y = np.random.randint(0, 2, sample_size)
18+
x = sparse_input + dense_input
19+
20+
model = DCN(feature_dim_dict, embedding_size=8,
21+
hidden_size=[32, 32], keep_prob=0.5, )
22+
model.compile('adam', 'binary_crossentropy',
23+
metrics=['binary_crossentropy'])
24+
model.fit(x, y, batch_size=100, epochs=1, validation_split=0.5)
25+
print(name+" test train valid pass!")
26+
model.save_weights(name + '_weights.h5')
27+
model.load_weights(name + '_weights.h5')
28+
print(name+" test save load weight pass!")
29+
save_model(model, name + '.h5')
30+
model = load_model(name + '.h5', custom_objects)
31+
print(name + " test save load model pass!")
32+
33+
print(name + " test pass!")
34+
35+
36+
if __name__ == "__main__":
37+
test_DCN()

tests/DIN_test.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import numpy as np
2+
from deepctr.models import DIN
3+
from deepctr.activations import Dice
4+
from deepctr.utils import custom_objects
5+
6+
from tensorflow.python.keras.layers import PReLU
7+
from tensorflow.python.keras.models import load_model, save_model
8+
9+
10+
def get_xy_fd():
11+
feature_dim_dict = {"sparse": {'user': 4, 'gender': 2,
12+
'item': 4, 'item_gender': 2}, "dense": []}
13+
behavior_feature_list = ["item"]
14+
uid = np.array([1, 2, 3])
15+
ugender = np.array([0, 1, 0])
16+
iid = np.array([0, 1, 2])
17+
igender = np.array([0, 1, 0])
18+
19+
hist_iid = np.array([[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]])
20+
hist_igender = np.array([[0, 1, 0, 1], [0, 1, 1, 1], [0, 0, 1, 0]])
21+
hist_length = np.array([4, 4, 4])
22+
23+
feature_dict = {'user': uid, 'gender': ugender, 'item': iid, 'item_gender': igender,
24+
'hist_item': hist_iid, 'hist_item_gender': hist_igender, }
25+
x = [feature_dict[feat] for feat in feature_dim_dict["sparse"]] \
26+
+ [feature_dict['hist_'+feat] for feat in behavior_feature_list]\
27+
+ [hist_length]
28+
y = [1, 0, 1]
29+
return x, y, feature_dim_dict, behavior_feature_list
30+
31+
32+
def test_DIN_att():
33+
34+
name = "DIN_att"
35+
x, y, feature_dim_dict, behavior_feature_list = get_xy_fd()
36+
37+
model = DIN(feature_dim_dict, behavior_feature_list, hist_len_max=4, embedding_size=8,
38+
use_din=True, hidden_size=[4, 4, 4], keep_prob=0.6,)
39+
40+
model.compile('adam', 'binary_crossentropy',
41+
metrics=['binary_crossentropy'])
42+
model.fit(x, y, verbose=1, validation_split=0.5)
43+
print(name+" test train valid pass!")
44+
model.save_weights(name + '_weights.h5')
45+
model.load_weights(name + '_weights.h5')
46+
print(name+" test save load weight pass!")
47+
48+
try:
49+
save_model(model, name + '.h5')
50+
model = load_model(name + '.h5', custom_objects)
51+
print(name + " test save load model pass!")
52+
except:
53+
print("【Error】There is a bug when save model use Dice or Prelu---------------------------------------------------")
54+
55+
print(name + " test pass!")
56+
57+
58+
def test_DIN_sum():
59+
60+
name = "DIN_sum"
61+
x, y, feature_dim_dict, behavior_feature_list = get_xy_fd()
62+
63+
model = DIN(feature_dim_dict, behavior_feature_list, hist_len_max=4, embedding_size=8,
64+
use_din=False, hidden_size=[4, 4, 4], keep_prob=0.6, activation="sigmoid")
65+
66+
model.compile('adam', 'binary_crossentropy',
67+
metrics=['binary_crossentropy'])
68+
model.fit(x, y, verbose=1, validation_split=0.5)
69+
print(name+" test train valid pass!")
70+
model.save_weights(name + '_weights.h5')
71+
model.load_weights(name + '_weights.h5')
72+
print(name+" test save load weight pass!")
73+
74+
try:
75+
save_model(model, name + '.h5')
76+
model = load_model(name + '.h5', custom_objects)
77+
print(name + " test save load model pass!")
78+
except:
79+
print("There is a bug when save model use Dice or Prelu")
80+
81+
print(name + " test pass!")
82+
83+
84+
if __name__ == "__main__":
85+
test_DIN_att()
86+
test_DIN_sum()

tests/DeepFM_test.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import numpy as np
2+
from deepctr.models import DeepFM
3+
from deepctr.utils import custom_objects
4+
from tensorflow.python.keras.models import save_model, load_model
5+
6+
7+
def test_DeepFM():
8+
name = "DeepFM"
9+
sample_size = 64
10+
feature_dim_dict = {'sparse': {'sparse_1': 2, 'sparse_2': 5,
11+
'sparse_3': 10}, 'dense': ['dense_1', 'dense_2', 'dense_3']}
12+
sparse_input = [np.random.randint(0, dim, sample_size)
13+
for dim in feature_dim_dict['sparse'].values()]
14+
dense_input = [np.random.random(sample_size)
15+
for name in feature_dim_dict['dense']]
16+
y = np.random.randint(0, 2, sample_size)
17+
x = sparse_input + dense_input
18+
19+
model = DeepFM(feature_dim_dict, use_fm=True,
20+
hidden_size=[32, 32], keep_prob=0.5, )
21+
model.compile('adam', 'binary_crossentropy',
22+
metrics=['binary_crossentropy'])
23+
model.fit(x, y, batch_size=100, epochs=1, validation_split=0.5)
24+
print(name+" test train valid pass!")
25+
model.save_weights(name + '_weights.h5')
26+
model.load_weights(name + '_weights.h5')
27+
print(name+" test save load weight pass!")
28+
save_model(model, name + '.h5')
29+
model = load_model(name + '.h5', custom_objects)
30+
print(name + " test save load model pass!")
31+
32+
print(name + " test pass!")
33+
34+
35+
if __name__ == "__main__":
36+
test_DeepFM()

tests/FNN_test.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import numpy as np
2+
from deepctr.models import FNN
3+
from deepctr.utils import custom_objects
4+
from tensorflow.python.keras.models import save_model, load_model
5+
6+
7+
def test_FNN():
8+
name = "FNN"
9+
10+
sample_size = 64
11+
feature_dim_dict = {'sparse': {'sparse_1': 2, 'sparse_2': 5,
12+
'sparse_3': 10}, 'dense': ['dense_1', 'dense_2', 'dense_3']}
13+
sparse_input = [np.random.randint(0, dim, sample_size)
14+
for dim in feature_dim_dict['sparse'].values()]
15+
dense_input = [np.random.random(sample_size)
16+
for name in feature_dim_dict['dense']]
17+
y = np.random.randint(0, 2, sample_size)
18+
x = sparse_input + dense_input
19+
20+
model = FNN(feature_dim_dict, hidden_size=[32, 32], keep_prob=0.5, )
21+
model.compile('adam', 'binary_crossentropy',
22+
metrics=['binary_crossentropy'])
23+
model.fit(x, y, batch_size=100, epochs=1, validation_split=0.5)
24+
print(name+" test train valid pass!")
25+
model.save_weights(name + '_weights.h5')
26+
model.load_weights(name + '_weights.h5')
27+
print(name+" test save load weight pass!")
28+
save_model(model, name + '.h5')
29+
model = load_model(name + '.h5', custom_objects)
30+
print(name + " test save load model pass!")
31+
32+
print(name + " test pass!")
33+
34+
35+
if __name__ == "__main__":
36+
test_FNN()

tests/MLR_test.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import numpy as np
2+
from deepctr.models import MLR
3+
from deepctr.utils import custom_objects
4+
from tensorflow.python.keras.models import save_model, load_model
5+
6+
7+
def test_MLR():
8+
name = "MLR"
9+
10+
sample_size = 64
11+
feature_dim_dict = {'sparse': {'sparse_1': 2, 'sparse_2': 5,
12+
'sparse_3': 10}, 'dense': ['dense_1', 'dense_2', 'dense_3']}
13+
sparse_input = [np.random.randint(0, dim, sample_size)
14+
for dim in feature_dim_dict['sparse'].values()]
15+
dense_input = [np.random.random(sample_size)
16+
for name in feature_dim_dict['dense']]
17+
y = np.random.randint(0, 2, sample_size)
18+
x = sparse_input + dense_input
19+
20+
model = MLR(feature_dim_dict)
21+
model.compile('adam', 'binary_crossentropy',
22+
metrics=['binary_crossentropy'])
23+
model.fit(x, y, batch_size=100, epochs=1, validation_split=0.5)
24+
print(name+" test train valid pass!")
25+
model.save_weights(name + '_weights.h5')
26+
model.load_weights(name + '_weights.h5')
27+
print(name+" test save load weight pass!")
28+
save_model(model, name + '.h5')
29+
model = load_model(name + '.h5', custom_objects)
30+
print(name + " test save load model pass!")
31+
32+
print(name + " test pass!")
33+
34+
35+
if __name__ == "__main__":
36+
test_MLR()

0 commit comments

Comments
 (0)