Showing preview only (925K chars total). Download the full file or copy to clipboard to get everything.
Repository: shenweichen/DeepCTR
Branch: master
Commit: e8f4d818f9b4
Files: 220
Total size: 867.4 KB
Directory structure:
gitextract_0q19foz8/
├── .gitattributes
├── .github/
│ ├── ISSUE_TEMPLATE/
│ │ ├── bug_report.md
│ │ ├── feature_request.md
│ │ └── question.md
│ └── workflows/
│ ├── ci.yml
│ └── ci2.yml
├── .gitignore
├── .readthedocs.yml
├── .travis.yml
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── deepctr/
│ ├── __init__.py
│ ├── contrib/
│ │ ├── __init__.py
│ │ ├── rnn.py
│ │ ├── rnn_v2.py
│ │ └── utils.py
│ ├── estimator/
│ │ ├── __init__.py
│ │ ├── feature_column.py
│ │ ├── inputs.py
│ │ ├── models/
│ │ │ ├── __init__.py
│ │ │ ├── afm.py
│ │ │ ├── autoint.py
│ │ │ ├── ccpm.py
│ │ │ ├── dcn.py
│ │ │ ├── deepfefm.py
│ │ │ ├── deepfm.py
│ │ │ ├── fibinet.py
│ │ │ ├── fnn.py
│ │ │ ├── fwfm.py
│ │ │ ├── nfm.py
│ │ │ ├── pnn.py
│ │ │ ├── wdl.py
│ │ │ └── xdeepfm.py
│ │ └── utils.py
│ ├── feature_column.py
│ ├── inputs.py
│ ├── layers/
│ │ ├── __init__.py
│ │ ├── activation.py
│ │ ├── core.py
│ │ ├── interaction.py
│ │ ├── normalization.py
│ │ ├── sequence.py
│ │ └── utils.py
│ ├── models/
│ │ ├── __init__.py
│ │ ├── afm.py
│ │ ├── autoint.py
│ │ ├── ccpm.py
│ │ ├── dcn.py
│ │ ├── dcnmix.py
│ │ ├── deepfefm.py
│ │ ├── deepfm.py
│ │ ├── difm.py
│ │ ├── edcn.py
│ │ ├── fgcnn.py
│ │ ├── fibinet.py
│ │ ├── flen.py
│ │ ├── fnn.py
│ │ ├── fwfm.py
│ │ ├── ifm.py
│ │ ├── mlr.py
│ │ ├── multitask/
│ │ │ ├── __init__.py
│ │ │ ├── esmm.py
│ │ │ ├── mmoe.py
│ │ │ ├── ple.py
│ │ │ └── sharedbottom.py
│ │ ├── nfm.py
│ │ ├── onn.py
│ │ ├── pnn.py
│ │ ├── sequence/
│ │ │ ├── __init__.py
│ │ │ ├── bst.py
│ │ │ ├── dien.py
│ │ │ ├── din.py
│ │ │ └── dsin.py
│ │ ├── wdl.py
│ │ └── xdeepfm.py
│ └── utils.py
├── docs/
│ ├── Makefile
│ ├── make.bat
│ ├── requirements.readthedocs.txt
│ └── source/
│ ├── Estimators.rst
│ ├── Examples.md
│ ├── FAQ.md
│ ├── Features.md
│ ├── History.md
│ ├── Layers.rst
│ ├── Model_Methods.md
│ ├── Models.rst
│ ├── Quick-Start.md
│ ├── conf.py
│ ├── deepctr.contrib.rnn.rst
│ ├── deepctr.contrib.rst
│ ├── deepctr.contrib.utils.rst
│ ├── deepctr.estimator.feature_column.rst
│ ├── deepctr.estimator.inputs.rst
│ ├── deepctr.estimator.models.afm.rst
│ ├── deepctr.estimator.models.autoint.rst
│ ├── deepctr.estimator.models.ccpm.rst
│ ├── deepctr.estimator.models.dcn.rst
│ ├── deepctr.estimator.models.deepfefm.rst
│ ├── deepctr.estimator.models.deepfm.rst
│ ├── deepctr.estimator.models.fibinet.rst
│ ├── deepctr.estimator.models.fnn.rst
│ ├── deepctr.estimator.models.fwfm.rst
│ ├── deepctr.estimator.models.nfm.rst
│ ├── deepctr.estimator.models.pnn.rst
│ ├── deepctr.estimator.models.rst
│ ├── deepctr.estimator.models.wdl.rst
│ ├── deepctr.estimator.models.xdeepfm.rst
│ ├── deepctr.estimator.rst
│ ├── deepctr.estimator.utils.rst
│ ├── deepctr.feature_column.rst
│ ├── deepctr.inputs.rst
│ ├── deepctr.layers.activation.rst
│ ├── deepctr.layers.core.rst
│ ├── deepctr.layers.interaction.rst
│ ├── deepctr.layers.normalization.rst
│ ├── deepctr.layers.rst
│ ├── deepctr.layers.sequence.rst
│ ├── deepctr.layers.utils.rst
│ ├── deepctr.models.afm.rst
│ ├── deepctr.models.autoint.rst
│ ├── deepctr.models.ccpm.rst
│ ├── deepctr.models.dcn.rst
│ ├── deepctr.models.dcnmix.rst
│ ├── deepctr.models.deepfefm.rst
│ ├── deepctr.models.deepfm.rst
│ ├── deepctr.models.deepfwfm.rst
│ ├── deepctr.models.difm.rst
│ ├── deepctr.models.edcn.rst
│ ├── deepctr.models.fgcnn.rst
│ ├── deepctr.models.fibinet.rst
│ ├── deepctr.models.flen.rst
│ ├── deepctr.models.fnn.rst
│ ├── deepctr.models.ifm.rst
│ ├── deepctr.models.mlr.rst
│ ├── deepctr.models.multitask.esmm.rst
│ ├── deepctr.models.multitask.mmoe.rst
│ ├── deepctr.models.multitask.ple.rst
│ ├── deepctr.models.multitask.sharedbottom.rst
│ ├── deepctr.models.nfm.rst
│ ├── deepctr.models.onn.rst
│ ├── deepctr.models.pnn.rst
│ ├── deepctr.models.rst
│ ├── deepctr.models.sequence.bst.rst
│ ├── deepctr.models.sequence.dien.rst
│ ├── deepctr.models.sequence.din.rst
│ ├── deepctr.models.sequence.dsin.rst
│ ├── deepctr.models.wdl.rst
│ ├── deepctr.models.xdeepfm.rst
│ ├── deepctr.rst
│ ├── deepctr.utils.rst
│ ├── index.rst
│ └── modules.rst
├── examples/
│ ├── avazu_sample.txt
│ ├── census-income.sample
│ ├── criteo_sample.te.tfrecords
│ ├── criteo_sample.tr.tfrecords
│ ├── criteo_sample.txt
│ ├── gen_tfrecords.py
│ ├── movielens_age_vocabulary.csv
│ ├── movielens_sample.txt
│ ├── run_all.sh
│ ├── run_classification_criteo.py
│ ├── run_classification_criteo_hash.py
│ ├── run_classification_criteo_multi_gpu.py
│ ├── run_dien.py
│ ├── run_din.py
│ ├── run_dsin.py
│ ├── run_estimator_pandas_classification.py
│ ├── run_estimator_tfrecord_classification.py
│ ├── run_flen.py
│ ├── run_mtl.py
│ ├── run_multivalue_movielens.py
│ ├── run_multivalue_movielens_hash.py
│ ├── run_multivalue_movielens_vocab_hash.py
│ └── run_regression_movielens.py
├── setup.cfg
├── setup.py
└── tests/
├── README.md
├── __init__.py
├── feature_test.py
├── layers/
│ ├── __init__.py
│ ├── activations_test.py
│ ├── core_test.py
│ ├── interaction_test.py
│ ├── normalization_test.py
│ ├── sequence_test.py
│ ├── utils_test.py
│ └── vocabulary_example.csv
├── models/
│ ├── AFM_test.py
│ ├── AutoInt_test.py
│ ├── BST_test.py
│ ├── CCPM_test.py
│ ├── DCNMix_test.py
│ ├── DCN_test.py
│ ├── DIEN_test.py
│ ├── DIFM_test.py
│ ├── DIN_test.py
│ ├── DSIN_test.py
│ ├── DeepFEFM_test.py
│ ├── DeepFM_test.py
│ ├── EDCN_test.py
│ ├── FGCNN_test.py
│ ├── FLEN_test.py
│ ├── FNN_test.py
│ ├── FiBiNET_test.py
│ ├── FwFM_test.py
│ ├── IFM_test.py
│ ├── MLR_test.py
│ ├── MTL_test.py
│ ├── NFM_test.py
│ ├── ONN_test.py
│ ├── PNN_test.py
│ ├── WDL_test.py
│ ├── __init__.py
│ └── xDeepFM_test.py
├── utils.py
├── utils_mtl.py
└── utils_test.py
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitattributes
================================================
# Auto detect text files and perform LF normalization
* text=auto
# Custom for Visual Studio
*.cs diff=csharp
# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
================================================
FILE: .github/ISSUE_TEMPLATE/bug_report.md
================================================
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''
---
**Describe the bug(问题描述)**
A clear and concise description of what the bug is.Better with standalone code to reproduce the issue.
**To Reproduce(复现步骤)**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error
**Operating environment(运行环境):**
- python version [e.g. 3.6, 3.7]
- tensorflow version [e.g. 1.4.0, 1.15.0, 2.10.0]
- deepctr version [e.g. 0.9.2,]
**Additional context**
Add any other context about the problem here.
================================================
FILE: .github/ISSUE_TEMPLATE/feature_request.md
================================================
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: enhancement&feature request
assignees: ''
---
**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
**Describe the solution you'd like**
A clear and concise description of what you want to happen.
**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.
**Additional context**
Add any other context or screenshots about the feature request here.
================================================
FILE: .github/ISSUE_TEMPLATE/question.md
================================================
---
name: Question
about: Ask any question ~
title: ''
labels: question
assignees: ''
---
Please refer to the [FAQ](https://deepctr-doc.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.
**Describe the question(问题描述)**
A clear and concise description of what the question is.
**Additional context**
Add any other context about the problem here.
**Operating environment(运行环境):**
- python version [e.g. 3.6]
- tensorflow version [e.g. 1.4.0, 1.15.0, 2.10.0]
- deepctr version [e.g. 0.9.2,]
================================================
FILE: .github/workflows/ci.yml
================================================
name: CI_TF2
on:
push:
path:
- 'deepctr/*'
- 'tests/*'
pull_request:
path:
- 'deepctr/*'
- 'tests/*'
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 180
strategy:
matrix:
python-version: [ 3.6,3.7,3.8, 3.9,3.10.7 ]
tf-version: [ 2.6.0,2.7.0,2.8.0,2.9.0,2.10.0 ]
exclude:
- python-version: 3.7
tf-version: 1.4.0
- python-version: 3.7
tf-version: 1.15.0
- python-version: 3.8
tf-version: 1.4.0
- python-version: 3.8
tf-version: 1.14.0
- python-version: 3.8
tf-version: 1.15.0
- python-version: 3.6
tf-version: 2.7.0
- python-version: 3.6
tf-version: 2.8.0
- python-version: 3.6
tf-version: 2.9.0
- python-version: 3.6
tf-version: 2.10.0
- python-version: 3.9
tf-version: 1.4.0
- python-version: 3.9
tf-version: 1.15.0
- python-version: 3.9
tf-version: 2.2.0
- python-version: 3.9
tf-version: 2.5.0
- python-version: 3.9
tf-version: 2.6.0
- python-version: 3.9
tf-version: 2.7.0
- python-version: 3.10.7
tf-version: 1.4.0
- python-version: 3.10.7
tf-version: 1.15.0
- python-version: 3.10.7
tf-version: 2.2.0
- python-version: 3.10.7
tf-version: 2.5.0
- python-version: 3.10.7
tf-version: 2.6.0
- python-version: 3.10.7
tf-version: 2.7.0
steps:
- uses: actions/checkout@v3
- name: Setup python environment
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip3 install -q tensorflow==${{ matrix.tf-version }}
pip install -q protobuf==3.19.0
pip install -q requests
pip install -e .
- name: Test with pytest
timeout-minutes: 180
run: |
pip install -q pytest
pip install -q pytest-cov
pip install -q python-coveralls
pytest --cov=deepctr --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3.1.0
with:
token: ${{secrets.CODECOV_TOKEN}}
file: ./coverage.xml
flags: pytest
name: py${{ matrix.python-version }}-tf${{ matrix.tf-version }}
================================================
FILE: .github/workflows/ci2.yml
================================================
name: CI_TF1
on:
push:
path:
- 'deepctr/*'
- 'tests/*'
pull_request:
path:
- 'deepctr/*'
- 'tests/*'
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 360
strategy:
matrix:
python-version: [ 3.6,3.7 ]
tf-version: [ 1.15.0 ]
exclude:
- python-version: 3.7
tf-version: 1.4.0
- python-version: 3.7
tf-version: 1.12.0
- python-version: 3.7
tf-version: 1.15.0
- python-version: 3.8
tf-version: 1.4.0
- python-version: 3.8
tf-version: 1.14.0
- python-version: 3.8
tf-version: 1.15.0
- python-version: 3.6
tf-version: 2.7.0
- python-version: 3.6
tf-version: 2.8.0
- python-version: 3.6
tf-version: 2.9.0
- python-version: 3.6
tf-version: 2.10.0
- python-version: 3.9
tf-version: 1.4.0
- python-version: 3.9
tf-version: 1.15.0
- python-version: 3.9
tf-version: 2.2.0
- python-version: 3.9
tf-version: 2.5.0
- python-version: 3.9
tf-version: 2.6.0
- python-version: 3.9
tf-version: 2.7.0
- python-version: 3.10.7
tf-version: 1.4.0
- python-version: 3.10.7
tf-version: 1.15.0
- python-version: 3.10.7
tf-version: 2.2.0
- python-version: 3.10.7
tf-version: 2.5.0
- python-version: 3.10.7
tf-version: 2.6.0
- python-version: 3.10.7
tf-version: 2.7.0
steps:
- uses: actions/checkout@v3
- name: Setup python environment
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip3 install -q tensorflow==${{ matrix.tf-version }}
pip install -q protobuf==3.19.0
pip install -q requests
pip install -e .
- name: Test with pytest
timeout-minutes: 360
run: |
pip install -q pytest
pip install -q pytest-cov
pip install -q python-coveralls
pytest --cov=deepctr --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3.1.0
with:
token: ${{secrets.CODECOV_TOKEN}}
file: ./coverage.xml
flags: pytest
name: py${{ matrix.python-version }}-tf${{ matrix.tf-version }}
================================================
FILE: .gitignore
================================================
*.h5
*.ipynb
.pytest_cache/
.vscode/
tests/unused/*
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
.idea/
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
# Flask instance folder
instance/
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# IPython Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# dotenv
.env
# virtualenv
venv/
ENV/
# Spyder project settings
.spyderproject
# Rope project settings
.ropeproject
# =========================
# Operating System Files
# =========================
# OSX
# =========================
.DS_Store
.AppleDouble
.LSOverride
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# Windows
# =========================
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msm
*.msp
# Windows shortcuts
*.lnk
================================================
FILE: .readthedocs.yml
================================================
build:
image: latest
python:
version: 3.6
================================================
FILE: .travis.yml
================================================
#sudo: required
#dist: trusty xenial
language: python
python:
- "2.7" #time out
#- "3.4"
- "3.5"
- "3.6"
#- "3.7"
env:
# - TF_VERSION=1.13.1
# - TF_VERSION=1.12.2
- TF_VERSION=1.4.0
#Not Support- TF_VERSION=1.7.0
#Not Support- TF_VERSION=1.7.1
#Not Support- TF_VERSION=1.8.0
#- TF_VERSION=1.8.0
# - TF_VERSION=1.11.0
#- TF_VERSION=1.6.0
- TF_VERSION=2.0.0b1
#- TF_VERSION=1.13.2
- TF_VERSION=1.14.0
matrix:
allow_failures:
- python: "2.7"
env: TF_VERSION=1.6.0 # to speed up
- python: "2.7"
env: TF_VERSION=2.0.0b1
- python: "3.4"
- python: "3.5"
- python: "3.7"
- env: TF_VERSION=1.5.0 #local is ok,but sometimes CI is failed
- env: TF_VERSION=1.7.0
- env: TF_VERSION=1.7.1
- env: TF_VERSION=1.8.0
- env: TF_VERSION=1.12.0 # too slow
- env: TF_VERSION=1.13.1 # too slow
- env: TF_VERSION=1.13.2 # too slow
- env: TF_VERSION=1.14.0 # too slow
fast_finish: true
cache: pip
# command to install dependencies
install:
- pip install -q pytest-cov==2.4.0 #>=2.4.0,<2.6
- pip install -q python-coveralls
- pip install -q codacy-coverage
- pip install -q tensorflow==$TF_VERSION
- pip install -q pandas
- pip install -q packaging
- pip install -e .
# command to run tests
script:
- pytest --cov=deepctr
notifications:
recipients:
- weichenswc@163.com
on_success: change
on_failure: change
after_success:
- coveralls
- coverage xml
- python-codacy-coverage -r coverage.xml
================================================
FILE: CONTRIBUTING.md
================================================
This project is under development and we need developers to participate in.
# Join us
If you
- familiar with and interested in ctr prediction algorithms
- familiar with tensorflow
- have spare time to learn and develop
- familiar with git
please send a brief introduction of your background and experience to weichenswc@163.com, welcome to join us!
# Creating a pull request
1. **Become a collaborator**: Send an email with introduction and your github account name to weichenswc@163.com and waiting for invitation to become a collaborator.
2. **Fork&Dev**: Fork your own branch(`dev_yourname`) in `DeepCTR` from the `master` branch for development.If the `master` is updated during the development process, remember to merge and update to `dev_yourname` regularly.
3. **Testing**: Test logical correctness and effect when finishing the code development of the `dev_yourname` branch.
4. **Pre-release** : After testing contact weichenswc@163.com for pre-release integration, usually your branch `dev_yourname` will be merged into `release` branch by squash merge.
5. **Release a new version**: After confirming that the change is no longer needed, `release` branch will be merged into `master` and a new python package will be released on pypi.
# Discussions
https://github.com/shenweichen/DeepCTR/discussions
================================================
FILE: LICENSE
================================================
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2017-present Weichen Shen
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
================================================
FILE: README.md
================================================
# DeepCTR
[](https://pypi.org/project/deepctr)
[](https://pypi.org/project/deepctr)
[](https://pepy.tech/project/deepctr)
[](https://pypi.org/project/deepctr)
[](https://github.com/shenweichen/deepctr/issues)
<!-- [](https://github.com/shenweichen/DeepCTR/commits/master) -->
[](https://deepctr-doc.readthedocs.io/)

[](https://codecov.io/gh/shenweichen/DeepCTR)
[](https://www.codacy.com/gh/shenweichen/DeepCTR?utm_source=github.com&utm_medium=referral&utm_content=shenweichen/DeepCTR&utm_campaign=Badge_Grade)
[](./README.md#DisscussionGroup)
[](https://github.com/shenweichen/deepctr/blob/master/LICENSE)
<!-- [](https://gitter.im/DeepCTR/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) -->
DeepCTR is a **Easy-to-use**, **Modular** and **Extendible** package of deep-learning based CTR models along with lots of
core components layers which can be used to easily build custom models.You can use any complex model with `model.fit()`
,and `model.predict()` .
- Provide `tf.keras.Model` like interfaces for **quick experiment**. [example](https://deepctr-doc.readthedocs.io/en/latest/Quick-Start.html#getting-started-4-steps-to-deepctr)
- Provide `tensorflow estimator` interface for **large scale data** and **distributed training**. [example](https://deepctr-doc.readthedocs.io/en/latest/Quick-Start.html#getting-started-4-steps-to-deepctr-estimator-with-tfrecord)
- It is compatible with both `tf 1.x` and `tf 2.x`.
Some related projects:
- DeepMatch: https://github.com/shenweichen/DeepMatch
- DeepCTR-Torch: https://github.com/shenweichen/DeepCTR-Torch
Let's [**Get Started!**](https://deepctr-doc.readthedocs.io/en/latest/Quick-Start.html)([Chinese
Introduction](https://zhuanlan.zhihu.com/p/53231955)) and [welcome to join us!](./CONTRIBUTING.md)
## Models List
| Model | Paper |
| :------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Convolutional Click Prediction Model | [CIKM 2015][A Convolutional Click Prediction Model](http://ir.ia.ac.cn/bitstream/173211/12337/1/A%20Convolutional%20Click%20Prediction%20Model.pdf) |
| Factorization-supported Neural Network | [ECIR 2016][Deep Learning over Multi-field Categorical Data: A Case Study on User Response Prediction](https://arxiv.org/pdf/1601.02376.pdf) |
| Product-based Neural Network | [ICDM 2016][Product-based neural networks for user response prediction](https://arxiv.org/pdf/1611.00144.pdf) |
| Wide & Deep | [DLRS 2016][Wide & Deep Learning for Recommender Systems](https://arxiv.org/pdf/1606.07792.pdf) |
| DeepFM | [IJCAI 2017][DeepFM: A Factorization-Machine based Neural Network for CTR Prediction](http://www.ijcai.org/proceedings/2017/0239.pdf) |
| Piece-wise Linear Model | [arxiv 2017][Learning Piece-wise Linear Models from Large Scale Data for Ad Click Prediction](https://arxiv.org/abs/1704.05194) |
| Deep & Cross Network | [ADKDD 2017][Deep & Cross Network for Ad Click Predictions](https://arxiv.org/abs/1708.05123) |
| Attentional Factorization Machine | [IJCAI 2017][Attentional Factorization Machines: Learning the Weight of Feature Interactions via Attention Networks](http://www.ijcai.org/proceedings/2017/435) |
| Neural Factorization Machine | [SIGIR 2017][Neural Factorization Machines for Sparse Predictive Analytics](https://arxiv.org/pdf/1708.05027.pdf) |
| xDeepFM | [KDD 2018][xDeepFM: Combining Explicit and Implicit Feature Interactions for Recommender Systems](https://arxiv.org/pdf/1803.05170.pdf) |
| Deep Interest Network | [KDD 2018][Deep Interest Network for Click-Through Rate Prediction](https://arxiv.org/pdf/1706.06978.pdf) |
| AutoInt | [CIKM 2019][AutoInt: Automatic Feature Interaction Learning via Self-Attentive Neural Networks](https://arxiv.org/abs/1810.11921) |
| Deep Interest Evolution Network | [AAAI 2019][Deep Interest Evolution Network for Click-Through Rate Prediction](https://arxiv.org/pdf/1809.03672.pdf) |
| FwFM | [WWW 2018][Field-weighted Factorization Machines for Click-Through Rate Prediction in Display Advertising](https://arxiv.org/pdf/1806.03514.pdf) |
| ONN | [arxiv 2019][Operation-aware Neural Networks for User Response Prediction](https://arxiv.org/pdf/1904.12579.pdf) |
| FGCNN | [WWW 2019][Feature Generation by Convolutional Neural Network for Click-Through Rate Prediction ](https://arxiv.org/pdf/1904.04447) |
| Deep Session Interest Network | [IJCAI 2019][Deep Session Interest Network for Click-Through Rate Prediction ](https://arxiv.org/abs/1905.06482) |
| FiBiNET | [RecSys 2019][FiBiNET: Combining Feature Importance and Bilinear feature Interaction for Click-Through Rate Prediction](https://arxiv.org/pdf/1905.09433.pdf) |
| FLEN | [arxiv 2019][FLEN: Leveraging Field for Scalable CTR Prediction](https://arxiv.org/pdf/1911.04690.pdf) |
| BST | [DLP-KDD 2019][Behavior sequence transformer for e-commerce recommendation in Alibaba](https://arxiv.org/pdf/1905.06874.pdf) |
| IFM | [IJCAI 2019][An Input-aware Factorization Machine for Sparse Prediction](https://www.ijcai.org/Proceedings/2019/0203.pdf) |
| DCN V2 | [arxiv 2020][DCN V2: Improved Deep & Cross Network and Practical Lessons for Web-scale Learning to Rank Systems](https://arxiv.org/abs/2008.13535) |
| DIFM | [IJCAI 2020][A Dual Input-aware Factorization Machine for CTR Prediction](https://www.ijcai.org/Proceedings/2020/0434.pdf) |
| FEFM and DeepFEFM | [arxiv 2020][Field-Embedded Factorization Machines for Click-through rate prediction](https://arxiv.org/abs/2009.09931) |
| SharedBottom | [arxiv 2017][An Overview of Multi-Task Learning in Deep Neural Networks](https://arxiv.org/pdf/1706.05098.pdf) |
| ESMM | [SIGIR 2018][Entire Space Multi-Task Model: An Effective Approach for Estimating Post-Click Conversion Rate](https://arxiv.org/abs/1804.07931) |
| MMOE | [KDD 2018][Modeling Task Relationships in Multi-task Learning with Multi-gate Mixture-of-Experts](https://dl.acm.org/doi/abs/10.1145/3219819.3220007) |
| PLE | [RecSys 2020][Progressive Layered Extraction (PLE): A Novel Multi-Task Learning (MTL) Model for Personalized Recommendations](https://dl.acm.org/doi/10.1145/3383313.3412236) |
| EDCN | [KDD 2021][Enhancing Explicit and Implicit Feature Interactions via Information Sharing for Parallel Deep CTR Models](https://dlp-kdd.github.io/assets/pdf/DLP-KDD_2021_paper_12.pdf) |
## Citation
- Weichen Shen. (2017). DeepCTR: Easy-to-use,Modular and Extendible package of deep-learning based CTR
models. https://github.com/shenweichen/deepctr.
If you find this code useful in your research, please cite it using the following BibTeX:
```bibtex
@misc{shen2017deepctr,
author = {Weichen Shen},
title = {DeepCTR: Easy-to-use,Modular and Extendible package of deep-learning based CTR models},
year = {2017},
publisher = {GitHub},
journal = {GitHub Repository},
howpublished = {\url{https://github.com/shenweichen/deepctr}},
}
```
## DisscussionGroup
- [Github Discussions](https://github.com/shenweichen/DeepCTR/discussions)
- Wechat Discussions
|公众号:浅梦学习笔记|微信:deepctrbot|学习小组 [加入](https://t.zsxq.com/026UJEuzv) [主题集合](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MjM5MzY4NzE3MA==&action=getalbum&album_id=1361647041096843265&scene=126#wechat_redirect)|
|:--:|:--:|:--:|
| [](https://github.com/shenweichen/AlgoNotes)| [](https://github.com/shenweichen/AlgoNotes)|[](https://t.zsxq.com/026UJEuzv)|
## Main contributors([welcome to join us!](./CONTRIBUTING.md))
<table border="0">
<tbody>
<tr align="center" >
<td>
<a href="https://github.com/shenweichen"><img width="70" height="70" src="https://github.com/shenweichen.png?s=40" alt="pic"></a><br>
<a href="https://github.com/shenweichen">Shen Weichen</a>
<p>
Alibaba Group </p>
</td>
<td>
<a href="https://github.com/zanshuxun"><img width="70" height="70" src="https://github.com/zanshuxun.png?s=40" alt="pic"></a><br>
<a href="https://github.com/zanshuxun">Zan Shuxun</a>
<p>Alibaba Group </p>
</td>
<td>
<a href="https://github.com/pandeconscious"><img width="70" height="70" src="https://github.com/pandeconscious.png?s=40" alt="pic"></a><br>
<a href="https://github.com/pandeconscious">Harshit Pande</a>
<p> Amazon </p>
</td>
<td>
<a href="https://github.com/morningsky"><img width="70" height="70" src="https://github.com/morningsky.png?s=40" alt="pic"></a><br>
<a href="https://github.com/morningsky">Lai Mincai</a>
<p> ByteDance </p>
</td>
<td>
<a href="https://github.com/codewithzichao"><img width="70" height="70" src="https://github.com/codewithzichao.png?s=40" alt="pic"></a><br>
<a href="https://github.com/codewithzichao">Li Zichao</a>
<p> ByteDance </p>
</td>
<td>
<a href="https://github.com/TanTingyi"><img width="70" height="70" src="https://github.com/TanTingyi.png?s=40" alt="pic"></a><br>
<a href="https://github.com/TanTingyi">Tan Tingyi</a>
<p> Chongqing University <br> of Posts and <br> Telecommunications </p>
</td>
</tr>
</tbody>
</table>
================================================
FILE: deepctr/__init__.py
================================================
from .utils import check_version
__version__ = '0.9.3'
check_version(__version__)
================================================
FILE: deepctr/contrib/__init__.py
================================================
================================================
FILE: deepctr/contrib/rnn.py
================================================
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""RNN helpers for TensorFlow models.
@@bidirectional_dynamic_rnn
@@dynamic_rnn
@@raw_rnn
@@static_rnn
@@static_state_saving_rnn
@@static_bidirectional_rnn
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_shape
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import rnn_cell_impl
from tensorflow.python.ops import tensor_array_ops
from tensorflow.python.ops import variable_scope as vs
from tensorflow.python.util import nest
import tensorflow as tf
def _like_rnncell_(cell):
"""Checks that a given object is an RNNCell by using duck typing."""
conditions = [hasattr(cell, "output_size"), hasattr(cell, "state_size"),
hasattr(cell, "zero_state"), callable(cell)]
return all(conditions)
# pylint: disable=protected-access
_concat = rnn_cell_impl._concat
try:
_like_rnncell = rnn_cell_impl._like_rnncell
except Exception as e:
_like_rnncell = _like_rnncell_
# pylint: enable=protected-access
def _transpose_batch_time(x):
"""Transpose the batch and time dimensions of a Tensor.
Retains as much of the static shape information as possible.
Args:
x: A tensor of rank 2 or higher.
Returns:
x transposed along the first two dimensions.
Raises:
ValueError: if `x` is rank 1 or lower.
"""
x_static_shape = x.get_shape()
if x_static_shape.ndims is not None and x_static_shape.ndims < 2:
raise ValueError(
"Expected input tensor %s to have rank at least 2, but saw shape: %s" %
(x, x_static_shape))
x_rank = array_ops.rank(x)
x_t = array_ops.transpose(
x, array_ops.concat(
([1, 0], math_ops.range(2, x_rank)), axis=0))
x_t.set_shape(
tensor_shape.TensorShape([
x_static_shape[1].value, x_static_shape[0].value
]).concatenate(x_static_shape[2:]))
return x_t
def _best_effort_input_batch_size(flat_input):
"""Get static input batch size if available, with fallback to the dynamic one.
Args:
flat_input: An iterable of time major input Tensors of shape [max_time,
batch_size, ...]. All inputs should have compatible batch sizes.
Returns:
The batch size in Python integer if available, or a scalar Tensor otherwise.
Raises:
ValueError: if there is any input with an invalid shape.
"""
for input_ in flat_input:
shape = input_.shape
if shape.ndims is None:
continue
if shape.ndims < 2:
raise ValueError(
"Expected input tensor %s to have rank at least 2" % input_)
batch_size = shape[1].value
if batch_size is not None:
return batch_size
# Fallback to the dynamic batch size of the first input.
return array_ops.shape(flat_input[0])[1]
def _infer_state_dtype(explicit_dtype, state):
"""Infer the dtype of an RNN state.
Args:
explicit_dtype: explicitly declared dtype or None.
state: RNN's hidden state. Must be a Tensor or a nested iterable containing
Tensors.
Returns:
dtype: inferred dtype of hidden state.
Raises:
ValueError: if `state` has heterogeneous dtypes or is empty.
"""
if explicit_dtype is not None:
return explicit_dtype
elif nest.is_sequence(state):
inferred_dtypes = [element.dtype for element in nest.flatten(state)]
if not inferred_dtypes:
raise ValueError("Unable to infer dtype from empty state.")
all_same = all([x == inferred_dtypes[0] for x in inferred_dtypes])
if not all_same:
raise ValueError(
"State has tensors of different inferred_dtypes. Unable to infer a "
"single representative dtype.")
return inferred_dtypes[0]
else:
return state.dtype
# pylint: disable=unused-argument
def _rnn_step(
time, sequence_length, min_sequence_length, max_sequence_length,
zero_output, state, call_cell, state_size, skip_conditionals=False):
"""Calculate one step of a dynamic RNN minibatch.
Returns an (output, state) pair conditioned on the sequence_lengths.
When skip_conditionals=False, the pseudocode is something like:
if t >= max_sequence_length:
return (zero_output, state)
if t < min_sequence_length:
return call_cell()
# Selectively output zeros or output, old state or new state depending
# on if we've finished calculating each row.
new_output, new_state = call_cell()
final_output = np.vstack([
zero_output if time >= sequence_lengths[r] else new_output_r
for r, new_output_r in enumerate(new_output)
])
final_state = np.vstack([
state[r] if time >= sequence_lengths[r] else new_state_r
for r, new_state_r in enumerate(new_state)
])
return (final_output, final_state)
Args:
time: Python int, the current time step
sequence_length: int32 `Tensor` vector of size [batch_size]
min_sequence_length: int32 `Tensor` scalar, min of sequence_length
max_sequence_length: int32 `Tensor` scalar, max of sequence_length
zero_output: `Tensor` vector of shape [output_size]
state: Either a single `Tensor` matrix of shape `[batch_size, state_size]`,
or a list/tuple of such tensors.
call_cell: lambda returning tuple of (new_output, new_state) where
new_output is a `Tensor` matrix of shape `[batch_size, output_size]`.
new_state is a `Tensor` matrix of shape `[batch_size, state_size]`.
state_size: The `cell.state_size` associated with the state.
skip_conditionals: Python bool, whether to skip using the conditional
calculations. This is useful for `dynamic_rnn`, where the input tensor
matches `max_sequence_length`, and using conditionals just slows
everything down.
Returns:
A tuple of (`final_output`, `final_state`) as given by the pseudocode above:
final_output is a `Tensor` matrix of shape [batch_size, output_size]
final_state is either a single `Tensor` matrix, or a tuple of such
matrices (matching length and shapes of input `state`).
Raises:
ValueError: If the cell returns a state tuple whose length does not match
that returned by `state_size`.
"""
# Convert state to a list for ease of use
flat_state = nest.flatten(state)
flat_zero_output = nest.flatten(zero_output)
def _copy_one_through(output, new_output):
# If the state contains a scalar value we simply pass it through.
if output.shape.ndims == 0:
return new_output
copy_cond = (time >= sequence_length)
with ops.colocate_with(new_output):
return array_ops.where(copy_cond, output, new_output)
def _copy_some_through(flat_new_output, flat_new_state):
# Use broadcasting select to determine which values should get
# the previous state & zero output, and which values should get
# a calculated state & output.
flat_new_output = [
_copy_one_through(zero_output, new_output)
for zero_output, new_output in zip(flat_zero_output, flat_new_output)]
flat_new_state = [
_copy_one_through(state, new_state)
for state, new_state in zip(flat_state, flat_new_state)]
return flat_new_output + flat_new_state
def _maybe_copy_some_through():
"""Run RNN step. Pass through either no or some past state."""
new_output, new_state = call_cell()
nest.assert_same_structure(state, new_state)
flat_new_state = nest.flatten(new_state)
flat_new_output = nest.flatten(new_output)
return control_flow_ops.cond(
# if t < min_seq_len: calculate and return everything
time < min_sequence_length, lambda: flat_new_output + flat_new_state,
# else copy some of it through
lambda: _copy_some_through(flat_new_output, flat_new_state))
# TODO(ebrevdo): skipping these conditionals may cause a slowdown,
# but benefits from removing cond() and its gradient. We should
# profile with and without this switch here.
if skip_conditionals:
# Instead of using conditionals, perform the selective copy at all time
# steps. This is faster when max_seq_len is equal to the number of unrolls
# (which is typical for dynamic_rnn).
new_output, new_state = call_cell()
nest.assert_same_structure(state, new_state)
new_state = nest.flatten(new_state)
new_output = nest.flatten(new_output)
final_output_and_state = _copy_some_through(new_output, new_state)
else:
empty_update = lambda: flat_zero_output + flat_state
final_output_and_state = control_flow_ops.cond(
# if t >= max_seq_len: copy all state through, output zeros
time >= max_sequence_length, empty_update,
# otherwise calculation is required: copy some or all of it through
_maybe_copy_some_through)
if len(final_output_and_state) != len(flat_zero_output) + len(flat_state):
raise ValueError("Internal error: state and output were not concatenated "
"correctly.")
final_output = final_output_and_state[:len(flat_zero_output)]
final_state = final_output_and_state[len(flat_zero_output):]
for output, flat_output in zip(final_output, flat_zero_output):
output.set_shape(flat_output.get_shape())
for substate, flat_substate in zip(final_state, flat_state):
substate.set_shape(flat_substate.get_shape())
final_output = nest.pack_sequence_as(
structure=zero_output, flat_sequence=final_output)
final_state = nest.pack_sequence_as(
structure=state, flat_sequence=final_state)
return final_output, final_state
def _reverse_seq(input_seq, lengths):
"""Reverse a list of Tensors up to specified lengths.
Args:
input_seq: Sequence of seq_len tensors of dimension (batch_size, n_features)
or nested tuples of tensors.
lengths: A `Tensor` of dimension batch_size, containing lengths for each
sequence in the batch. If "None" is specified, simply reverses
the list.
Returns:
time-reversed sequence
"""
if lengths is None:
return list(reversed(input_seq))
flat_input_seq = tuple(nest.flatten(input_) for input_ in input_seq)
flat_results = [[] for _ in range(len(input_seq))]
for sequence in zip(*flat_input_seq):
input_shape = tensor_shape.unknown_shape(
ndims=sequence[0].get_shape().ndims)
for input_ in sequence:
input_shape.merge_with(input_.get_shape())
input_.set_shape(input_shape)
# Join into (time, batch_size, depth)
s_joined = array_ops.stack(sequence)
# Reverse along dimension 0
s_reversed = array_ops.reverse_sequence(s_joined, lengths, 0, 1)
# Split again into list
result = array_ops.unstack(s_reversed)
for r, flat_result in zip(result, flat_results):
r.set_shape(input_shape)
flat_result.append(r)
results = [nest.pack_sequence_as(structure=input_, flat_sequence=flat_result)
for input_, flat_result in zip(input_seq, flat_results)]
return results
#
# def bidirectional_dynamic_rnn(cell_fw, cell_bw, inputs, sequence_length=None,
#
# initial_state_fw=None, initial_state_bw=None,
#
# dtype=None, parallel_iterations=None,
#
# swap_memory=False, time_major=False, scope=None):
#
# """Creates a dynamic version of bidirectional recurrent neural network.
#
#
#
# Takes input and builds independent forward and backward RNNs. The input_size
#
# of forward and backward cell must match. The initial state for both directions
#
# is zero by default (but can be set optionally) and no intermediate states are
#
# ever returned -- the network is fully unrolled for the given (passed in)
#
# length(s) of the sequence(s) or completely unrolled if length(s) is not
#
# given.
#
#
#
# Args:
#
# cell_fw: An instance of RNNCell, to be used for forward direction.
#
# cell_bw: An instance of RNNCell, to be used for backward direction.
#
# inputs: The RNN inputs.
#
# If time_major == False (default), this must be a tensor of shape:
#
# `[batch_size, max_time, ...]`, or a nested tuple of such elements.
#
# If time_major == True, this must be a tensor of shape:
#
# `[max_time, batch_size, ...]`, or a nested tuple of such elements.
#
# sequence_length: (optional) An int32/int64 vector, size `[batch_size]`,
#
# containing the actual lengths for each of the sequences in the batch.
#
# If not provided, all batch entries are assumed to be full sequences; and
#
# time reversal is applied from time `0` to `max_time` for each sequence.
#
# initial_state_fw: (optional) An initial state for the forward RNN.
#
# This must be a tensor of appropriate type and shape
#
# `[batch_size, cell_fw.state_size]`.
#
# If `cell_fw.state_size` is a tuple, this should be a tuple of
#
# tensors having shapes `[batch_size, s] for s in cell_fw.state_size`.
#
# initial_state_bw: (optional) Same as for `initial_state_fw`, but using
#
# the corresponding properties of `cell_bw`.
#
# dtype: (optional) The data type for the initial states and expected output.
#
# Required if initial_states are not provided or RNN states have a
#
# heterogeneous dtype.
#
# parallel_iterations: (Default: 32). The number of iterations to run in
#
# parallel. Those operations which do not have any temporal dependency
#
# and can be run in parallel, will be. This parameter trades off
#
# time for space. Values >> 1 use more memory but take less time,
#
# while smaller values use less memory but computations take longer.
#
# swap_memory: Transparently swap the tensors produced in forward inference
#
# but needed for back prop from GPU to CPU. This allows training RNNs
#
# which would typically not fit on a single GPU, with very minimal (or no)
#
# performance penalty.
#
# time_major: The shape format of the `inputs` and `outputs` Tensors.
#
# If true, these `Tensors` must be shaped `[max_time, batch_size, depth]`.
#
# If false, these `Tensors` must be shaped `[batch_size, max_time, depth]`.
#
# Using `time_major = True` is a bit more efficient because it avoids
#
# transposes at the beginning and end of the RNN calculation. However,
#
# most TensorFlow data is batch-major, so by default this function
#
# accepts input and emits output in batch-major form.
#
# scope: VariableScope for the created subgraph; defaults to
#
# "bidirectional_rnn"
#
#
#
# Returns:
#
# A tuple (outputs, output_states) where:
#
# outputs: A tuple (output_fw, output_bw) containing the forward and
#
# the backward rnn output `Tensor`.
#
# If time_major == False (default),
#
# output_fw will be a `Tensor` shaped:
#
# `[batch_size, max_time, cell_fw.output_size]`
#
# and output_bw will be a `Tensor` shaped:
#
# `[batch_size, max_time, cell_bw.output_size]`.
#
# If time_major == True,
#
# output_fw will be a `Tensor` shaped:
#
# `[max_time, batch_size, cell_fw.output_size]`
#
# and output_bw will be a `Tensor` shaped:
#
# `[max_time, batch_size, cell_bw.output_size]`.
#
# It returns a tuple instead of a single concatenated `Tensor`, unlike
#
# in the `bidirectional_rnn`. If the concatenated one is preferred,
#
# the forward and backward outputs can be concatenated as
#
# `tf.concat(outputs, 2)`.
#
# output_states: A tuple (output_state_fw, output_state_bw) containing
#
# the forward and the backward final states of bidirectional rnn.
#
#
#
# Raises:
#
# TypeError: If `cell_fw` or `cell_bw` is not an instance of `RNNCell`.
#
# """
#
#
#
# if not _like_rnncell(cell_fw):
#
# raise TypeError("cell_fw must be an instance of RNNCell")
#
# if not _like_rnncell(cell_bw):
#
# raise TypeError("cell_bw must be an instance of RNNCell")
#
#
#
# with vs.variable_scope(scope or "bidirectional_rnn"):
#
# # Forward direction
#
# with vs.variable_scope("fw") as fw_scope:
#
# output_fw, output_state_fw = dynamic_rnn(
#
# cell=cell_fw, inputs=inputs, sequence_length=sequence_length,
#
# initial_state=initial_state_fw, dtype=dtype,
#
# parallel_iterations=parallel_iterations, swap_memory=swap_memory,
#
# time_major=time_major, scope=fw_scope)
#
#
#
# # Backward direction
#
# if not time_major:
#
# time_dim = 1
#
# batch_dim = 0
#
# else:
#
# time_dim = 0
#
# batch_dim = 1
#
#
#
# def _reverse(input_, seq_lengths, seq_dim, batch_dim):
#
# if seq_lengths is not None:
#
# return array_ops.reverse_sequence(
#
# input=input_, seq_lengths=seq_lengths,
#
# seq_dim=seq_dim, batch_dim=batch_dim)
#
# else:
#
# return array_ops.reverse(input_, axis=[seq_dim])
#
#
#
# with vs.variable_scope("bw") as bw_scope:
#
# inputs_reverse = _reverse(
#
# inputs, seq_lengths=sequence_length,
#
# seq_dim=time_dim, batch_dim=batch_dim)
#
# tmp, output_state_bw = dynamic_rnn(
#
# cell=cell_bw, inputs=inputs_reverse, sequence_length=sequence_length,
#
# initial_state=initial_state_bw, dtype=dtype,
#
# parallel_iterations=parallel_iterations, swap_memory=swap_memory,
#
# time_major=time_major, scope=bw_scope)
#
#
#
# output_bw = _reverse(
#
# tmp, seq_lengths=sequence_length,
#
# seq_dim=time_dim, batch_dim=batch_dim)
#
#
#
# outputs = (output_fw, output_bw)
#
# output_states = (output_state_fw, output_state_bw)
#
#
#
# return (outputs, output_states)
#
def dynamic_rnn(cell, inputs, att_scores=None, sequence_length=None, initial_state=None,
dtype=None, parallel_iterations=None, swap_memory=False,
time_major=False, scope=None):
"""Creates a recurrent neural network specified by RNNCell `cell`.
Performs fully dynamic unrolling of `inputs`.
Example:
```python
# create a BasicRNNCell
rnn_cell = tf.nn.rnn_cell.BasicRNNCell(hidden_size)
# 'outputs' is a tensor of shape [batch_size, max_time, cell_state_size]
# defining initial state
initial_state = rnn_cell.zero_state(batch_size, dtype=tf.float32)
# 'state' is a tensor of shape [batch_size, cell_state_size]
outputs, state = tf.nn.dynamic_rnn(rnn_cell, input_data,
initial_state=initial_state,
dtype=tf.float32)
```
```python
# create 2 LSTMCells
rnn_layers = [tf.nn.rnn_cell.LSTMCell(size) for size in [128, 256]]
# create a RNN cell composed sequentially of a number of RNNCells
multi_rnn_cell = tf.nn.rnn_cell.MultiRNNCell(rnn_layers)
# 'outputs' is a tensor of shape [batch_size, max_time, 256]
# 'state' is a N-tuple where N is the number of LSTMCells containing a
# tf.contrib.rnn.LSTMStateTuple for each cell
outputs, state = tf.nn.dynamic_rnn(cell=multi_rnn_cell,
inputs=data,
dtype=tf.float32)
```
Args:
cell: An instance of RNNCell.
inputs: The RNN inputs.
If `time_major == False` (default), this must be a `Tensor` of shape:
`[batch_size, max_time, ...]`, or a nested tuple of such
elements.
If `time_major == True`, this must be a `Tensor` of shape:
`[max_time, batch_size, ...]`, or a nested tuple of such
elements.
This may also be a (possibly nested) tuple of Tensors satisfying
this property. The first two dimensions must match across all the inputs,
but otherwise the ranks and other shape components may differ.
In this case, input to `cell` at each time-step will replicate the
structure of these tuples, except for the time dimension (from which the
time is taken).
The input to `cell` at each time step will be a `Tensor` or (possibly
nested) tuple of Tensors each with dimensions `[batch_size, ...]`.
sequence_length: (optional) An int32/int64 vector sized `[batch_size]`.
Used to copy-through state and zero-out outputs when past a batch
element's sequence length. So it's more for correctness than performance.
initial_state: (optional) An initial state for the RNN.
If `cell.state_size` is an integer, this must be
a `Tensor` of appropriate type and shape `[batch_size, cell.state_size]`.
If `cell.state_size` is a tuple, this should be a tuple of
tensors having shapes `[batch_size, s] for s in cell.state_size`.
dtype: (optional) The data type for the initial state and expected output.
Required if initial_state is not provided or RNN state has a heterogeneous
dtype.
parallel_iterations: (Default: 32). The number of iterations to run in
parallel. Those operations which do not have any temporal dependency
and can be run in parallel, will be. This parameter trades off
time for space. Values >> 1 use more memory but take less time,
while smaller values use less memory but computations take longer.
swap_memory: Transparently swap the tensors produced in forward inference
but needed for back prop from GPU to CPU. This allows training RNNs
which would typically not fit on a single GPU, with very minimal (or no)
performance penalty.
time_major: The shape format of the `inputs` and `outputs` Tensors.
If true, these `Tensors` must be shaped `[max_time, batch_size, depth]`.
If false, these `Tensors` must be shaped `[batch_size, max_time, depth]`.
Using `time_major = True` is a bit more efficient because it avoids
transposes at the beginning and end of the RNN calculation. However,
most TensorFlow data is batch-major, so by default this function
accepts input and emits output in batch-major form.
scope: VariableScope for the created subgraph; defaults to "rnn".
Returns:
A pair (outputs, state) where:
outputs: The RNN output `Tensor`.
If time_major == False (default), this will be a `Tensor` shaped:
`[batch_size, max_time, cell.output_size]`.
If time_major == True, this will be a `Tensor` shaped:
`[max_time, batch_size, cell.output_size]`.
Note, if `cell.output_size` is a (possibly nested) tuple of integers
or `TensorShape` objects, then `outputs` will be a tuple having the
same structure as `cell.output_size`, containing Tensors having shapes
corresponding to the shape data in `cell.output_size`.
state: The final state. If `cell.state_size` is an int, this
will be shaped `[batch_size, cell.state_size]`. If it is a
`TensorShape`, this will be shaped `[batch_size] + cell.state_size`.
If it is a (possibly nested) tuple of ints or `TensorShape`, this will
be a tuple having the corresponding shapes. If cells are `LSTMCells`
`state` will be a tuple containing a `LSTMStateTuple` for each cell.
Raises:
TypeError: If `cell` is not an instance of RNNCell.
ValueError: If inputs is None or an empty list.
"""
if not _like_rnncell(cell):
raise TypeError("cell must be an instance of RNNCell")
# By default, time_major==False and inputs are batch-major: shaped
# [batch, time, depth]
# For internal calculations, we transpose to [time, batch, depth]
flat_input = nest.flatten(inputs)
if not time_major:
# (B,T,D) => (T,B,D)
flat_input = [ops.convert_to_tensor(input_) for input_ in flat_input]
flat_input = tuple(_transpose_batch_time(input_) for input_ in flat_input)
parallel_iterations = parallel_iterations or 32
if sequence_length is not None:
sequence_length = math_ops.to_int32(sequence_length)
if sequence_length.get_shape().ndims not in (None, 1):
raise ValueError(
"sequence_length must be a vector of length batch_size, "
"but saw shape: %s" % sequence_length.get_shape())
sequence_length = array_ops.identity( # Just to find it in the graph.
sequence_length, name="sequence_length")
# Create a new scope in which the caching device is either
# determined by the parent scope, or is set to place the cached
# Variable using the same placement as for the rest of the RNN.
with vs.variable_scope(scope or "rnn",reuse=tf.AUTO_REUSE) as varscope:#TODO:user defined reuse
if varscope.caching_device is None:
varscope.set_caching_device(lambda op: op.device)
batch_size = _best_effort_input_batch_size(flat_input)
if initial_state is not None:
state = initial_state
else:
if not dtype:
raise ValueError("If there is no initial_state, you must give a dtype.")
state = cell.zero_state(batch_size, dtype)
def _assert_has_shape(x, shape):
x_shape = array_ops.shape(x)
packed_shape = array_ops.stack(shape)
return control_flow_ops.Assert(
math_ops.reduce_all(math_ops.equal(x_shape, packed_shape)),
["Expected shape for Tensor %s is " % x.name,
packed_shape, " but saw shape: ", x_shape])
if sequence_length is not None:
# Perform some shape validation
with ops.control_dependencies(
[_assert_has_shape(sequence_length, [batch_size])]):
sequence_length = array_ops.identity(
sequence_length, name="CheckSeqLen")
inputs = nest.pack_sequence_as(structure=inputs, flat_sequence=flat_input)
(outputs, final_state) = _dynamic_rnn_loop(
cell,
inputs,
state,
parallel_iterations=parallel_iterations,
swap_memory=swap_memory,
att_scores=att_scores,
sequence_length=sequence_length,
dtype=dtype)
# Outputs of _dynamic_rnn_loop are always shaped [time, batch, depth].
# If we are performing batch-major calculations, transpose output back
# to shape [batch, time, depth]
if not time_major:
# (T,B,D) => (B,T,D)
outputs = nest.map_structure(_transpose_batch_time, outputs)
return (outputs, final_state)
def _dynamic_rnn_loop(cell,
inputs,
initial_state,
parallel_iterations,
swap_memory,
att_scores=None,
sequence_length=None,
dtype=None):
"""Internal implementation of Dynamic RNN.
Args:
cell: An instance of RNNCell.
inputs: A `Tensor` of shape [time, batch_size, input_size], or a nested
tuple of such elements.
initial_state: A `Tensor` of shape `[batch_size, state_size]`, or if
`cell.state_size` is a tuple, then this should be a tuple of
tensors having shapes `[batch_size, s] for s in cell.state_size`.
parallel_iterations: Positive Python int.
swap_memory: A Python boolean
sequence_length: (optional) An `int32` `Tensor` of shape [batch_size].
dtype: (optional) Expected dtype of output. If not specified, inferred from
initial_state.
Returns:
Tuple `(final_outputs, final_state)`.
final_outputs:
A `Tensor` of shape `[time, batch_size, cell.output_size]`. If
`cell.output_size` is a (possibly nested) tuple of ints or `TensorShape`
objects, then this returns a (possibly nsted) tuple of Tensors matching
the corresponding shapes.
final_state:
A `Tensor`, or possibly nested tuple of Tensors, matching in length
and shapes to `initial_state`.
Raises:
ValueError: If the input depth cannot be inferred via shape inference
from the inputs.
"""
state = initial_state
assert isinstance(parallel_iterations, int), "parallel_iterations must be int"
state_size = cell.state_size
flat_input = nest.flatten(inputs)
flat_output_size = nest.flatten(cell.output_size)
# Construct an initial output
input_shape = array_ops.shape(flat_input[0])
time_steps = input_shape[0]
batch_size = _best_effort_input_batch_size(flat_input)
inputs_got_shape = tuple(input_.get_shape().with_rank_at_least(3)
for input_ in flat_input)
const_time_steps, const_batch_size = inputs_got_shape[0].as_list()[:2]
for shape in inputs_got_shape:
if not shape[2:].is_fully_defined():
raise ValueError(
"Input size (depth of inputs) must be accessible via shape inference,"
" but saw value None.")
got_time_steps = shape[0].value
got_batch_size = shape[1].value
if const_time_steps != got_time_steps:
raise ValueError(
"Time steps is not the same for all the elements in the input in a "
"batch.")
if const_batch_size != got_batch_size:
raise ValueError(
"Batch_size is not the same for all the elements in the input.")
# Prepare dynamic conditional copying of state & output
def _create_zero_arrays(size):
size = _concat(batch_size, size)
return array_ops.zeros(
array_ops.stack(size), _infer_state_dtype(dtype, state))
flat_zero_output = tuple(_create_zero_arrays(output)
for output in flat_output_size)
zero_output = nest.pack_sequence_as(structure=cell.output_size,
flat_sequence=flat_zero_output)
if sequence_length is not None:
min_sequence_length = math_ops.reduce_min(sequence_length)
max_sequence_length = math_ops.reduce_max(sequence_length)
time = array_ops.constant(0, dtype=dtypes.int32, name="time")
with ops.name_scope("dynamic_rnn") as scope:
base_name = scope
def _create_ta(name, dtype):
return tensor_array_ops.TensorArray(dtype=dtype,
size=time_steps,
tensor_array_name=base_name + name)
output_ta = tuple(_create_ta("output_%d" % i,
_infer_state_dtype(dtype, state))
for i in range(len(flat_output_size)))
input_ta = tuple(_create_ta("input_%d" % i, flat_input[i].dtype)
for i in range(len(flat_input)))
input_ta = tuple(ta.unstack(input_)
for ta, input_ in zip(input_ta, flat_input))
def _time_step(time, output_ta_t, state, att_scores=None):
"""Take a time step of the dynamic RNN.
Args:
time: int32 scalar Tensor.
output_ta_t: List of `TensorArray`s that represent the output.
state: nested tuple of vector tensors that represent the state.
Returns:
The tuple (time + 1, output_ta_t with updated flow, new_state).
"""
input_t = tuple(ta.read(time) for ta in input_ta)
# Restore some shape information
for input_, shape in zip(input_t, inputs_got_shape):
input_.set_shape(shape[1:])
input_t = nest.pack_sequence_as(structure=inputs, flat_sequence=input_t)
if att_scores is not None:
att_score = att_scores[:, time, :]
call_cell = lambda: cell(input_t, state, att_score)
else:
call_cell = lambda: cell(input_t, state)
if sequence_length is not None:
(output, new_state) = _rnn_step(
time=time,
sequence_length=sequence_length,
min_sequence_length=min_sequence_length,
max_sequence_length=max_sequence_length,
zero_output=zero_output,
state=state,
call_cell=call_cell,
state_size=state_size,
skip_conditionals=True)
else:
(output, new_state) = call_cell()
# Pack state if using state tuples
output = nest.flatten(output)
output_ta_t = tuple(
ta.write(time, out) for ta, out in zip(output_ta_t, output))
if att_scores is not None:
return (time + 1, output_ta_t, new_state, att_scores)
else:
return (time + 1, output_ta_t, new_state)
if att_scores is not None:
_, output_final_ta, final_state, _ = control_flow_ops.while_loop(
cond=lambda time, *_: time < time_steps,
body=_time_step,
loop_vars=(time, output_ta, state, att_scores),
parallel_iterations=parallel_iterations,
swap_memory=swap_memory)
else:
_, output_final_ta, final_state = control_flow_ops.while_loop(
cond=lambda time, *_: time < time_steps,
body=_time_step,
loop_vars=(time, output_ta, state),
parallel_iterations=parallel_iterations,
swap_memory=swap_memory)
# Unpack final output if not using output tuples.
final_outputs = tuple(ta.stack() for ta in output_final_ta)
# Restore some shape information
for output, output_size in zip(final_outputs, flat_output_size):
shape = _concat(
[const_time_steps, const_batch_size], output_size, static=True)
output.set_shape(shape)
final_outputs = nest.pack_sequence_as(
structure=cell.output_size, flat_sequence=final_outputs)
return (final_outputs, final_state)
================================================
FILE: deepctr/contrib/rnn_v2.py
================================================
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""RNN helpers for TensorFlow models.
@@bidirectional_dynamic_rnn
@@dynamic_rnn
@@raw_rnn
@@static_rnn
@@static_state_saving_rnn
@@static_bidirectional_rnn
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_shape
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import rnn_cell_impl
from tensorflow.python.ops import tensor_array_ops
from tensorflow.python.ops import variable_scope as vs
from tensorflow.python.util import nest
import tensorflow as tf
def _like_rnncell_(cell):
"""Checks that a given object is an RNNCell by using duck typing."""
conditions = [hasattr(cell, "output_size"), hasattr(cell, "state_size"),
hasattr(cell, "zero_state"), callable(cell)]
return all(conditions)
# pylint: disable=protected-access
_concat = rnn_cell_impl._concat
try:
_like_rnncell = rnn_cell_impl._like_rnncell
except:
_like_rnncell = _like_rnncell_
# pylint: enable=protected-access
def _transpose_batch_time(x):
"""Transpose the batch and time dimensions of a Tensor.
Retains as much of the static shape information as possible.
Args:
x: A tensor of rank 2 or higher.
Returns:
x transposed along the first two dimensions.
Raises:
ValueError: if `x` is rank 1 or lower.
"""
x_static_shape = x.get_shape()
if x_static_shape.ndims is not None and x_static_shape.ndims < 2:
raise ValueError(
"Expected input tensor %s to have rank at least 2, but saw shape: %s" %
(x, x_static_shape))
x_rank = array_ops.rank(x)
x_t = array_ops.transpose(
x, array_ops.concat(
([1, 0], math_ops.range(2, x_rank)), axis=0))
x_t.set_shape(
tensor_shape.TensorShape([
x_static_shape[1], x_static_shape[0]
]).concatenate(x_static_shape[2:]))
return x_t
def _best_effort_input_batch_size(flat_input):
"""Get static input batch size if available, with fallback to the dynamic one.
Args:
flat_input: An iterable of time major input Tensors of shape [max_time,
batch_size, ...]. All inputs should have compatible batch sizes.
Returns:
The batch size in Python integer if available, or a scalar Tensor otherwise.
Raises:
ValueError: if there is any input with an invalid shape.
"""
for input_ in flat_input:
shape = input_.shape
if shape.ndims is None:
continue
if shape.ndims < 2:
raise ValueError(
"Expected input tensor %s to have rank at least 2" % input_)
batch_size = shape[1]
if batch_size is not None:
return batch_size
# Fallback to the dynamic batch size of the first input.
return array_ops.shape(flat_input[0])[1]
def _infer_state_dtype(explicit_dtype, state):
"""Infer the dtype of an RNN state.
Args:
explicit_dtype: explicitly declared dtype or None.
state: RNN's hidden state. Must be a Tensor or a nested iterable containing
Tensors.
Returns:
dtype: inferred dtype of hidden state.
Raises:
ValueError: if `state` has heterogeneous dtypes or is empty.
"""
if explicit_dtype is not None:
return explicit_dtype
elif nest.is_sequence(state):
inferred_dtypes = [element.dtype for element in nest.flatten(state)]
if not inferred_dtypes:
raise ValueError("Unable to infer dtype from empty state.")
all_same = all([x == inferred_dtypes[0] for x in inferred_dtypes])
if not all_same:
raise ValueError(
"State has tensors of different inferred_dtypes. Unable to infer a "
"single representative dtype.")
return inferred_dtypes[0]
else:
return state.dtype
# pylint: disable=unused-argument
def _rnn_step(
time, sequence_length, min_sequence_length, max_sequence_length,
zero_output, state, call_cell, state_size, skip_conditionals=False):
"""Calculate one step of a dynamic RNN minibatch.
Returns an (output, state) pair conditioned on the sequence_lengths.
When skip_conditionals=False, the pseudocode is something like:
if t >= max_sequence_length:
return (zero_output, state)
if t < min_sequence_length:
return call_cell()
# Selectively output zeros or output, old state or new state depending
# on if we've finished calculating each row.
new_output, new_state = call_cell()
final_output = np.vstack([
zero_output if time >= sequence_lengths[r] else new_output_r
for r, new_output_r in enumerate(new_output)
])
final_state = np.vstack([
state[r] if time >= sequence_lengths[r] else new_state_r
for r, new_state_r in enumerate(new_state)
])
return (final_output, final_state)
Args:
time: Python int, the current time step
sequence_length: int32 `Tensor` vector of size [batch_size]
min_sequence_length: int32 `Tensor` scalar, min of sequence_length
max_sequence_length: int32 `Tensor` scalar, max of sequence_length
zero_output: `Tensor` vector of shape [output_size]
state: Either a single `Tensor` matrix of shape `[batch_size, state_size]`,
or a list/tuple of such tensors.
call_cell: lambda returning tuple of (new_output, new_state) where
new_output is a `Tensor` matrix of shape `[batch_size, output_size]`.
new_state is a `Tensor` matrix of shape `[batch_size, state_size]`.
state_size: The `cell.state_size` associated with the state.
skip_conditionals: Python bool, whether to skip using the conditional
calculations. This is useful for `dynamic_rnn`, where the input tensor
matches `max_sequence_length`, and using conditionals just slows
everything down.
Returns:
A tuple of (`final_output`, `final_state`) as given by the pseudocode above:
final_output is a `Tensor` matrix of shape [batch_size, output_size]
final_state is either a single `Tensor` matrix, or a tuple of such
matrices (matching length and shapes of input `state`).
Raises:
ValueError: If the cell returns a state tuple whose length does not match
that returned by `state_size`.
"""
# Convert state to a list for ease of use
flat_state = nest.flatten(state)
flat_zero_output = nest.flatten(zero_output)
def _copy_one_through(output, new_output):
# If the state contains a scalar value we simply pass it through.
if output.shape.ndims == 0:
return new_output
copy_cond = (time >= sequence_length)
with ops.colocate_with(new_output):
return array_ops.where(copy_cond, output, new_output)
def _copy_some_through(flat_new_output, flat_new_state):
# Use broadcasting select to determine which values should get
# the previous state & zero output, and which values should get
# a calculated state & output.
flat_new_output = [
_copy_one_through(zero_output, new_output)
for zero_output, new_output in zip(flat_zero_output, flat_new_output)]
flat_new_state = [
_copy_one_through(state, new_state)
for state, new_state in zip(flat_state, flat_new_state)]
return flat_new_output + flat_new_state
def _maybe_copy_some_through():
"""Run RNN step. Pass through either no or some past state."""
new_output, new_state = call_cell()
nest.assert_same_structure(state, new_state)
flat_new_state = nest.flatten(new_state)
flat_new_output = nest.flatten(new_output)
return control_flow_ops.cond(
# if t < min_seq_len: calculate and return everything
time < min_sequence_length, lambda: flat_new_output + flat_new_state,
# else copy some of it through
lambda: _copy_some_through(flat_new_output, flat_new_state))
# TODO(ebrevdo): skipping these conditionals may cause a slowdown,
# but benefits from removing cond() and its gradient. We should
# profile with and without this switch here.
if skip_conditionals:
# Instead of using conditionals, perform the selective copy at all time
# steps. This is faster when max_seq_len is equal to the number of unrolls
# (which is typical for dynamic_rnn).
new_output, new_state = call_cell()
nest.assert_same_structure(state, new_state)
new_state = nest.flatten(new_state)
new_output = nest.flatten(new_output)
final_output_and_state = _copy_some_through(new_output, new_state)
else:
empty_update = lambda: flat_zero_output + flat_state
final_output_and_state = control_flow_ops.cond(
# if t >= max_seq_len: copy all state through, output zeros
time >= max_sequence_length, empty_update,
# otherwise calculation is required: copy some or all of it through
_maybe_copy_some_through)
if len(final_output_and_state) != len(flat_zero_output) + len(flat_state):
raise ValueError("Internal error: state and output were not concatenated "
"correctly.")
final_output = final_output_and_state[:len(flat_zero_output)]
final_state = final_output_and_state[len(flat_zero_output):]
for output, flat_output in zip(final_output, flat_zero_output):
output.set_shape(flat_output.get_shape())
for substate, flat_substate in zip(final_state, flat_state):
substate.set_shape(flat_substate.get_shape())
final_output = nest.pack_sequence_as(
structure=zero_output, flat_sequence=final_output)
final_state = nest.pack_sequence_as(
structure=state, flat_sequence=final_state)
return final_output, final_state
def _reverse_seq(input_seq, lengths):
"""Reverse a list of Tensors up to specified lengths.
Args:
input_seq: Sequence of seq_len tensors of dimension (batch_size, n_features)
or nested tuples of tensors.
lengths: A `Tensor` of dimension batch_size, containing lengths for each
sequence in the batch. If "None" is specified, simply reverses
the list.
Returns:
time-reversed sequence
"""
if lengths is None:
return list(reversed(input_seq))
flat_input_seq = tuple(nest.flatten(input_) for input_ in input_seq)
flat_results = [[] for _ in range(len(input_seq))]
for sequence in zip(*flat_input_seq):
input_shape = tensor_shape.unknown_shape(
ndims=sequence[0].get_shape().ndims)
for input_ in sequence:
input_shape.merge_with(input_.get_shape())
input_.set_shape(input_shape)
# Join into (time, batch_size, depth)
s_joined = array_ops.stack(sequence)
# Reverse along dimension 0
s_reversed = array_ops.reverse_sequence(s_joined, lengths, 0, 1)
# Split again into list
result = array_ops.unstack(s_reversed)
for r, flat_result in zip(result, flat_results):
r.set_shape(input_shape)
flat_result.append(r)
results = [nest.pack_sequence_as(structure=input_, flat_sequence=flat_result)
for input_, flat_result in zip(input_seq, flat_results)]
return results
#
# def bidirectional_dynamic_rnn(cell_fw, cell_bw, inputs, sequence_length=None,
#
# initial_state_fw=None, initial_state_bw=None,
#
# dtype=None, parallel_iterations=None,
#
# swap_memory=False, time_major=False, scope=None):
#
# """Creates a dynamic version of bidirectional recurrent neural network.
#
#
#
# Takes input and builds independent forward and backward RNNs. The input_size
#
# of forward and backward cell must match. The initial state for both directions
#
# is zero by default (but can be set optionally) and no intermediate states are
#
# ever returned -- the network is fully unrolled for the given (passed in)
#
# length(s) of the sequence(s) or completely unrolled if length(s) is not
#
# given.
#
#
#
# Args:
#
# cell_fw: An instance of RNNCell, to be used for forward direction.
#
# cell_bw: An instance of RNNCell, to be used for backward direction.
#
# inputs: The RNN inputs.
#
# If time_major == False (default), this must be a tensor of shape:
#
# `[batch_size, max_time, ...]`, or a nested tuple of such elements.
#
# If time_major == True, this must be a tensor of shape:
#
# `[max_time, batch_size, ...]`, or a nested tuple of such elements.
#
# sequence_length: (optional) An int32/int64 vector, size `[batch_size]`,
#
# containing the actual lengths for each of the sequences in the batch.
#
# If not provided, all batch entries are assumed to be full sequences; and
#
# time reversal is applied from time `0` to `max_time` for each sequence.
#
# initial_state_fw: (optional) An initial state for the forward RNN.
#
# This must be a tensor of appropriate type and shape
#
# `[batch_size, cell_fw.state_size]`.
#
# If `cell_fw.state_size` is a tuple, this should be a tuple of
#
# tensors having shapes `[batch_size, s] for s in cell_fw.state_size`.
#
# initial_state_bw: (optional) Same as for `initial_state_fw`, but using
#
# the corresponding properties of `cell_bw`.
#
# dtype: (optional) The data type for the initial states and expected output.
#
# Required if initial_states are not provided or RNN states have a
#
# heterogeneous dtype.
#
# parallel_iterations: (Default: 32). The number of iterations to run in
#
# parallel. Those operations which do not have any temporal dependency
#
# and can be run in parallel, will be. This parameter trades off
#
# time for space. Values >> 1 use more memory but take less time,
#
# while smaller values use less memory but computations take longer.
#
# swap_memory: Transparently swap the tensors produced in forward inference
#
# but needed for back prop from GPU to CPU. This allows training RNNs
#
# which would typically not fit on a single GPU, with very minimal (or no)
#
# performance penalty.
#
# time_major: The shape format of the `inputs` and `outputs` Tensors.
#
# If true, these `Tensors` must be shaped `[max_time, batch_size, depth]`.
#
# If false, these `Tensors` must be shaped `[batch_size, max_time, depth]`.
#
# Using `time_major = True` is a bit more efficient because it avoids
#
# transposes at the beginning and end of the RNN calculation. However,
#
# most TensorFlow data is batch-major, so by default this function
#
# accepts input and emits output in batch-major form.
#
# scope: VariableScope for the created subgraph; defaults to
#
# "bidirectional_rnn"
#
#
#
# Returns:
#
# A tuple (outputs, output_states) where:
#
# outputs: A tuple (output_fw, output_bw) containing the forward and
#
# the backward rnn output `Tensor`.
#
# If time_major == False (default),
#
# output_fw will be a `Tensor` shaped:
#
# `[batch_size, max_time, cell_fw.output_size]`
#
# and output_bw will be a `Tensor` shaped:
#
# `[batch_size, max_time, cell_bw.output_size]`.
#
# If time_major == True,
#
# output_fw will be a `Tensor` shaped:
#
# `[max_time, batch_size, cell_fw.output_size]`
#
# and output_bw will be a `Tensor` shaped:
#
# `[max_time, batch_size, cell_bw.output_size]`.
#
# It returns a tuple instead of a single concatenated `Tensor`, unlike
#
# in the `bidirectional_rnn`. If the concatenated one is preferred,
#
# the forward and backward outputs can be concatenated as
#
# `tf.concat(outputs, 2)`.
#
# output_states: A tuple (output_state_fw, output_state_bw) containing
#
# the forward and the backward final states of bidirectional rnn.
#
#
#
# Raises:
#
# TypeError: If `cell_fw` or `cell_bw` is not an instance of `RNNCell`.
#
# """
#
#
#
# if not _like_rnncell(cell_fw):
#
# raise TypeError("cell_fw must be an instance of RNNCell")
#
# if not _like_rnncell(cell_bw):
#
# raise TypeError("cell_bw must be an instance of RNNCell")
#
#
#
# with vs.variable_scope(scope or "bidirectional_rnn"):
#
# # Forward direction
#
# with vs.variable_scope("fw") as fw_scope:
#
# output_fw, output_state_fw = dynamic_rnn(
#
# cell=cell_fw, inputs=inputs, sequence_length=sequence_length,
#
# initial_state=initial_state_fw, dtype=dtype,
#
# parallel_iterations=parallel_iterations, swap_memory=swap_memory,
#
# time_major=time_major, scope=fw_scope)
#
#
#
# # Backward direction
#
# if not time_major:
#
# time_dim = 1
#
# batch_dim = 0
#
# else:
#
# time_dim = 0
#
# batch_dim = 1
#
#
#
# def _reverse(input_, seq_lengths, seq_dim, batch_dim):
#
# if seq_lengths is not None:
#
# return array_ops.reverse_sequence(
#
# input=input_, seq_lengths=seq_lengths,
#
# seq_dim=seq_dim, batch_dim=batch_dim)
#
# else:
#
# return array_ops.reverse(input_, axis=[seq_dim])
#
#
#
# with vs.variable_scope("bw") as bw_scope:
#
# inputs_reverse = _reverse(
#
# inputs, seq_lengths=sequence_length,
#
# seq_dim=time_dim, batch_dim=batch_dim)
#
# tmp, output_state_bw = dynamic_rnn(
#
# cell=cell_bw, inputs=inputs_reverse, sequence_length=sequence_length,
#
# initial_state=initial_state_bw, dtype=dtype,
#
# parallel_iterations=parallel_iterations, swap_memory=swap_memory,
#
# time_major=time_major, scope=bw_scope)
#
#
#
# output_bw = _reverse(
#
# tmp, seq_lengths=sequence_length,
#
# seq_dim=time_dim, batch_dim=batch_dim)
#
#
#
# outputs = (output_fw, output_bw)
#
# output_states = (output_state_fw, output_state_bw)
#
#
#
# return (outputs, output_states)
#
def dynamic_rnn(cell, inputs, att_scores=None, sequence_length=None, initial_state=None,
dtype=None, parallel_iterations=None, swap_memory=False,
time_major=False, scope=None):
"""Creates a recurrent neural network specified by RNNCell `cell`.
Performs fully dynamic unrolling of `inputs`.
Example:
```python
# create a BasicRNNCell
rnn_cell = tf.nn.rnn_cell.BasicRNNCell(hidden_size)
# 'outputs' is a tensor of shape [batch_size, max_time, cell_state_size]
# defining initial state
initial_state = rnn_cell.zero_state(batch_size, dtype=tf.float32)
# 'state' is a tensor of shape [batch_size, cell_state_size]
outputs, state = tf.nn.dynamic_rnn(rnn_cell, input_data,
initial_state=initial_state,
dtype=tf.float32)
```
```python
# create 2 LSTMCells
rnn_layers = [tf.nn.rnn_cell.LSTMCell(size) for size in [128, 256]]
# create a RNN cell composed sequentially of a number of RNNCells
multi_rnn_cell = tf.nn.rnn_cell.MultiRNNCell(rnn_layers)
# 'outputs' is a tensor of shape [batch_size, max_time, 256]
# 'state' is a N-tuple where N is the number of LSTMCells containing a
# tf.contrib.rnn.LSTMStateTuple for each cell
outputs, state = tf.nn.dynamic_rnn(cell=multi_rnn_cell,
inputs=data,
dtype=tf.float32)
```
Args:
cell: An instance of RNNCell.
inputs: The RNN inputs.
If `time_major == False` (default), this must be a `Tensor` of shape:
`[batch_size, max_time, ...]`, or a nested tuple of such
elements.
If `time_major == True`, this must be a `Tensor` of shape:
`[max_time, batch_size, ...]`, or a nested tuple of such
elements.
This may also be a (possibly nested) tuple of Tensors satisfying
this property. The first two dimensions must match across all the inputs,
but otherwise the ranks and other shape components may differ.
In this case, input to `cell` at each time-step will replicate the
structure of these tuples, except for the time dimension (from which the
time is taken).
The input to `cell` at each time step will be a `Tensor` or (possibly
nested) tuple of Tensors each with dimensions `[batch_size, ...]`.
sequence_length: (optional) An int32/int64 vector sized `[batch_size]`.
Used to copy-through state and zero-out outputs when past a batch
element's sequence length. So it's more for correctness than performance.
initial_state: (optional) An initial state for the RNN.
If `cell.state_size` is an integer, this must be
a `Tensor` of appropriate type and shape `[batch_size, cell.state_size]`.
If `cell.state_size` is a tuple, this should be a tuple of
tensors having shapes `[batch_size, s] for s in cell.state_size`.
dtype: (optional) The data type for the initial state and expected output.
Required if initial_state is not provided or RNN state has a heterogeneous
dtype.
parallel_iterations: (Default: 32). The number of iterations to run in
parallel. Those operations which do not have any temporal dependency
and can be run in parallel, will be. This parameter trades off
time for space. Values >> 1 use more memory but take less time,
while smaller values use less memory but computations take longer.
swap_memory: Transparently swap the tensors produced in forward inference
but needed for back prop from GPU to CPU. This allows training RNNs
which would typically not fit on a single GPU, with very minimal (or no)
performance penalty.
time_major: The shape format of the `inputs` and `outputs` Tensors.
If true, these `Tensors` must be shaped `[max_time, batch_size, depth]`.
If false, these `Tensors` must be shaped `[batch_size, max_time, depth]`.
Using `time_major = True` is a bit more efficient because it avoids
transposes at the beginning and end of the RNN calculation. However,
most TensorFlow data is batch-major, so by default this function
accepts input and emits output in batch-major form.
scope: VariableScope for the created subgraph; defaults to "rnn".
Returns:
A pair (outputs, state) where:
outputs: The RNN output `Tensor`.
If time_major == False (default), this will be a `Tensor` shaped:
`[batch_size, max_time, cell.output_size]`.
If time_major == True, this will be a `Tensor` shaped:
`[max_time, batch_size, cell.output_size]`.
Note, if `cell.output_size` is a (possibly nested) tuple of integers
or `TensorShape` objects, then `outputs` will be a tuple having the
same structure as `cell.output_size`, containing Tensors having shapes
corresponding to the shape data in `cell.output_size`.
state: The final state. If `cell.state_size` is an int, this
will be shaped `[batch_size, cell.state_size]`. If it is a
`TensorShape`, this will be shaped `[batch_size] + cell.state_size`.
If it is a (possibly nested) tuple of ints or `TensorShape`, this will
be a tuple having the corresponding shapes. If cells are `LSTMCells`
`state` will be a tuple containing a `LSTMStateTuple` for each cell.
Raises:
TypeError: If `cell` is not an instance of RNNCell.
ValueError: If inputs is None or an empty list.
"""
if not _like_rnncell(cell):
raise TypeError("cell must be an instance of RNNCell")
# By default, time_major==False and inputs are batch-major: shaped
# [batch, time, depth]
# For internal calculations, we transpose to [time, batch, depth]
flat_input = nest.flatten(inputs)
if not time_major:
# (B,T,D) => (T,B,D)
flat_input = [ops.convert_to_tensor(input_) for input_ in flat_input]
flat_input = tuple(_transpose_batch_time(input_) for input_ in flat_input)
parallel_iterations = parallel_iterations or 32
if sequence_length is not None:
sequence_length = math_ops.to_int32(sequence_length)
if sequence_length.get_shape().ndims not in (None, 1):
raise ValueError(
"sequence_length must be a vector of length batch_size, "
"but saw shape: %s" % sequence_length.get_shape())
sequence_length = array_ops.identity( # Just to find it in the graph.
sequence_length, name="sequence_length")
# Create a new scope in which the caching device is either
# determined by the parent scope, or is set to place the cached
# Variable using the same placement as for the rest of the RNN.
try:
resue = tf.AUTO_REUSE
except:
resue = tf.compat.v1.AUTO_REUSE
with vs.variable_scope(scope or "rnn",reuse=resue) as varscope:#TODO:user defined reuse
if varscope.caching_device is None:
varscope.set_caching_device(lambda op: op.device)
batch_size = _best_effort_input_batch_size(flat_input)
if initial_state is not None:
state = initial_state
else:
if not dtype:
raise ValueError("If there is no initial_state, you must give a dtype.")
state = cell.zero_state(batch_size, dtype)
def _assert_has_shape(x, shape):
x_shape = array_ops.shape(x)
packed_shape = array_ops.stack(shape)
return control_flow_ops.Assert(
math_ops.reduce_all(math_ops.equal(x_shape, packed_shape)),
["Expected shape for Tensor %s is " % x.name,
packed_shape, " but saw shape: ", x_shape])
if sequence_length is not None:
# Perform some shape validation
with ops.control_dependencies(
[_assert_has_shape(sequence_length, [batch_size])]):
sequence_length = array_ops.identity(
sequence_length, name="CheckSeqLen")
inputs = nest.pack_sequence_as(structure=inputs, flat_sequence=flat_input)
(outputs, final_state) = _dynamic_rnn_loop(
cell,
inputs,
state,
parallel_iterations=parallel_iterations,
swap_memory=swap_memory,
att_scores=att_scores,
sequence_length=sequence_length,
dtype=dtype)
# Outputs of _dynamic_rnn_loop are always shaped [time, batch, depth].
# If we are performing batch-major calculations, transpose output back
# to shape [batch, time, depth]
if not time_major:
# (T,B,D) => (B,T,D)
outputs = nest.map_structure(_transpose_batch_time, outputs)
return (outputs, final_state)
def _dynamic_rnn_loop(cell,
inputs,
initial_state,
parallel_iterations,
swap_memory,
att_scores=None,
sequence_length=None,
dtype=None):
"""Internal implementation of Dynamic RNN.
Args:
cell: An instance of RNNCell.
inputs: A `Tensor` of shape [time, batch_size, input_size], or a nested
tuple of such elements.
initial_state: A `Tensor` of shape `[batch_size, state_size]`, or if
`cell.state_size` is a tuple, then this should be a tuple of
tensors having shapes `[batch_size, s] for s in cell.state_size`.
parallel_iterations: Positive Python int.
swap_memory: A Python boolean
sequence_length: (optional) An `int32` `Tensor` of shape [batch_size].
dtype: (optional) Expected dtype of output. If not specified, inferred from
initial_state.
Returns:
Tuple `(final_outputs, final_state)`.
final_outputs:
A `Tensor` of shape `[time, batch_size, cell.output_size]`. If
`cell.output_size` is a (possibly nested) tuple of ints or `TensorShape`
objects, then this returns a (possibly nsted) tuple of Tensors matching
the corresponding shapes.
final_state:
A `Tensor`, or possibly nested tuple of Tensors, matching in length
and shapes to `initial_state`.
Raises:
ValueError: If the input depth cannot be inferred via shape inference
from the inputs.
"""
state = initial_state
assert isinstance(parallel_iterations, int), "parallel_iterations must be int"
state_size = cell.state_size
flat_input = nest.flatten(inputs)
flat_output_size = nest.flatten(cell.output_size)
# Construct an initial output
input_shape = array_ops.shape(flat_input[0])
time_steps = input_shape[0]
batch_size = _best_effort_input_batch_size(flat_input)
inputs_got_shape = tuple(input_.get_shape().with_rank_at_least(3)
for input_ in flat_input)
const_time_steps, const_batch_size = inputs_got_shape[0].as_list()[:2]
for shape in inputs_got_shape:
if not shape[2:].is_fully_defined():
raise ValueError(
"Input size (depth of inputs) must be accessible via shape inference,"
" but saw value None.")
got_time_steps = shape[0]
got_batch_size = shape[1]
if const_time_steps != got_time_steps:
raise ValueError(
"Time steps is not the same for all the elements in the input in a "
"batch.")
if const_batch_size != got_batch_size:
raise ValueError(
"Batch_size is not the same for all the elements in the input.")
# Prepare dynamic conditional copying of state & output
def _create_zero_arrays(size):
size = _concat(batch_size, size)
return array_ops.zeros(
array_ops.stack(size), _infer_state_dtype(dtype, state))
flat_zero_output = tuple(_create_zero_arrays(output)
for output in flat_output_size)
zero_output = nest.pack_sequence_as(structure=cell.output_size,
flat_sequence=flat_zero_output)
if sequence_length is not None:
min_sequence_length = math_ops.reduce_min(sequence_length)
max_sequence_length = math_ops.reduce_max(sequence_length)
time = array_ops.constant(0, dtype=dtypes.int32, name="time")
with ops.name_scope("dynamic_rnn") as scope:
base_name = scope
def _create_ta(name, dtype):
return tensor_array_ops.TensorArray(dtype=dtype,
size=time_steps,
tensor_array_name=base_name + name)
output_ta = tuple(_create_ta("output_%d" % i,
_infer_state_dtype(dtype, state))
for i in range(len(flat_output_size)))
input_ta = tuple(_create_ta("input_%d" % i, flat_input[i].dtype)
for i in range(len(flat_input)))
input_ta = tuple(ta.unstack(input_)
for ta, input_ in zip(input_ta, flat_input))
def _time_step(time, output_ta_t, state, att_scores=None):
"""Take a time step of the dynamic RNN.
Args:
time: int32 scalar Tensor.
output_ta_t: List of `TensorArray`s that represent the output.
state: nested tuple of vector tensors that represent the state.
Returns:
The tuple (time + 1, output_ta_t with updated flow, new_state).
"""
input_t = tuple(ta.read(time) for ta in input_ta)
# Restore some shape information
for input_, shape in zip(input_t, inputs_got_shape):
input_.set_shape(shape[1:])
input_t = nest.pack_sequence_as(structure=inputs, flat_sequence=input_t)
if att_scores is not None:
att_score = att_scores[:, time, :]
call_cell = lambda: cell(input_t, state, att_score)
else:
call_cell = lambda: cell(input_t, state)
if sequence_length is not None:
(output, new_state) = _rnn_step(
time=time,
sequence_length=sequence_length,
min_sequence_length=min_sequence_length,
max_sequence_length=max_sequence_length,
zero_output=zero_output,
state=state,
call_cell=call_cell,
state_size=state_size,
skip_conditionals=True)
else:
(output, new_state) = call_cell()
# Pack state if using state tuples
output = nest.flatten(output)
output_ta_t = tuple(
ta.write(time, out) for ta, out in zip(output_ta_t, output))
if att_scores is not None:
return (time + 1, output_ta_t, new_state, att_scores)
else:
return (time + 1, output_ta_t, new_state)
if att_scores is not None:
_, output_final_ta, final_state, _ = control_flow_ops.while_loop(
cond=lambda time, *_: time < time_steps,
body=_time_step,
loop_vars=(time, output_ta, state, att_scores),
parallel_iterations=parallel_iterations,
swap_memory=swap_memory)
else:
_, output_final_ta, final_state = control_flow_ops.while_loop(
cond=lambda time, *_: time < time_steps,
body=_time_step,
loop_vars=(time, output_ta, state),
parallel_iterations=parallel_iterations,
swap_memory=swap_memory)
# Unpack final output if not using output tuples.
final_outputs = tuple(ta.stack() for ta in output_final_ta)
# Restore some shape information
for output, output_size in zip(final_outputs, flat_output_size):
shape = _concat(
[const_time_steps, const_batch_size], output_size, static=True)
output.set_shape(shape)
final_outputs = nest.pack_sequence_as(
structure=cell.output_size, flat_sequence=final_outputs)
return (final_outputs, final_state)
================================================
FILE: deepctr/contrib/utils.py
================================================
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import init_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import nn_ops
from tensorflow.python.ops import variable_scope as vs
from tensorflow.python.ops.rnn_cell import *
from tensorflow.python.util import nest
_BIAS_VARIABLE_NAME = "bias"
_WEIGHTS_VARIABLE_NAME = "kernel"
class _Linear_(object):
"""Linear map: sum_i(args[i] * W[i]), where W[i] is a variable.
Args:
args: a 2D Tensor or a list of 2D, batch x n, Tensors.
output_size: int, second dimension of weight variable.
dtype: data type for variables.
build_bias: boolean, whether to build a bias variable.
bias_initializer: starting value to initialize the bias
(default is all zeros).
kernel_initializer: starting value to initialize the weight.
Raises:
ValueError: if inputs_shape is wrong.
"""
def __init__(self,
args,
output_size,
build_bias,
bias_initializer=None,
kernel_initializer=None):
self._build_bias = build_bias
if args is None or (nest.is_sequence(args) and not args):
raise ValueError("`args` must be specified")
if not nest.is_sequence(args):
args = [args]
self._is_sequence = False
else:
self._is_sequence = True
# Calculate the total size of arguments on dimension 1.
total_arg_size = 0
shapes = [a.get_shape() for a in args]
for shape in shapes:
if shape.ndims != 2:
raise ValueError(
"linear is expecting 2D arguments: %s" % shapes)
if shape[1] is None:
raise ValueError("linear expects shape[1] to be provided for shape %s, "
"but saw %s" % (shape, shape[1]))
else:
total_arg_size += int(shape[1])#.value
dtype = [a.dtype for a in args][0]
scope = vs.get_variable_scope()
with vs.variable_scope(scope) as outer_scope:
self._weights = vs.get_variable(
_WEIGHTS_VARIABLE_NAME, [total_arg_size, output_size],
dtype=dtype,
initializer=kernel_initializer)
if build_bias:
with vs.variable_scope(outer_scope) as inner_scope:
inner_scope.set_partitioner(None)
if bias_initializer is None:
bias_initializer = init_ops.constant_initializer(
0.0, dtype=dtype)
self._biases = vs.get_variable(
_BIAS_VARIABLE_NAME, [output_size],
dtype=dtype,
initializer=bias_initializer)
def __call__(self, args):
if not self._is_sequence:
args = [args]
if len(args) == 1:
res = math_ops.matmul(args[0], self._weights)
else:
res = math_ops.matmul(array_ops.concat(args, 1), self._weights)
if self._build_bias:
res = nn_ops.bias_add(res, self._biases)
return res
try:
from tensorflow.python.ops.rnn_cell_impl import _Linear
except:
_Linear = _Linear_
class QAAttGRUCell(RNNCell):
"""Gated Recurrent Unit cell (cf. http://arxiv.org/abs/1406.1078).
Args:
num_units: int, The number of units in the GRU cell.
activation: Nonlinearity to use. Default: `tanh`.
reuse: (optional) Python boolean describing whether to reuse variables
in an existing scope. If not `True`, and the existing scope already has
the given variables, an error is raised.
kernel_initializer: (optional) The initializer to use for the weight and
projection matrices.
bias_initializer: (optional) The initializer to use for the bias.
"""
def __init__(self,
num_units,
activation=None,
reuse=None,
kernel_initializer=None,
bias_initializer=None):
super(QAAttGRUCell, self).__init__(_reuse=reuse)
self._num_units = num_units
self._activation = activation or math_ops.tanh
self._kernel_initializer = kernel_initializer
self._bias_initializer = bias_initializer
self._gate_linear = None
self._candidate_linear = None
@property
def state_size(self):
return self._num_units
@property
def output_size(self):
return self._num_units
def __call__(self, inputs, state, att_score):
return self.call(inputs, state, att_score)
def call(self, inputs, state, att_score=None):
"""Gated recurrent unit (GRU) with nunits cells."""
if self._gate_linear is None:
bias_ones = self._bias_initializer
if self._bias_initializer is None:
bias_ones = init_ops.constant_initializer(
1.0, dtype=inputs.dtype)
with vs.variable_scope("gates"): # Reset gate and update gate.
self._gate_linear = _Linear(
[inputs, state],
2 * self._num_units,
True,
bias_initializer=bias_ones,
kernel_initializer=self._kernel_initializer)
value = math_ops.sigmoid(self._gate_linear([inputs, state]))
r, u = array_ops.split(value=value, num_or_size_splits=2, axis=1)
r_state = r * state
if self._candidate_linear is None:
with vs.variable_scope("candidate"):
self._candidate_linear = _Linear(
[inputs, r_state],
self._num_units,
True,
bias_initializer=self._bias_initializer,
kernel_initializer=self._kernel_initializer)
c = self._activation(self._candidate_linear([inputs, r_state]))
new_h = (1. - att_score) * state + att_score * c
return new_h, new_h
class VecAttGRUCell(RNNCell):
"""Gated Recurrent Unit cell (cf. http://arxiv.org/abs/1406.1078).
Args:
num_units: int, The number of units in the GRU cell.
activation: Nonlinearity to use. Default: `tanh`.
reuse: (optional) Python boolean describing whether to reuse variables
in an existing scope. If not `True`, and the existing scope already has
the given variables, an error is raised.
kernel_initializer: (optional) The initializer to use for the weight and
projection matrices.
bias_initializer: (optional) The initializer to use for the bias.
"""
def __init__(self,
num_units,
activation=None,
reuse=None,
kernel_initializer=None,
bias_initializer=None):
super(VecAttGRUCell, self).__init__(_reuse=reuse)
self._num_units = num_units
self._activation = activation or math_ops.tanh
self._kernel_initializer = kernel_initializer
self._bias_initializer = bias_initializer
self._gate_linear = None
self._candidate_linear = None
@property
def state_size(self):
return self._num_units
@property
def output_size(self):
return self._num_units
def __call__(self, inputs, state, att_score):
return self.call(inputs, state, att_score)
def call(self, inputs, state, att_score=None):
"""Gated recurrent unit (GRU) with nunits cells."""
if self._gate_linear is None:
bias_ones = self._bias_initializer
if self._bias_initializer is None:
bias_ones = init_ops.constant_initializer(
1.0, dtype=inputs.dtype)
with vs.variable_scope("gates"): # Reset gate and update gate.
self._gate_linear = _Linear(
[inputs, state],
2 * self._num_units,
True,
bias_initializer=bias_ones,
kernel_initializer=self._kernel_initializer)
value = math_ops.sigmoid(self._gate_linear([inputs, state]))
r, u = array_ops.split(value=value, num_or_size_splits=2, axis=1)
r_state = r * state
if self._candidate_linear is None:
with vs.variable_scope("candidate"):
self._candidate_linear = _Linear(
[inputs, r_state],
self._num_units,
True,
bias_initializer=self._bias_initializer,
kernel_initializer=self._kernel_initializer)
c = self._activation(self._candidate_linear([inputs, r_state]))
u = (1.0 - att_score) * u
new_h = u * state + (1 - u) * c
return new_h, new_h
================================================
FILE: deepctr/estimator/__init__.py
================================================
from .models import *
================================================
FILE: deepctr/estimator/feature_column.py
================================================
import tensorflow as tf
from tensorflow.python.feature_column.feature_column import _EmbeddingColumn
from .utils import LINEAR_SCOPE_NAME, variable_scope, get_collection, get_GraphKeys, input_layer, get_losses
def linear_model(features, linear_feature_columns):
if tf.__version__ >= '2.0.0':
linear_logits = tf.compat.v1.feature_column.linear_model(features, linear_feature_columns)
else:
linear_logits = tf.feature_column.linear_model(features, linear_feature_columns)
return linear_logits
def get_linear_logit(features, linear_feature_columns, l2_reg_linear=0):
with variable_scope(LINEAR_SCOPE_NAME):
if not linear_feature_columns:
linear_logits = tf.Variable([[0.0]], name='bias_weights')
else:
linear_logits = linear_model(features, linear_feature_columns)
if l2_reg_linear > 0:
for var in get_collection(get_GraphKeys().TRAINABLE_VARIABLES, LINEAR_SCOPE_NAME)[:-1]:
get_losses().add_loss(l2_reg_linear * tf.nn.l2_loss(var, name=var.name.split(":")[0] + "_l2loss"),
get_GraphKeys().REGULARIZATION_LOSSES)
return linear_logits
def input_from_feature_columns(features, feature_columns, l2_reg_embedding=0.0):
dense_value_list = []
sparse_emb_list = []
for feat in feature_columns:
if is_embedding(feat):
sparse_emb = tf.expand_dims(input_layer(features, [feat]), axis=1)
sparse_emb_list.append(sparse_emb)
if l2_reg_embedding > 0:
get_losses().add_loss(l2_reg_embedding * tf.nn.l2_loss(sparse_emb, name=feat.name + "_l2loss"),
get_GraphKeys().REGULARIZATION_LOSSES)
else:
dense_value_list.append(input_layer(features, [feat]))
return sparse_emb_list, dense_value_list
def is_embedding(feature_column):
try:
from tensorflow.python.feature_column.feature_column_v2 import EmbeddingColumn
except ImportError:
EmbeddingColumn = _EmbeddingColumn
return isinstance(feature_column, (_EmbeddingColumn, EmbeddingColumn))
================================================
FILE: deepctr/estimator/inputs.py
================================================
import tensorflow as tf
def input_fn_pandas(df, features, label=None, batch_size=256, num_epochs=1, shuffle=False, queue_capacity_factor=10,
num_threads=1):
if label is not None:
y = df[label]
else:
y = None
if tf.__version__ >= "2.0.0":
return tf.compat.v1.estimator.inputs.pandas_input_fn(df[features], y, batch_size=batch_size,
num_epochs=num_epochs,
shuffle=shuffle,
queue_capacity=batch_size * queue_capacity_factor,
num_threads=num_threads)
return tf.estimator.inputs.pandas_input_fn(df[features], y, batch_size=batch_size, num_epochs=num_epochs,
shuffle=shuffle, queue_capacity=batch_size * queue_capacity_factor,
num_threads=num_threads)
def input_fn_tfrecord(filenames, feature_description, label=None, batch_size=256, num_epochs=1, num_parallel_calls=8,
shuffle_factor=10, prefetch_factor=1,
):
def _parse_examples(serial_exmp):
try:
features = tf.parse_single_example(serial_exmp, features=feature_description)
except AttributeError:
features = tf.io.parse_single_example(serial_exmp, features=feature_description)
if label is not None:
labels = features.pop(label)
return features, labels
return features
def input_fn():
dataset = tf.data.TFRecordDataset(filenames)
dataset = dataset.map(_parse_examples, num_parallel_calls=num_parallel_calls)
if shuffle_factor > 0:
dataset = dataset.shuffle(buffer_size=batch_size * shuffle_factor)
dataset = dataset.repeat(num_epochs).batch(batch_size)
if prefetch_factor > 0:
dataset = dataset.prefetch(buffer_size=batch_size * prefetch_factor)
try:
iterator = dataset.make_one_shot_iterator()
except AttributeError:
iterator = tf.compat.v1.data.make_one_shot_iterator(dataset)
return iterator.get_next()
return input_fn
================================================
FILE: deepctr/estimator/models/__init__.py
================================================
from .afm import AFMEstimator
from .autoint import AutoIntEstimator
from .ccpm import CCPMEstimator
from .dcn import DCNEstimator
from .deepfm import DeepFMEstimator
from .fwfm import FwFMEstimator
from .fibinet import FiBiNETEstimator
from .fnn import FNNEstimator
from .nfm import NFMEstimator
from .pnn import PNNEstimator
from .wdl import WDLEstimator
from .xdeepfm import xDeepFMEstimator
from .deepfefm import DeepFEFMEstimator
================================================
FILE: deepctr/estimator/models/afm.py
================================================
# -*- coding:utf-8 -*-
"""
Author:
Weichen Shen, weichenswc@163.com
Reference:
[1] Xiao J, Ye H, He X, et al. Attentional factorization machines: Learning the weight of feature interactions via attention networks[J]. arXiv preprint arXiv:1708.04617, 2017.
(https://arxiv.org/abs/1708.04617)
"""
import tensorflow as tf
from ..feature_column import get_linear_logit, input_from_feature_columns
from ..utils import deepctr_model_fn, DNN_SCOPE_NAME, variable_scope
from ...layers.interaction import AFMLayer, FM
from ...layers.utils import concat_func
def AFMEstimator(linear_feature_columns, dnn_feature_columns, use_attention=True, attention_factor=8,
l2_reg_linear=1e-5, l2_reg_embedding=1e-5, l2_reg_att=1e-5, afm_dropout=0, seed=1024,
task='binary', model_dir=None, config=None, linear_optimizer='Ftrl',
dnn_optimizer='Adagrad', training_chief_hooks=None):
"""Instantiates the Attentional Factorization Machine architecture.
:param linear_feature_columns: An iterable containing all the features used by linear part of the model.
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
:param use_attention: bool,whether use attention or not,if set to ``False``.it is the same as **standard Factorization Machine**
:param attention_factor: positive integer,units in attention net
:param l2_reg_linear: float. L2 regularizer strength applied to linear part
:param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
:param l2_reg_att: float. L2 regularizer strength applied to attention net
:param afm_dropout: float in [0,1), Fraction of the attention net output units to dropout.
:param seed: integer ,to use as random seed.
:param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
:param model_dir: Directory to save model parameters, graph and etc. This can
also be used to load checkpoints from the directory into a estimator
to continue training a previously saved model.
:param config: tf.RunConfig object to configure the runtime settings.
:param linear_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the linear part of the model. Defaults to FTRL optimizer.
:param dnn_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the deep part of the model. Defaults to Adagrad optimizer.
:param training_chief_hooks: Iterable of `tf.train.SessionRunHook` objects to
run on the chief worker during training.
:return: A Tensorflow Estimator instance.
"""
def _model_fn(features, labels, mode, config):
train_flag = (mode == tf.estimator.ModeKeys.TRAIN)
linear_logits = get_linear_logit(features, linear_feature_columns, l2_reg_linear=l2_reg_linear)
with variable_scope(DNN_SCOPE_NAME):
sparse_embedding_list, _ = input_from_feature_columns(features, dnn_feature_columns,
l2_reg_embedding=l2_reg_embedding)
if use_attention:
fm_logit = AFMLayer(attention_factor, l2_reg_att, afm_dropout,
seed)(sparse_embedding_list, training=train_flag)
else:
fm_logit = FM()(concat_func(sparse_embedding_list, axis=1))
logits = linear_logits + fm_logit
return deepctr_model_fn(features, mode, logits, labels, task, linear_optimizer, dnn_optimizer,
training_chief_hooks=training_chief_hooks)
return tf.estimator.Estimator(_model_fn, model_dir=model_dir, config=config)
================================================
FILE: deepctr/estimator/models/autoint.py
================================================
# -*- coding:utf-8 -*-
"""
Author:
Weichen Shen, weichenswc@163.com
Reference:
[1] Song W, Shi C, Xiao Z, et al. AutoInt: Automatic Feature Interaction Learning via Self-Attentive Neural Networks[J]. arXiv preprint arXiv:1810.11921, 2018.(https://arxiv.org/abs/1810.11921)
"""
import tensorflow as tf
from ..feature_column import get_linear_logit, input_from_feature_columns
from ..utils import deepctr_model_fn, DNN_SCOPE_NAME, variable_scope
from ...layers.core import DNN
from ...layers.interaction import InteractingLayer
from ...layers.utils import concat_func, combined_dnn_input
def AutoIntEstimator(linear_feature_columns, dnn_feature_columns, att_layer_num=3, att_embedding_size=8, att_head_num=2,
att_res=True,
dnn_hidden_units=(256, 128, 64), dnn_activation='relu', l2_reg_linear=1e-5,
l2_reg_embedding=1e-5, l2_reg_dnn=0, dnn_use_bn=False, dnn_dropout=0, seed=1024,
task='binary', model_dir=None, config=None, linear_optimizer='Ftrl',
dnn_optimizer='Adagrad', training_chief_hooks=None):
"""Instantiates the AutoInt Network architecture.
:param linear_feature_columns: An iterable containing all the features used by linear part of the model.
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
:param att_layer_num: int.The InteractingLayer number to be used.
:param att_embedding_size: int.The embedding size in multi-head self-attention network.
:param att_head_num: int.The head number in multi-head self-attention network.
:param att_res: bool.Whether or not use standard residual connections before output.
:param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of DNN
:param dnn_activation: Activation function to use in DNN
:param l2_reg_linear: float. L2 regularizer strength applied to linear part
:param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
:param l2_reg_dnn: float. L2 regularizer strength applied to DNN
:param dnn_use_bn: bool. Whether use BatchNormalization before activation or not in DNN
:param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
:param seed: integer ,to use as random seed.
:param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
:param model_dir: Directory to save model parameters, graph and etc. This can
also be used to load checkpoints from the directory into a estimator
to continue training a previously saved model.
:param config: tf.RunConfig object to configure the runtime settings.
:param linear_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the linear part of the model. Defaults to FTRL optimizer.
:param dnn_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the deep part of the model. Defaults to Adagrad optimizer.
:param training_chief_hooks: Iterable of `tf.train.SessionRunHook` objects to
run on the chief worker during training.
:return: A Tensorflow Estimator instance.
"""
def _model_fn(features, labels, mode, config):
train_flag = (mode == tf.estimator.ModeKeys.TRAIN)
linear_logits = get_linear_logit(features, linear_feature_columns, l2_reg_linear=l2_reg_linear)
with variable_scope(DNN_SCOPE_NAME):
sparse_embedding_list, dense_value_list = input_from_feature_columns(features, dnn_feature_columns,
l2_reg_embedding=l2_reg_embedding)
att_input = concat_func(sparse_embedding_list, axis=1)
for _ in range(att_layer_num):
att_input = InteractingLayer(
att_embedding_size, att_head_num, att_res)(att_input)
att_output = tf.keras.layers.Flatten()(att_input)
dnn_input = combined_dnn_input(sparse_embedding_list, dense_value_list)
if len(dnn_hidden_units) > 0 and att_layer_num > 0: # Deep & Interacting Layer
deep_out = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout, dnn_use_bn, seed=seed)(dnn_input, training=train_flag)
stack_out = tf.keras.layers.Concatenate()([att_output, deep_out])
final_logit = tf.keras.layers.Dense(
1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed))(stack_out)
elif len(dnn_hidden_units) > 0: # Only Deep
deep_out = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout, dnn_use_bn, seed=seed)(dnn_input, training=train_flag)
final_logit = tf.keras.layers.Dense(
1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed))(deep_out)
elif att_layer_num > 0: # Only Interacting Layer
final_logit = tf.keras.layers.Dense(
1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed))(att_output)
else: # Error
raise NotImplementedError
logits = linear_logits + final_logit
return deepctr_model_fn(features, mode, logits, labels, task, linear_optimizer, dnn_optimizer,
training_chief_hooks=training_chief_hooks)
return tf.estimator.Estimator(_model_fn, model_dir=model_dir, config=config)
================================================
FILE: deepctr/estimator/models/ccpm.py
================================================
# -*- coding:utf-8 -*-
"""
Author:
Weichen Shen, weichenswc@163.com
Reference:
[1] Liu Q, Yu F, Wu S, et al. A convolutional click prediction model[C]//Proceedings of the 24th ACM International on Conference on Information and Knowledge Management. ACM, 2015: 1743-1746.
(http://ir.ia.ac.cn/bitstream/173211/12337/1/A%20Convolutional%20Click%20Prediction%20Model.pdf)
"""
import tensorflow as tf
from ..feature_column import get_linear_logit, input_from_feature_columns
from ..utils import deepctr_model_fn, DNN_SCOPE_NAME, variable_scope
from ...layers.core import DNN
from ...layers.sequence import KMaxPooling
from ...layers.utils import concat_func
def CCPMEstimator(linear_feature_columns, dnn_feature_columns, conv_kernel_width=(6, 5), conv_filters=(4, 4),
dnn_hidden_units=(128, 64), l2_reg_linear=1e-5, l2_reg_embedding=1e-5, l2_reg_dnn=0, dnn_dropout=0,
seed=1024, task='binary', model_dir=None, config=None, linear_optimizer='Ftrl',
dnn_optimizer='Adagrad', training_chief_hooks=None):
"""Instantiates the Convolutional Click Prediction Model architecture.
:param linear_feature_columns: An iterable containing all the features used by linear part of the model.
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
:param conv_kernel_width: list,list of positive integer or empty list,the width of filter in each conv layer.
:param conv_filters: list,list of positive integer or empty list,the number of filters in each conv layer.
:param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of DNN.
:param l2_reg_linear: float. L2 regularizer strength applied to linear part
:param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
:param l2_reg_dnn: float. L2 regularizer strength applied to DNN
:param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
:param init_std: float,to use as the initialize std of embedding vector
:param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
:param model_dir: Directory to save model parameters, graph and etc. This can
also be used to load checkpoints from the directory into a estimator
to continue training a previously saved model.
:param config: tf.RunConfig object to configure the runtime settings.
:param linear_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the linear part of the model. Defaults to FTRL optimizer.
:param dnn_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the deep part of the model. Defaults to Adagrad optimizer.
:param training_chief_hooks: Iterable of `tf.train.SessionRunHook` objects to
run on the chief worker during training.
:return: A Tensorflow Estimator instance.
"""
if len(conv_kernel_width) != len(conv_filters):
raise ValueError(
"conv_kernel_width must have same element with conv_filters")
def _model_fn(features, labels, mode, config):
train_flag = (mode == tf.estimator.ModeKeys.TRAIN)
linear_logits = get_linear_logit(features, linear_feature_columns, l2_reg_linear=l2_reg_linear)
with variable_scope(DNN_SCOPE_NAME):
sparse_embedding_list, _ = input_from_feature_columns(features, dnn_feature_columns,
l2_reg_embedding=l2_reg_embedding)
n = len(sparse_embedding_list)
l = len(conv_filters)
conv_input = concat_func(sparse_embedding_list, axis=1)
pooling_result = tf.keras.layers.Lambda(
lambda x: tf.expand_dims(x, axis=3))(conv_input)
for i in range(1, l + 1):
filters = conv_filters[i - 1]
width = conv_kernel_width[i - 1]
k = max(1, int((1 - pow(i / l, l - i)) * n)) if i < l else 3
conv_result = tf.keras.layers.Conv2D(filters=filters, kernel_size=(width, 1), strides=(1, 1),
padding='same',
activation='tanh', use_bias=True, )(pooling_result)
pooling_result = KMaxPooling(
k=min(k, int(conv_result.shape[1])), axis=1)(conv_result)
flatten_result = tf.keras.layers.Flatten()(pooling_result)
dnn_out = DNN(dnn_hidden_units, l2_reg=l2_reg_dnn, dropout_rate=dnn_dropout, seed=seed)(flatten_result, training=train_flag)
dnn_logit = tf.keras.layers.Dense(1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed))(dnn_out)
logits = linear_logits + dnn_logit
return deepctr_model_fn(features, mode, logits, labels, task, linear_optimizer, dnn_optimizer,
training_chief_hooks=training_chief_hooks
)
return tf.estimator.Estimator(_model_fn, model_dir=model_dir, config=config)
================================================
FILE: deepctr/estimator/models/dcn.py
================================================
# -*- coding:utf-8 -*-
"""
Author:
Weichen Shen, weichenswc@163.com
Reference:
[1] Wang R, Fu B, Fu G, et al. Deep & cross network for ad click predictions[C]//Proceedings of the ADKDD'17. ACM, 2017: 12. (https://arxiv.org/abs/1708.05123)
"""
import tensorflow as tf
from ..feature_column import get_linear_logit, input_from_feature_columns
from ..utils import deepctr_model_fn, DNN_SCOPE_NAME, variable_scope
from ...layers.core import DNN
from ...layers.interaction import CrossNet
from ...layers.utils import combined_dnn_input
def DCNEstimator(linear_feature_columns, dnn_feature_columns, cross_num=2, dnn_hidden_units=(256, 128, 64),
l2_reg_linear=1e-5,
l2_reg_embedding=1e-5,
l2_reg_cross=1e-5, l2_reg_dnn=0, seed=1024, dnn_dropout=0, dnn_use_bn=False,
dnn_activation='relu', task='binary', model_dir=None, config=None, linear_optimizer='Ftrl',
dnn_optimizer='Adagrad', training_chief_hooks=None):
"""Instantiates the Deep&Cross Network architecture.
:param linear_feature_columns: An iterable containing all the features used by linear part of the model.
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
:param cross_num: positive integet,cross layer number
:param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of DNN
:param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
:param l2_reg_cross: float. L2 regularizer strength applied to cross net
:param l2_reg_dnn: float. L2 regularizer strength applied to DNN
:param seed: integer ,to use as random seed.
:param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
:param dnn_use_bn: bool. Whether use BatchNormalization before activation or not DNN
:param dnn_activation: Activation function to use in DNN
:param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
:param model_dir: Directory to save model parameters, graph and etc. This can
also be used to load checkpoints from the directory into a estimator
to continue training a previously saved model.
:param config: tf.RunConfig object to configure the runtime settings.
:param linear_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the linear part of the model. Defaults to FTRL optimizer.
:param dnn_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the deep part of the model. Defaults to Adagrad optimizer.
:param training_chief_hooks: Iterable of `tf.train.SessionRunHook` objects to
run on the chief worker during training.
:return: A Tensorflow Estimator instance.
"""
if len(dnn_hidden_units) == 0 and cross_num == 0:
raise ValueError("Either hidden_layer or cross layer must > 0")
def _model_fn(features, labels, mode, config):
train_flag = (mode == tf.estimator.ModeKeys.TRAIN)
linear_logits = get_linear_logit(features, linear_feature_columns, l2_reg_linear=l2_reg_linear)
with variable_scope(DNN_SCOPE_NAME):
sparse_embedding_list, dense_value_list = input_from_feature_columns(features, dnn_feature_columns,
l2_reg_embedding=l2_reg_embedding)
dnn_input = combined_dnn_input(sparse_embedding_list, dense_value_list)
if len(dnn_hidden_units) > 0 and cross_num > 0: # Deep & Cross
deep_out = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout, dnn_use_bn, seed=seed)(dnn_input, training=train_flag)
cross_out = CrossNet(cross_num, l2_reg=l2_reg_cross)(dnn_input)
stack_out = tf.keras.layers.Concatenate()([cross_out, deep_out])
final_logit = tf.keras.layers.Dense(
1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed))(stack_out)
elif len(dnn_hidden_units) > 0: # Only Deep
deep_out = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout, dnn_use_bn, seed=seed)(dnn_input, training=train_flag)
final_logit = tf.keras.layers.Dense(
1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed))(deep_out)
elif cross_num > 0: # Only Cross
cross_out = CrossNet(cross_num, l2_reg=l2_reg_cross)(dnn_input)
final_logit = tf.keras.layers.Dense(
1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed))(cross_out)
else: # Error
raise NotImplementedError
logits = linear_logits + final_logit
return deepctr_model_fn(features, mode, logits, labels, task, linear_optimizer, dnn_optimizer,
training_chief_hooks=training_chief_hooks)
return tf.estimator.Estimator(_model_fn, model_dir=model_dir, config=config)
================================================
FILE: deepctr/estimator/models/deepfefm.py
================================================
# -*- coding:utf-8 -*-
"""
Author:
Harshit Pande
Reference:
[1] Field-Embedded Factorization Machines for Click-through Rate Prediction]
(https://arxiv.org/abs/2009.09931)
"""
import tensorflow as tf
from ..feature_column import get_linear_logit, input_from_feature_columns
from ..utils import DNN_SCOPE_NAME, deepctr_model_fn, variable_scope
from ...layers.core import DNN
from ...layers.interaction import FEFMLayer
from ...layers.utils import concat_func, add_func, combined_dnn_input, reduce_sum
def DeepFEFMEstimator(linear_feature_columns, dnn_feature_columns,
dnn_hidden_units=(256, 128, 64), l2_reg_linear=0.00001, l2_reg_embedding_feat=0.00001,
l2_reg_embedding_field=0.00001, l2_reg_dnn=0, seed=1024, dnn_dropout=0.0,
dnn_activation='relu', dnn_use_bn=False, task='binary', model_dir=None,
config=None, linear_optimizer='Ftrl', dnn_optimizer='Adagrad', training_chief_hooks=None):
"""Instantiates the DeepFEFM Network architecture or the shallow FEFM architecture (Ablation support not provided
as estimator is meant for production, Ablation support provided in DeepFEFM implementation in models
:param linear_feature_columns: An iterable containing all the features used by linear part of the model.
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
:param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of DNN
:param l2_reg_linear: float. L2 regularizer strength applied to linear part
:param l2_reg_embedding_feat: float. L2 regularizer strength applied to embedding vector of features
:param l2_reg_embedding_field: float, L2 regularizer to field embeddings
:param l2_reg_dnn: float. L2 regularizer strength applied to DNN
:param seed: integer ,to use as random seed.
:param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
:param dnn_activation: Activation function to use in DNN
:param dnn_use_bn: bool. Whether use BatchNormalization before activation or not in DNN
:param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
:param model_dir: Directory to save model parameters, graph and etc. This can
also be used to load checkpoints from the directory into a estimator
to continue training a previously saved model.
:param config: tf.RunConfig object to configure the runtime settings.
:param linear_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the linear part of the model. Defaults to FTRL optimizer.
:param dnn_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the deep part of the model. Defaults to Adagrad optimizer.
:param training_chief_hooks: Iterable of `tf.train.SessionRunHook` objects to
run on the chief worker during training.
:return: A Tensorflow Estimator instance.
"""
def _model_fn(features, labels, mode, config):
train_flag = (mode == tf.estimator.ModeKeys.TRAIN)
linear_logits = get_linear_logit(features, linear_feature_columns, l2_reg_linear=l2_reg_linear)
final_logit_components = [linear_logits]
with variable_scope(DNN_SCOPE_NAME):
sparse_embedding_list, dense_value_list = input_from_feature_columns(features, dnn_feature_columns,
l2_reg_embedding=l2_reg_embedding_feat)
fefm_interaction_embedding = FEFMLayer(
regularizer=l2_reg_embedding_field)(concat_func(sparse_embedding_list, axis=1))
fefm_logit = tf.keras.layers.Lambda(lambda x: reduce_sum(x, axis=1, keep_dims=True))(
fefm_interaction_embedding)
final_logit_components.append(fefm_logit)
if dnn_hidden_units:
dnn_input = combined_dnn_input(sparse_embedding_list, dense_value_list)
dnn_input = concat_func([dnn_input, fefm_interaction_embedding], axis=1)
dnn_output = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout, dnn_use_bn, seed=seed)(
dnn_input, training=train_flag)
dnn_logit = tf.keras.layers.Dense(
1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed))(dnn_output)
final_logit_components.append(dnn_logit)
logits = add_func(final_logit_components)
return deepctr_model_fn(features, mode, logits, labels, task, linear_optimizer, dnn_optimizer,
training_chief_hooks=training_chief_hooks)
return tf.estimator.Estimator(_model_fn, model_dir=model_dir, config=config)
================================================
FILE: deepctr/estimator/models/deepfm.py
================================================
# -*- coding:utf-8 -*-
"""
Author:
Weichen Shen, weichenswc@163.com
Reference:
[1] Guo H, Tang R, Ye Y, et al. Deepfm: a factorization-machine based neural network for ctr prediction[J]. arXiv preprint arXiv:1703.04247, 2017.(https://arxiv.org/abs/1703.04247)
"""
import tensorflow as tf
from ..feature_column import get_linear_logit, input_from_feature_columns
from ..utils import deepctr_model_fn, DNN_SCOPE_NAME, variable_scope
from ...layers.core import DNN
from ...layers.interaction import FM
from ...layers.utils import concat_func, combined_dnn_input
def DeepFMEstimator(linear_feature_columns, dnn_feature_columns, dnn_hidden_units=(256, 128, 64),
l2_reg_linear=0.00001, l2_reg_embedding=0.00001, l2_reg_dnn=0, seed=1024, dnn_dropout=0,
dnn_activation='relu', dnn_use_bn=False, task='binary', model_dir=None, config=None,
linear_optimizer='Ftrl',
dnn_optimizer='Adagrad', training_chief_hooks=None):
"""Instantiates the DeepFM Network architecture.
:param linear_feature_columns: An iterable containing all the features used by linear part of the model.
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
:param fm_group: list, group_name of features that will be used to do feature interactions.
:param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of DNN
:param l2_reg_linear: float. L2 regularizer strength applied to linear part
:param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
:param l2_reg_dnn: float. L2 regularizer strength applied to DNN
:param seed: integer ,to use as random seed.
:param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
:param dnn_activation: Activation function to use in DNN
:param dnn_use_bn: bool. Whether use BatchNormalization before activation or not in DNN
:param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
:param model_dir: Directory to save model parameters, graph and etc. This can
also be used to load checkpoints from the directory into a estimator
to continue training a previously saved model.
:param config: tf.RunConfig object to configure the runtime settings.
:param linear_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the linear part of the model. Defaults to FTRL optimizer.
:param dnn_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the deep part of the model. Defaults to Adagrad optimizer.
:param training_chief_hooks: Iterable of `tf.train.SessionRunHook` objects to
run on the chief worker during training.
:return: A Tensorflow Estimator instance.
"""
def _model_fn(features, labels, mode, config):
train_flag = (mode == tf.estimator.ModeKeys.TRAIN)
linear_logits = get_linear_logit(features, linear_feature_columns, l2_reg_linear=l2_reg_linear)
with variable_scope(DNN_SCOPE_NAME):
sparse_embedding_list, dense_value_list = input_from_feature_columns(features, dnn_feature_columns,
l2_reg_embedding=l2_reg_embedding)
dnn_input = combined_dnn_input(sparse_embedding_list, dense_value_list)
fm_logit = FM()(concat_func(sparse_embedding_list, axis=1))
dnn_output = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout, dnn_use_bn, seed=seed)(dnn_input, training=train_flag)
dnn_logit = tf.keras.layers.Dense(
1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed=seed))(dnn_output)
logits = linear_logits + fm_logit + dnn_logit
return deepctr_model_fn(features, mode, logits, labels, task, linear_optimizer, dnn_optimizer,
training_chief_hooks
=training_chief_hooks)
return tf.estimator.Estimator(_model_fn, model_dir=model_dir, config=config)
================================================
FILE: deepctr/estimator/models/fibinet.py
================================================
# -*- coding:utf-8 -*-
"""
Author:
Weichen Shen, weichenswc@163.com
Reference:
[1] Huang T, Zhang Z, Zhang J. FiBiNET: Combining Feature Importance and Bilinear feature Interaction for Click-Through Rate Prediction[J]. arXiv preprint arXiv:1905.09433, 2019.
"""
import tensorflow as tf
from tensorflow.python.keras.layers import Dense, Flatten
from ..feature_column import get_linear_logit, input_from_feature_columns
from ..utils import deepctr_model_fn, DNN_SCOPE_NAME, variable_scope
from ...layers.core import DNN
from ...layers.interaction import SENETLayer, BilinearInteraction
from ...layers.utils import concat_func, combined_dnn_input
def FiBiNETEstimator(linear_feature_columns, dnn_feature_columns, bilinear_type='interaction', reduction_ratio=3,
dnn_hidden_units=(256, 128, 64), l2_reg_linear=1e-5,
l2_reg_embedding=1e-5, l2_reg_dnn=0, seed=1024, dnn_dropout=0, dnn_activation='relu',
task='binary', model_dir=None, config=None, linear_optimizer='Ftrl',
dnn_optimizer='Adagrad', training_chief_hooks=None):
"""Instantiates the Feature Importance and Bilinear feature Interaction NETwork architecture.
:param linear_feature_columns: An iterable containing all the features used by linear part of the model.
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
:param bilinear_type: str,bilinear function type used in Bilinear Interaction Layer,can be ``'all'`` , ``'each'`` or ``'interaction'``
:param reduction_ratio: integer in [1,inf), reduction ratio used in SENET Layer
:param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of DNN
:param l2_reg_linear: float. L2 regularizer strength applied to wide part
:param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
:param l2_reg_dnn: float. L2 regularizer strength applied to DNN
:param seed: integer ,to use as random seed.
:param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
:param dnn_activation: Activation function to use in DNN
:param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
:param model_dir: Directory to save model parameters, graph and etc. This can
also be used to load checkpoints from the directory into a estimator
to continue training a previously saved model.
:param config: tf.RunConfig object to configure the runtime settings.
:param linear_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the linear part of the model. Defaults to FTRL optimizer.
:param dnn_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the deep part of the model. Defaults to Adagrad optimizer.
:param training_chief_hooks: Iterable of `tf.train.SessionRunHook` objects to
run on the chief worker during training.
:return: A Tensorflow Estimator instance.
"""
def _model_fn(features, labels, mode, config):
train_flag = (mode == tf.estimator.ModeKeys.TRAIN)
linear_logits = get_linear_logit(features, linear_feature_columns, l2_reg_linear=l2_reg_linear)
with variable_scope(DNN_SCOPE_NAME):
sparse_embedding_list, dense_value_list = input_from_feature_columns(features, dnn_feature_columns,
l2_reg_embedding=l2_reg_embedding)
senet_embedding_list = SENETLayer(
reduction_ratio, seed)(sparse_embedding_list)
senet_bilinear_out = BilinearInteraction(
bilinear_type=bilinear_type, seed=seed)(senet_embedding_list)
bilinear_out = BilinearInteraction(
bilinear_type=bilinear_type, seed=seed)(sparse_embedding_list)
dnn_input = combined_dnn_input(
[Flatten()(concat_func([senet_bilinear_out, bilinear_out]))], dense_value_list)
dnn_out = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout, False, seed=seed)(dnn_input, training=train_flag)
dnn_logit = Dense(
1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed))(dnn_out)
logits = linear_logits + dnn_logit
return deepctr_model_fn(features, mode, logits, labels, task, linear_optimizer, dnn_optimizer,
training_chief_hooks=training_chief_hooks)
return tf.estimator.Estimator(_model_fn, model_dir=model_dir, config=config)
================================================
FILE: deepctr/estimator/models/fnn.py
================================================
# -*- coding:utf-8 -*-
"""
Author:
Weichen Shen, weichenswc@163.com
Reference:
[1] Zhang W, Du T, Wang J. Deep learning over multi-field categorical data[C]//European conference on information retrieval. Springer, Cham, 2016: 45-57.(https://arxiv.org/pdf/1601.02376.pdf)
"""
import tensorflow as tf
from ..feature_column import get_linear_logit, input_from_feature_columns
from ..utils import deepctr_model_fn, DNN_SCOPE_NAME, variable_scope
from ...layers.core import DNN
from ...layers.utils import combined_dnn_input
def FNNEstimator(linear_feature_columns, dnn_feature_columns, dnn_hidden_units=(256, 128, 64),
l2_reg_embedding=1e-5, l2_reg_linear=1e-5, l2_reg_dnn=0, seed=1024, dnn_dropout=0,
dnn_activation='relu', task='binary', model_dir=None, config=None, linear_optimizer='Ftrl',
dnn_optimizer='Adagrad', training_chief_hooks=None):
"""Instantiates the Factorization-supported Neural Network architecture.
:param linear_feature_columns: An iterable containing all the features used by linear part of the model.
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
:param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of deep net
:param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
:param l2_reg_linear: float. L2 regularizer strength applied to linear weight
:param l2_reg_dnn: float . L2 regularizer strength applied to DNN
:param seed: integer ,to use as random seed.
:param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
:param dnn_activation: Activation function to use in DNN
:param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
:param model_dir: Directory to save model parameters, graph and etc. This can
also be used to load checkpoints from the directory into a estimator
to continue training a previously saved model.
:param config: tf.RunConfig object to configure the runtime settings.
:param linear_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the linear part of the model. Defaults to FTRL optimizer.
:param dnn_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the deep part of the model. Defaults to Adagrad optimizer.
:param training_chief_hooks: Iterable of `tf.train.SessionRunHook` objects to
run on the chief worker during training.
:return: A Tensorflow Estimator instance.
"""
def _model_fn(features, labels, mode, config):
train_flag = (mode == tf.estimator.ModeKeys.TRAIN)
linear_logits = get_linear_logit(features, linear_feature_columns, l2_reg_linear=l2_reg_linear)
with variable_scope(DNN_SCOPE_NAME):
sparse_embedding_list, dense_value_list = input_from_feature_columns(features, dnn_feature_columns,
l2_reg_embedding=l2_reg_embedding)
dnn_input = combined_dnn_input(sparse_embedding_list, dense_value_list)
deep_out = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout, False, seed=seed)(dnn_input, training=train_flag)
dnn_logit = tf.keras.layers.Dense(
1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed))(deep_out)
logits = linear_logits + dnn_logit
return deepctr_model_fn(features, mode, logits, labels, task, linear_optimizer, dnn_optimizer,
training_chief_hooks=training_chief_hooks)
return tf.estimator.Estimator(_model_fn, model_dir=model_dir, config=config)
================================================
FILE: deepctr/estimator/models/fwfm.py
================================================
# -*- coding:utf-8 -*-
"""
Author:
Weichen Shen, weichenswc@163.com
Harshit Pande
Reference:
[1] Field-weighted Factorization Machines for Click-Through Rate Prediction in Display Advertising
(https://arxiv.org/pdf/1806.03514.pdf)
"""
import tensorflow as tf
from ..feature_column import get_linear_logit, input_from_feature_columns
from ..utils import DNN_SCOPE_NAME, deepctr_model_fn, variable_scope
from ...layers.core import DNN
from ...layers.interaction import FwFMLayer
from ...layers.utils import concat_func, add_func, combined_dnn_input
def FwFMEstimator(linear_feature_columns, dnn_feature_columns, dnn_hidden_units=(256, 128, 64),
l2_reg_linear=0.00001, l2_reg_embedding=0.00001, l2_reg_field_strength=0.00001, l2_reg_dnn=0,
seed=1024, dnn_dropout=0, dnn_activation='relu', dnn_use_bn=False, task='binary', model_dir=None,
config=None, linear_optimizer='Ftrl',
dnn_optimizer='Adagrad', training_chief_hooks=None):
"""Instantiates the DeepFwFM Network architecture.
:param linear_feature_columns: An iterable containing all the features used by linear part of the model.
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
:param fm_group: list, group_name of features that will be used to do feature interactions.
:param dnn_hidden_units: list,list of positive integer or empty list if do not want DNN, the layer number and units
in each layer of DNN
:param l2_reg_linear: float. L2 regularizer strength applied to linear part
:param l2_reg_field_strength: float. L2 regularizer strength applied to the field pair strength parameters
:param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
:param l2_reg_dnn: float. L2 regularizer strength applied to DNN
:param seed: integer ,to use as random seed.
:param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
:param dnn_activation: Activation function to use in DNN
:param dnn_use_bn: bool. Whether use BatchNormalization before activation or not in DNN
:param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
:param model_dir: Directory to save model parameters, graph and etc. This can
also be used to load checkpoints from the directory into a estimator
to continue training a previously saved model.
:param config: tf.RunConfig object to configure the runtime settings.
:param linear_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the linear part of the model. Defaults to FTRL optimizer.
:param dnn_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the deep part of the model. Defaults to Adagrad optimizer.
:param training_chief_hooks: Iterable of `tf.train.SessionRunHook` objects to
run on the chief worker during training.
:return: A Tensorflow Estimator instance.
"""
def _model_fn(features, labels, mode, config):
train_flag = (mode == tf.estimator.ModeKeys.TRAIN)
linear_logits = get_linear_logit(features, linear_feature_columns, l2_reg_linear=l2_reg_linear)
final_logit_components = [linear_logits]
with variable_scope(DNN_SCOPE_NAME):
sparse_embedding_list, dense_value_list = input_from_feature_columns(features, dnn_feature_columns,
l2_reg_embedding=l2_reg_embedding)
fwfm_logit = FwFMLayer(num_fields=len(sparse_embedding_list), regularizer=l2_reg_field_strength)(
concat_func(sparse_embedding_list, axis=1))
final_logit_components.append(fwfm_logit)
if dnn_hidden_units:
dnn_input = combined_dnn_input(sparse_embedding_list, dense_value_list)
dnn_output = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout, dnn_use_bn, seed=seed)(dnn_input, training=train_flag)
dnn_logit = tf.keras.layers.Dense(
1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed))(dnn_output)
final_logit_components.append(dnn_logit)
logits = add_func(final_logit_components)
return deepctr_model_fn(features, mode, logits, labels, task, linear_optimizer, dnn_optimizer,
training_chief_hooks=training_chief_hooks)
return tf.estimator.Estimator(_model_fn, model_dir=model_dir, config=config)
================================================
FILE: deepctr/estimator/models/nfm.py
================================================
# -*- coding:utf-8 -*-
"""
Author:
Weichen Shen, weichenswc@163.com
Reference:
[1] He X, Chua T S. Neural factorization machines for sparse predictive analytics[C]//Proceedings of the 40th International ACM SIGIR conference on Research and Development in Information Retrieval. ACM, 2017: 355-364. (https://arxiv.org/abs/1708.05027)
"""
import tensorflow as tf
from ..feature_column import get_linear_logit, input_from_feature_columns
from ..utils import deepctr_model_fn, DNN_SCOPE_NAME, variable_scope
from ...layers.core import DNN
from ...layers.interaction import BiInteractionPooling
from ...layers.utils import concat_func, combined_dnn_input
def NFMEstimator(linear_feature_columns, dnn_feature_columns, dnn_hidden_units=(256, 128, 64),
l2_reg_embedding=1e-5, l2_reg_linear=1e-5, l2_reg_dnn=0, seed=1024, bi_dropout=0,
dnn_dropout=0, dnn_activation='relu', task='binary', model_dir=None, config=None,
linear_optimizer='Ftrl',
dnn_optimizer='Adagrad', training_chief_hooks=None):
"""Instantiates the Neural Factorization Machine architecture.
:param linear_feature_columns: An iterable containing all the features used by linear part of the model.
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
:param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of deep net
:param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
:param l2_reg_linear: float. L2 regularizer strength applied to linear part.
:param l2_reg_dnn: float . L2 regularizer strength applied to DNN
:param seed: integer ,to use as random seed.
:param biout_dropout: When not ``None``, the probability we will drop out the output of BiInteractionPooling Layer.
:param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
:param dnn_activation: Activation function to use in deep net
:param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
:param model_dir: Directory to save model parameters, graph and etc. This can
also be used to load checkpoints from the directory into a estimator
to continue training a previously saved model.
:param config: tf.RunConfig object to configure the runtime settings.
:param linear_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the linear part of the model. Defaults to FTRL optimizer.
:param dnn_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the deep part of the model. Defaults to Adagrad optimizer.
:param training_chief_hooks: Iterable of `tf.train.SessionRunHook` objects to
run on the chief worker during training.
:return: A Tensorflow Estimator instance.
"""
def _model_fn(features, labels, mode, config):
train_flag = (mode == tf.estimator.ModeKeys.TRAIN)
linear_logits = get_linear_logit(features, linear_feature_columns, l2_reg_linear=l2_reg_linear)
with variable_scope(DNN_SCOPE_NAME):
sparse_embedding_list, dense_value_list = input_from_feature_columns(features, dnn_feature_columns,
l2_reg_embedding=l2_reg_embedding)
fm_input = concat_func(sparse_embedding_list, axis=1)
bi_out = BiInteractionPooling()(fm_input)
if bi_dropout:
bi_out = tf.keras.layers.Dropout(bi_dropout)(bi_out, training=None)
dnn_input = combined_dnn_input([bi_out], dense_value_list)
dnn_output = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout, False, seed=seed)(dnn_input, training=train_flag)
dnn_logit = tf.keras.layers.Dense(
1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed))(dnn_output)
logits = linear_logits + dnn_logit
return deepctr_model_fn(features, mode, logits, labels, task, linear_optimizer, dnn_optimizer,
training_chief_hooks=training_chief_hooks)
return tf.estimator.Estimator(_model_fn, model_dir=model_dir, config=config)
================================================
FILE: deepctr/estimator/models/pnn.py
================================================
# -*- coding:utf-8 -*-
"""
Author:
Weichen Shen, weichenswc@163.com
Reference:
[1] Qu Y, Cai H, Ren K, et al. Product-based neural networks for user response prediction[C]//Data Mining (ICDM), 2016 IEEE 16th International Conference on. IEEE, 2016: 1149-1154.(https://arxiv.org/pdf/1611.00144.pdf)
"""
import tensorflow as tf
from ..feature_column import get_linear_logit, input_from_feature_columns
from ..utils import deepctr_model_fn, DNN_SCOPE_NAME, variable_scope
from ...layers.core import DNN
from ...layers.interaction import InnerProductLayer, OutterProductLayer
from ...layers.utils import concat_func, combined_dnn_input
def PNNEstimator(dnn_feature_columns, dnn_hidden_units=(256, 128, 64), l2_reg_embedding=1e-5, l2_reg_dnn=0,
seed=1024, dnn_dropout=0, dnn_activation='relu', use_inner=True, use_outter=False, kernel_type='mat',
task='binary', model_dir=None, config=None,
linear_optimizer='Ftrl',
dnn_optimizer='Adagrad', training_chief_hooks=None):
"""Instantiates the Product-based Neural Network architecture.
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
:param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of deep net
:param l2_reg_embedding: float . L2 regularizer strength applied to embedding vector
:param l2_reg_dnn: float. L2 regularizer strength applied to DNN
:param seed: integer ,to use as random seed.
:param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
:param dnn_activation: Activation function to use in DNN
:param use_inner: bool,whether use inner-product or not.
:param use_outter: bool,whether use outter-product or not.
:param kernel_type: str,kernel_type used in outter-product,can be ``'mat'`` , ``'vec'`` or ``'num'``
:param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
:param model_dir: Directory to save model parameters, graph and etc. This can
also be used to load checkpoints from the directory into a estimator
to continue training a previously saved model.
:param config: tf.RunConfig object to configure the runtime settings.
:param linear_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the linear part of the model. Defaults to FTRL optimizer.
:param dnn_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the deep part of the model. Defaults to Adagrad optimizer.
:param training_chief_hooks: Iterable of `tf.train.SessionRunHook` objects to
run on the chief worker during training.
:return: A Tensorflow Estimator instance.
"""
if kernel_type not in ['mat', 'vec', 'num']:
raise ValueError("kernel_type must be mat,vec or num")
def _model_fn(features, labels, mode, config):
train_flag = (mode == tf.estimator.ModeKeys.TRAIN)
linear_logits = get_linear_logit(features, [], l2_reg_linear=0)
with variable_scope(DNN_SCOPE_NAME):
sparse_embedding_list, dense_value_list = input_from_feature_columns(features, dnn_feature_columns,
l2_reg_embedding=l2_reg_embedding)
inner_product = tf.keras.layers.Flatten()(
InnerProductLayer()(sparse_embedding_list))
outter_product = OutterProductLayer(kernel_type)(sparse_embedding_list)
# ipnn deep input
linear_signal = tf.keras.layers.Reshape(
[sum(map(lambda x: int(x.shape[-1]), sparse_embedding_list))])(concat_func(sparse_embedding_list))
if use_inner and use_outter:
deep_input = tf.keras.layers.Concatenate()(
[linear_signal, inner_product, outter_product])
elif use_inner:
deep_input = tf.keras.layers.Concatenate()(
[linear_signal, inner_product])
elif use_outter:
deep_input = tf.keras.layers.Concatenate()(
[linear_signal, outter_product])
else:
deep_input = linear_signal
dnn_input = combined_dnn_input([deep_input], dense_value_list)
dnn_out = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout, False, seed=seed)(dnn_input, training=train_flag)
dnn_logit = tf.keras.layers.Dense(
1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed))(dnn_out)
logits = linear_logits + dnn_logit
return deepctr_model_fn(features, mode, logits, labels, task, linear_optimizer, dnn_optimizer,
training_chief_hooks=training_chief_hooks)
return tf.estimator.Estimator(_model_fn, model_dir=model_dir, config=config)
================================================
FILE: deepctr/estimator/models/wdl.py
================================================
# -*- coding:utf-8 -*-
"""
Author:
Weichen Shen, weichenswc@163.com
Reference:
[1] Cheng H T, Koc L, Harmsen J, et al. Wide & deep learning for recommender systems[C]//Proceedings of the 1st Workshop on Deep Learning for Recommender Systems. ACM, 2016: 7-10.(https://arxiv.org/pdf/1606.07792.pdf)
"""
import tensorflow as tf
from tensorflow.python.keras.layers import Dense
from ..feature_column import get_linear_logit, input_from_feature_columns
from ..utils import deepctr_model_fn, DNN_SCOPE_NAME, variable_scope
from ...layers import DNN, combined_dnn_input
def WDLEstimator(linear_feature_columns, dnn_feature_columns, dnn_hidden_units=(256, 128, 64), l2_reg_linear=1e-5,
l2_reg_embedding=1e-5, l2_reg_dnn=0, seed=1024, dnn_dropout=0, dnn_activation='relu',
task='binary', model_dir=None, config=None, linear_optimizer='Ftrl',
dnn_optimizer='Adagrad', training_chief_hooks=None):
"""Instantiates the Wide&Deep Learning architecture.
:param linear_feature_columns: An iterable containing all the features used by linear part of the model.
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
:param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of DNN
:param l2_reg_linear: float. L2 regularizer strength applied to wide part
:param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
:param l2_reg_dnn: float. L2 regularizer strength applied to DNN
:param seed: integer ,to use as random seed.
:param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
:param dnn_activation: Activation function to use in DNN
:param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
:param model_dir: Directory to save model parameters, graph and etc. This can
also be used to load checkpoints from the directory into a estimator
to continue training a previously saved model.
:param config: tf.RunConfig object to configure the runtime settings.
:param linear_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the linear part of the model. Defaults to FTRL optimizer.
:param dnn_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the deep part of the model. Defaults to Adagrad optimizer.
:param training_chief_hooks: Iterable of `tf.train.SessionRunHook` objects to
run on the chief worker during training.
:return: A Tensorflow Estimator instance.
"""
def _model_fn(features, labels, mode, config):
train_flag = (mode == tf.estimator.ModeKeys.TRAIN)
linear_logits = get_linear_logit(features, linear_feature_columns, l2_reg_linear=l2_reg_linear)
with variable_scope(DNN_SCOPE_NAME):
sparse_embedding_list, dense_value_list = input_from_feature_columns(features, dnn_feature_columns,
l2_reg_embedding=l2_reg_embedding)
dnn_input = combined_dnn_input(sparse_embedding_list, dense_value_list)
dnn_out = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout, False, seed=seed)(dnn_input, training=train_flag)
dnn_logits = Dense(
1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed))(dnn_out)
logits = linear_logits + dnn_logits
return deepctr_model_fn(features, mode, logits, labels, task, linear_optimizer, dnn_optimizer,
training_chief_hooks=training_chief_hooks)
return tf.estimator.Estimator(_model_fn, model_dir=model_dir, config=config)
================================================
FILE: deepctr/estimator/models/xdeepfm.py
================================================
# -*- coding:utf-8 -*-
"""
Author:
Weichen Shen, weichenswc@163.com
Reference:
[1] Lian J, Zhou X, Zhang F, et al. xDeepFM: Combining Explicit and Implicit Feature Interactions for Recommender Systems[J]. arXiv preprint arXiv:1803.05170, 2018.(https://arxiv.org/pdf/1803.05170.pdf)
"""
import tensorflow as tf
from ..feature_column import get_linear_logit, input_from_feature_columns
from ..utils import deepctr_model_fn, DNN_SCOPE_NAME, variable_scope
from ...layers.core import DNN
from ...layers.interaction import CIN
from ...layers.utils import concat_func, add_func, combined_dnn_input
def xDeepFMEstimator(linear_feature_columns, dnn_feature_columns, dnn_hidden_units=(256, 128, 64),
cin_layer_size=(128, 128,), cin_split_half=True, cin_activation='relu', l2_reg_linear=0.00001,
l2_reg_embedding=0.00001, l2_reg_dnn=0, l2_reg_cin=0, seed=1024, dnn_dropout=0,
dnn_activation='relu', dnn_use_bn=False, task='binary', model_dir=None, config=None,
linear_optimizer='Ftrl',
dnn_optimizer='Adagrad', training_chief_hooks=None):
"""Instantiates the xDeepFM architecture.
:param linear_feature_columns: An iterable containing all the features used by linear part of the model.
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
:param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of deep net
:param cin_layer_size: list,list of positive integer or empty list, the feature maps in each hidden layer of Compressed Interaction Network
:param cin_split_half: bool.if set to True, half of the feature maps in each hidden will connect to output unit
:param cin_activation: activation function used on feature maps
:param l2_reg_linear: float. L2 regularizer strength applied to linear part
:param l2_reg_embedding: L2 regularizer strength applied to embedding vector
:param l2_reg_dnn: L2 regularizer strength applied to deep net
:param l2_reg_cin: L2 regularizer strength applied to CIN.
:param seed: integer ,to use as random seed.
:param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
:param dnn_activation: Activation function to use in DNN
:param dnn_use_bn: bool. Whether use BatchNormalization before activation or not in DNN
:param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
:param model_dir: Directory to save model parameters, graph and etc. This can
also be used to load checkpoints from the directory into a estimator
to continue training a previously saved model.
:param config: tf.RunConfig object to configure the runtime settings.
:param linear_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the linear part of the model. Defaults to FTRL optimizer.
:param dnn_optimizer: An instance of `tf.Optimizer` used to apply gradients to
the deep part of the model. Defaults to Adagrad optimizer.
:param training_chief_hooks: Iterable of `tf.train.SessionRunHook` objects to
run on the chief worker during training.
:return: A Tensorflow Estimator instance.
"""
def _model_fn(features, labels, mode, config):
train_flag = (mode == tf.estimator.ModeKeys.TRAIN)
linear_logits = get_linear_logit(features, linear_feature_columns, l2_reg_linear=l2_reg_linear)
logits_list = [linear_logits]
with variable_scope(DNN_SCOPE_NAME):
sparse_embedding_list, dense_value_list = input_from_feature_columns(features, dnn_feature_columns,
l2_reg_embedding=l2_reg_embedding)
fm_input = concat_func(sparse_embedding_list, axis=1)
dnn_input = combined_dnn_input(sparse_embedding_list, dense_value_list)
dnn_output = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout, dnn_use_bn, seed=seed)(dnn_input, training=train_flag)
dnn_logit = tf.keras.layers.Dense(
1, use_bias=False, kernel_initializer=tf.keras.initializers.glorot_normal(seed))(dnn_output)
logits_list.append(dnn_logit)
if len(cin_layer_size) > 0:
exFM_out = CIN(cin_layer_size, cin_activation,
cin_split_half, l2_reg_cin, seed)(fm_input, training=train_flag)
exFM_logit = tf.keras.layers.Dense(1, kernel_initializer=tf.keras.initializers.glorot_normal(seed) )(exFM_out)
logits_list.append(exFM_logit)
logits = add_func(logits_list)
return deepctr_model_fn(features, mode, logits, labels, task, linear_optimizer, dnn_optimizer,
training_chief_hooks=training_chief_hooks)
return tf.estimator.Estimator(_model_fn, model_dir=model_dir, config=config)
================================================
FILE: deepctr/estimator/utils.py
================================================
import tensorflow as tf
from tensorflow.python.estimator.canned.head import _Head
from tensorflow.python.estimator.canned.optimizers import get_optimizer_instance
LINEAR_SCOPE_NAME = 'linear'
DNN_SCOPE_NAME = 'dnn'
def _summary_key(head_name, val):
return '%s/%s' % (val, head_name) if head_name else val
class Head(_Head):
def __init__(self, task,
name=None):
self._task = task
self._name = name
@property
def name(self):
return self._name
@property
def logits_dimension(self):
return 1
def _eval_metric_ops(self,
labels,
logits,
predictions,
unweighted_loss,
weights=None):
labels = to_float(labels)
predictions = to_float(predictions)
# with name_scope(None, 'metrics', (labels, logits, predictions,
# unweighted_loss, weights)):
metrics = get_metrics()
losses = get_losses()
metric_ops = {
_summary_key(self._name, "prediction/mean"): metrics.mean(predictions, weights=weights),
_summary_key(self._name, "label/mean"): metrics.mean(labels, weights=weights),
}
summary_scalar("prediction/mean", metric_ops[_summary_key(self._name, "prediction/mean")][1])
summary_scalar("label/mean", metric_ops[_summary_key(self._name, "label/mean")][1])
mean_loss = losses.compute_weighted_loss(
unweighted_loss, weights=1.0, reduction=losses.Reduction.MEAN)
if self._task == "binary":
metric_ops[_summary_key(self._name, "LogLoss")] = metrics.mean(mean_loss, weights=weights, )
summary_scalar("LogLoss", mean_loss)
metric_ops[_summary_key(self._name, "AUC")] = metrics.auc(labels, predictions, weights=weights)
summary_scalar("AUC", metric_ops[_summary_key(self._name, "AUC")][1])
else:
metric_ops[_summary_key(self._name, "MSE")] = metrics.mean_squared_error(labels, predictions,
weights=weights)
summary_scalar("MSE", mean_loss)
metric_ops[_summary_key(self._name, "MAE")] = metrics.mean_absolute_error(labels, predictions,
weights=weights)
summary_scalar("MAE", metric_ops[_summary_key(self._name, "MAE")][1])
return metric_ops
def create_loss(self, features, mode, logits, labels):
del mode, features # Unused for this head.
losses = get_losses()
if self._task == "binary":
loss = losses.sigmoid_cross_entropy(labels, logits, reduction=losses.Reduction.NONE)
else:
loss = losses.mean_squared_error(labels, logits, reduction=losses.Reduction.NONE)
return loss
def create_estimator_spec(
self, features, mode, logits, labels=None, train_op_fn=None, training_chief_hooks=None):
# with name_scope('head'):
logits = tf.reshape(logits, [-1, 1])
if self._task == 'binary':
pred = tf.sigmoid(logits)
else:
pred = logits
predictions = {"pred": pred, "logits": logits}
export_outputs = {"predict": tf.estimator.export.PredictOutput(predictions)}
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(
mode=mode,
predictions=predictions,
export_outputs=export_outputs)
labels = tf.reshape(labels, [-1, 1])
unweighted_loss = self.create_loss(features, mode, logits, labels)
losses = get_losses()
loss = losses.compute_weighted_loss(
unweighted_loss, weights=1.0, reduction=losses.Reduction.SUM)
reg_loss = losses.get_regularization_loss()
training_loss = loss + reg_loss
eval_metric_ops = self._eval_metric_ops(labels, logits, pred, unweighted_loss)
return tf.estimator.EstimatorSpec(
mode=mode,
predictions=predictions,
loss=training_loss,
train_op=train_op_fn(training_loss),
eval_metric_ops=eval_metric_ops,
training_chief_hooks=training_chief_hooks)
def deepctr_model_fn(features, mode, logits, labels, task, linear_optimizer, dnn_optimizer, training_chief_hooks):
linear_optimizer = get_optimizer_instance(linear_optimizer, 0.005)
dnn_optimizer = get_optimizer_instance(dnn_optimizer, 0.01)
train_op_fn = get_train_op_fn(linear_optimizer, dnn_optimizer)
head = Head(task)
return head.create_estimator_spec(features=features,
mode=mode,
labels=labels,
train_op_fn=train_op_fn,
logits=logits, training_chief_hooks=training_chief_hooks)
def get_train_op_fn(linear_optimizer, dnn_optimizer):
def _train_op_fn(loss):
train_ops = []
try:
global_step = tf.train.get_global_step()
except AttributeError:
global_step = tf.compat.v1.train.get_global_step()
linear_var_list = get_collection(get_GraphKeys().TRAINABLE_VARIABLES, LINEAR_SCOPE_NAME)
dnn_var_list = get_collection(get_GraphKeys().TRAINABLE_VARIABLES, DNN_SCOPE_NAME)
if len(dnn_var_list) > 0:
train_ops.append(
dnn_optimizer.minimize(
loss,
var_list=dnn_var_list))
if len(linear_var_list) > 0:
train_ops.append(
linear_optimizer.minimize(
loss,
var_list=linear_var_list))
train_op = tf.group(*train_ops)
with tf.control_dependencies([train_op]):
try:
return tf.assign_add(global_step, 1).op
except AttributeError:
return tf.compat.v1.assign_add(global_step, 1).op
return _train_op_fn
def variable_scope(name_or_scope):
try:
return tf.variable_scope(name_or_scope)
except AttributeError:
return tf.compat.v1.variable_scope(name_or_scope)
def get_collection(key, scope=None):
try:
return tf.get_collection(key, scope=scope)
except AttributeError:
return tf.compat.v1.get_collection(key, scope=scope)
def get_GraphKeys():
try:
return tf.GraphKeys
except AttributeError:
return tf.compat.v1.GraphKeys
def get_losses():
try:
return tf.compat.v1.losses
except AttributeError:
return tf.losses
def input_layer(features, feature_columns):
try:
return tf.feature_column.input_layer(features, feature_columns)
except AttributeError:
return tf.compat.v1.feature_column.input_layer(features, feature_columns)
def get_metrics():
try:
return tf.compat.v1.metrics
except AttributeError:
return tf.metrics
def to_float(x, name="ToFloat"):
try:
return tf.to_float(x, name)
except AttributeError:
return tf.compat.v1.to_float(x, name)
def summary_scalar(name, data):
try:
tf.summary.scalar(name, data)
except AttributeError: # tf version 2.5.0+:AttributeError: module 'tensorflow._api.v2.summary' has no attribute 'scalar'
tf.compat.v1.summary.scalar(name, data)
================================================
FILE: deepctr/feature_column.py
================================================
import tensorflow as tf
from collections import namedtuple, OrderedDict
from copy import copy
from itertools import chain
from tensorflow.python.keras.initializers import RandomNormal, Zeros
from tensorflow.python.keras.layers import Input, Lambda
from .inputs import create_embedding_matrix, embedding_lookup, get_dense_input, varlen_embedding_lookup, \
get_varlen_pooling_list, mergeDict
from .layers import Linear
from .layers.utils import concat_func
DEFAULT_GROUP_NAME = "default_group"
class SparseFeat(namedtuple('SparseFeat',
['name', 'vocabulary_size', 'embedding_dim', 'use_hash', 'vocabulary_path', 'dtype', 'embeddings_initializer',
'embedding_name',
'group_name', 'trainable'])):
__slots__ = ()
def __new__(cls, name, vocabulary_size, embedding_dim=4, use_hash=False, vocabulary_path=None, dtype="int32", embeddings_initializer=None,
embedding_name=None,
group_name=DEFAULT_GROUP_NAME, trainable=True):
if embedding_dim == "auto":
embedding_dim = 6 * int(pow(vocabulary_size, 0.25))
if embeddings_initializer is None:
embeddings_initializer = RandomNormal(mean=0.0, stddev=0.0001, seed=2020)
if embedding_name is None:
embedding_name = name
return super(SparseFeat, cls).__new__(cls, name, vocabulary_size, embedding_dim, use_hash, vocabulary_path, dtype,
embeddings_initializer,
embedding_name, group_name, trainable)
def __hash__(self):
return self.name.__hash__()
class VarLenSparseFeat(namedtuple('VarLenSparseFeat',
['sparsefeat', 'maxlen', 'combiner', 'length_name', 'weight_name', 'weight_norm'])):
__slots__ = ()
def __new__(cls, sparsefeat, maxlen, combiner="mean", length_name=None, weight_name=None, weight_norm=True):
return super(VarLenSparseFeat, cls).__new__(cls, sparsefeat, maxlen, combiner, length_name, weight_name,
weight_norm)
@property
def name(self):
return self.sparsefeat.name
@property
def vocabulary_size(self):
return self.sparsefeat.vocabulary_size
@property
def embedding_dim(self):
return self.sparsefeat.embedding_dim
@property
def use_hash(self):
return self.sparsefeat.use_hash
@property
def vocabulary_path(self):
return self.sparsefeat.vocabulary_path
@property
def dtype(self):
return self.sparsefeat.dtype
@property
def embeddings_initializer(self):
return self.sparsefeat.embeddings_initializer
@property
def embedding_name(self):
return self.sparsefeat.embedding_name
@property
def group_name(self):
return self.sparsefeat.group_name
@property
def trainable(self):
return self.sparsefeat.trainable
def __hash__(self):
return self.name.__hash__()
class DenseFeat(namedtuple('DenseFeat', ['name', 'dimension', 'dtype', 'transform_fn'])):
""" Dense feature
Args:
name: feature name.
dimension: dimension of the feature, default = 1.
dtype: dtype of the feature, default="float32".
transform_fn: If not `None` , a function that can be used to transform
values of the feature. the function takes the input Tensor as its
argument, and returns the output Tensor.
(e.g. lambda x: (x - 3.0) / 4.2).
"""
__slots__ = ()
def __new__(cls, name, dimension=1, dtype="float32", transform_fn=None):
return super(DenseFeat, cls).__new__(cls, name, dimension, dtype, transform_fn)
def __hash__(self):
return self.name.__hash__()
# def __eq__(self, other):
# if self.name == other.name:
# return True
# return False
# def __repr__(self):
# return 'DenseFeat:'+self.name
def get_feature_names(feature_columns):
features = build_input_features(feature_columns)
return list(features.keys())
def build_input_features(feature_columns, prefix=''):
input_features = OrderedDict()
for fc in feature_columns:
if isinstance(fc, SparseFeat):
input_features[fc.name] = Input(
shape=(1,), name=prefix + fc.name, dtype=fc.dtype)
elif isinstance(fc, DenseFeat):
input_features[fc.name] = Input(
shape=(fc.dimension,), name=prefix + fc.name, dtype=fc.dtype)
elif isinstance(fc, VarLenSparseFeat):
input_features[fc.name] = Input(shape=(fc.maxlen,), name=prefix + fc.name,
dtype=fc.dtype)
if fc.weight_name is not None:
input_features[fc.weight_name] = Input(shape=(fc.maxlen, 1), name=prefix + fc.weight_name,
dtype="float32")
if fc.length_name is not None:
input_features[fc.length_name] = Input((1,), name=prefix + fc.length_name, dtype='int32')
else:
raise TypeError("Invalid feature column type,got", type(fc))
return input_features
def get_linear_logit(features, feature_columns, units=1, use_bias=False, seed=1024, prefix='linear',
l2_reg=0, sparse_feat_refine_weight=None):
linear_feature_columns = copy(feature_columns)
for i in range(len(linear_feature_columns)):
if isinstance(linear_feature_columns[i], SparseFeat):
linear_feature_columns[i] = linear_feature_columns[i]._replace(embedding_dim=1,
embeddings_initializer=Zeros())
if isinstance(linear_feature_columns[i], VarLenSparseFeat):
linear_feature_columns[i] = linear_feature_columns[i]._replace(
sparsefeat=linear_feature_columns[i].sparsefeat._replace(embedding_dim=1,
embeddings_initializer=Zeros()))
linear_emb_list = [input_from_feature_columns(features, linear_feature_columns, l2_reg, seed,
prefix=prefix + str(i))[0] for i in range(units)]
_, dense_input_list = input_from_feature_columns(features, linear_feature_columns, l2_reg, seed, prefix=prefix)
linear_logit_list = []
for i in range(units):
if len(linear_emb_list[i]) > 0 and len(dense_input_list) > 0:
sparse_input = concat_func(linear_emb_list[i])
dense_input = concat_func(dense_input_list)
if sparse_feat_refine_weight is not None:
sparse_input = Lambda(lambda x: x[0] * tf.expand_dims(x[1], axis=1))(
[sparse_input, sparse_feat_refine_weight])
linear_logit = Linear(l2_reg, mode=2, use_bias=use_bias, seed=seed)([sparse_input, dense_input])
elif len(linear_emb_list[i]) > 0:
sparse_input = concat_func(linear_emb_list[
gitextract_0q19foz8/
├── .gitattributes
├── .github/
│ ├── ISSUE_TEMPLATE/
│ │ ├── bug_report.md
│ │ ├── feature_request.md
│ │ └── question.md
│ └── workflows/
│ ├── ci.yml
│ └── ci2.yml
├── .gitignore
├── .readthedocs.yml
├── .travis.yml
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── deepctr/
│ ├── __init__.py
│ ├── contrib/
│ │ ├── __init__.py
│ │ ├── rnn.py
│ │ ├── rnn_v2.py
│ │ └── utils.py
│ ├── estimator/
│ │ ├── __init__.py
│ │ ├── feature_column.py
│ │ ├── inputs.py
│ │ ├── models/
│ │ │ ├── __init__.py
│ │ │ ├── afm.py
│ │ │ ├── autoint.py
│ │ │ ├── ccpm.py
│ │ │ ├── dcn.py
│ │ │ ├── deepfefm.py
│ │ │ ├── deepfm.py
│ │ │ ├── fibinet.py
│ │ │ ├── fnn.py
│ │ │ ├── fwfm.py
│ │ │ ├── nfm.py
│ │ │ ├── pnn.py
│ │ │ ├── wdl.py
│ │ │ └── xdeepfm.py
│ │ └── utils.py
│ ├── feature_column.py
│ ├── inputs.py
│ ├── layers/
│ │ ├── __init__.py
│ │ ├── activation.py
│ │ ├── core.py
│ │ ├── interaction.py
│ │ ├── normalization.py
│ │ ├── sequence.py
│ │ └── utils.py
│ ├── models/
│ │ ├── __init__.py
│ │ ├── afm.py
│ │ ├── autoint.py
│ │ ├── ccpm.py
│ │ ├── dcn.py
│ │ ├── dcnmix.py
│ │ ├── deepfefm.py
│ │ ├── deepfm.py
│ │ ├── difm.py
│ │ ├── edcn.py
│ │ ├── fgcnn.py
│ │ ├── fibinet.py
│ │ ├── flen.py
│ │ ├── fnn.py
│ │ ├── fwfm.py
│ │ ├── ifm.py
│ │ ├── mlr.py
│ │ ├── multitask/
│ │ │ ├── __init__.py
│ │ │ ├── esmm.py
│ │ │ ├── mmoe.py
│ │ │ ├── ple.py
│ │ │ └── sharedbottom.py
│ │ ├── nfm.py
│ │ ├── onn.py
│ │ ├── pnn.py
│ │ ├── sequence/
│ │ │ ├── __init__.py
│ │ │ ├── bst.py
│ │ │ ├── dien.py
│ │ │ ├── din.py
│ │ │ └── dsin.py
│ │ ├── wdl.py
│ │ └── xdeepfm.py
│ └── utils.py
├── docs/
│ ├── Makefile
│ ├── make.bat
│ ├── requirements.readthedocs.txt
│ └── source/
│ ├── Estimators.rst
│ ├── Examples.md
│ ├── FAQ.md
│ ├── Features.md
│ ├── History.md
│ ├── Layers.rst
│ ├── Model_Methods.md
│ ├── Models.rst
│ ├── Quick-Start.md
│ ├── conf.py
│ ├── deepctr.contrib.rnn.rst
│ ├── deepctr.contrib.rst
│ ├── deepctr.contrib.utils.rst
│ ├── deepctr.estimator.feature_column.rst
│ ├── deepctr.estimator.inputs.rst
│ ├── deepctr.estimator.models.afm.rst
│ ├── deepctr.estimator.models.autoint.rst
│ ├── deepctr.estimator.models.ccpm.rst
│ ├── deepctr.estimator.models.dcn.rst
│ ├── deepctr.estimator.models.deepfefm.rst
│ ├── deepctr.estimator.models.deepfm.rst
│ ├── deepctr.estimator.models.fibinet.rst
│ ├── deepctr.estimator.models.fnn.rst
│ ├── deepctr.estimator.models.fwfm.rst
│ ├── deepctr.estimator.models.nfm.rst
│ ├── deepctr.estimator.models.pnn.rst
│ ├── deepctr.estimator.models.rst
│ ├── deepctr.estimator.models.wdl.rst
│ ├── deepctr.estimator.models.xdeepfm.rst
│ ├── deepctr.estimator.rst
│ ├── deepctr.estimator.utils.rst
│ ├── deepctr.feature_column.rst
│ ├── deepctr.inputs.rst
│ ├── deepctr.layers.activation.rst
│ ├── deepctr.layers.core.rst
│ ├── deepctr.layers.interaction.rst
│ ├── deepctr.layers.normalization.rst
│ ├── deepctr.layers.rst
│ ├── deepctr.layers.sequence.rst
│ ├── deepctr.layers.utils.rst
│ ├── deepctr.models.afm.rst
│ ├── deepctr.models.autoint.rst
│ ├── deepctr.models.ccpm.rst
│ ├── deepctr.models.dcn.rst
│ ├── deepctr.models.dcnmix.rst
│ ├── deepctr.models.deepfefm.rst
│ ├── deepctr.models.deepfm.rst
│ ├── deepctr.models.deepfwfm.rst
│ ├── deepctr.models.difm.rst
│ ├── deepctr.models.edcn.rst
│ ├── deepctr.models.fgcnn.rst
│ ├── deepctr.models.fibinet.rst
│ ├── deepctr.models.flen.rst
│ ├── deepctr.models.fnn.rst
│ ├── deepctr.models.ifm.rst
│ ├── deepctr.models.mlr.rst
│ ├── deepctr.models.multitask.esmm.rst
│ ├── deepctr.models.multitask.mmoe.rst
│ ├── deepctr.models.multitask.ple.rst
│ ├── deepctr.models.multitask.sharedbottom.rst
│ ├── deepctr.models.nfm.rst
│ ├── deepctr.models.onn.rst
│ ├── deepctr.models.pnn.rst
│ ├── deepctr.models.rst
│ ├── deepctr.models.sequence.bst.rst
│ ├── deepctr.models.sequence.dien.rst
│ ├── deepctr.models.sequence.din.rst
│ ├── deepctr.models.sequence.dsin.rst
│ ├── deepctr.models.wdl.rst
│ ├── deepctr.models.xdeepfm.rst
│ ├── deepctr.rst
│ ├── deepctr.utils.rst
│ ├── index.rst
│ └── modules.rst
├── examples/
│ ├── avazu_sample.txt
│ ├── census-income.sample
│ ├── criteo_sample.te.tfrecords
│ ├── criteo_sample.tr.tfrecords
│ ├── criteo_sample.txt
│ ├── gen_tfrecords.py
│ ├── movielens_age_vocabulary.csv
│ ├── movielens_sample.txt
│ ├── run_all.sh
│ ├── run_classification_criteo.py
│ ├── run_classification_criteo_hash.py
│ ├── run_classification_criteo_multi_gpu.py
│ ├── run_dien.py
│ ├── run_din.py
│ ├── run_dsin.py
│ ├── run_estimator_pandas_classification.py
│ ├── run_estimator_tfrecord_classification.py
│ ├── run_flen.py
│ ├── run_mtl.py
│ ├── run_multivalue_movielens.py
│ ├── run_multivalue_movielens_hash.py
│ ├── run_multivalue_movielens_vocab_hash.py
│ └── run_regression_movielens.py
├── setup.cfg
├── setup.py
└── tests/
├── README.md
├── __init__.py
├── feature_test.py
├── layers/
│ ├── __init__.py
│ ├── activations_test.py
│ ├── core_test.py
│ ├── interaction_test.py
│ ├── normalization_test.py
│ ├── sequence_test.py
│ ├── utils_test.py
│ └── vocabulary_example.csv
├── models/
│ ├── AFM_test.py
│ ├── AutoInt_test.py
│ ├── BST_test.py
│ ├── CCPM_test.py
│ ├── DCNMix_test.py
│ ├── DCN_test.py
│ ├── DIEN_test.py
│ ├── DIFM_test.py
│ ├── DIN_test.py
│ ├── DSIN_test.py
│ ├── DeepFEFM_test.py
│ ├── DeepFM_test.py
│ ├── EDCN_test.py
│ ├── FGCNN_test.py
│ ├── FLEN_test.py
│ ├── FNN_test.py
│ ├── FiBiNET_test.py
│ ├── FwFM_test.py
│ ├── IFM_test.py
│ ├── MLR_test.py
│ ├── MTL_test.py
│ ├── NFM_test.py
│ ├── ONN_test.py
│ ├── PNN_test.py
│ ├── WDL_test.py
│ ├── __init__.py
│ └── xDeepFM_test.py
├── utils.py
├── utils_mtl.py
└── utils_test.py
SYMBOL INDEX (463 symbols across 98 files)
FILE: deepctr/contrib/rnn.py
function _like_rnncell_ (line 56) | def _like_rnncell_(cell):
function _transpose_batch_time (line 78) | def _transpose_batch_time(x):
function _best_effort_input_batch_size (line 117) | def _best_effort_input_batch_size(flat_input):
function _infer_state_dtype (line 150) | def _infer_state_dtype(explicit_dtype, state):
function _rnn_step (line 191) | def _rnn_step(
function _reverse_seq (line 367) | def _reverse_seq(input_seq, lengths):
function dynamic_rnn (line 678) | def dynamic_rnn(cell, inputs, att_scores=None, sequence_length=None, ini...
function _dynamic_rnn_loop (line 893) | def _dynamic_rnn_loop(cell,
FILE: deepctr/contrib/rnn_v2.py
function _like_rnncell_ (line 67) | def _like_rnncell_(cell):
function _transpose_batch_time (line 89) | def _transpose_batch_time(x):
function _best_effort_input_batch_size (line 144) | def _best_effort_input_batch_size(flat_input):
function _infer_state_dtype (line 191) | def _infer_state_dtype(explicit_dtype, state):
function _rnn_step (line 247) | def _rnn_step(
function _reverse_seq (line 478) | def _reverse_seq(input_seq, lengths):
function dynamic_rnn (line 802) | def dynamic_rnn(cell, inputs, att_scores=None, sequence_length=None, ini...
function _dynamic_rnn_loop (line 1149) | def _dynamic_rnn_loop(cell,
FILE: deepctr/contrib/utils.py
class _Linear_ (line 14) | class _Linear_(object):
method __init__ (line 43) | def __init__(self,
method __call__ (line 124) | def __call__(self, args):
class QAAttGRUCell (line 149) | class QAAttGRUCell(RNNCell):
method __init__ (line 172) | def __init__(self,
method state_size (line 199) | def state_size(self):
method output_size (line 204) | def output_size(self):
method __call__ (line 208) | def __call__(self, inputs, state, att_score):
method call (line 212) | def call(self, inputs, state, att_score=None):
class VecAttGRUCell (line 264) | class VecAttGRUCell(RNNCell):
method __init__ (line 287) | def __init__(self,
method state_size (line 314) | def state_size(self):
method output_size (line 319) | def output_size(self):
method __call__ (line 323) | def __call__(self, inputs, state, att_score):
method call (line 327) | def call(self, inputs, state, att_score=None):
FILE: deepctr/estimator/feature_column.py
function linear_model (line 7) | def linear_model(features, linear_feature_columns):
function get_linear_logit (line 15) | def get_linear_logit(features, linear_feature_columns, l2_reg_linear=0):
function input_from_feature_columns (line 30) | def input_from_feature_columns(features, feature_columns, l2_reg_embeddi...
function is_embedding (line 47) | def is_embedding(feature_column):
FILE: deepctr/estimator/inputs.py
function input_fn_pandas (line 4) | def input_fn_pandas(df, features, label=None, batch_size=256, num_epochs...
function input_fn_tfrecord (line 22) | def input_fn_tfrecord(filenames, feature_description, label=None, batch_...
FILE: deepctr/estimator/models/afm.py
function AFMEstimator (line 20) | def AFMEstimator(linear_feature_columns, dnn_feature_columns, use_attent...
FILE: deepctr/estimator/models/autoint.py
function AutoIntEstimator (line 21) | def AutoIntEstimator(linear_feature_columns, dnn_feature_columns, att_la...
FILE: deepctr/estimator/models/ccpm.py
function CCPMEstimator (line 21) | def CCPMEstimator(linear_feature_columns, dnn_feature_columns, conv_kern...
FILE: deepctr/estimator/models/dcn.py
function DCNEstimator (line 18) | def DCNEstimator(linear_feature_columns, dnn_feature_columns, cross_num=...
FILE: deepctr/estimator/models/deepfefm.py
function DeepFEFMEstimator (line 21) | def DeepFEFMEstimator(linear_feature_columns, dnn_feature_columns,
FILE: deepctr/estimator/models/deepfm.py
function DeepFMEstimator (line 20) | def DeepFMEstimator(linear_feature_columns, dnn_feature_columns, dnn_hid...
FILE: deepctr/estimator/models/fibinet.py
function FiBiNETEstimator (line 20) | def FiBiNETEstimator(linear_feature_columns, dnn_feature_columns, biline...
FILE: deepctr/estimator/models/fnn.py
function FNNEstimator (line 17) | def FNNEstimator(linear_feature_columns, dnn_feature_columns, dnn_hidden...
FILE: deepctr/estimator/models/fwfm.py
function FwFMEstimator (line 22) | def FwFMEstimator(linear_feature_columns, dnn_feature_columns, dnn_hidde...
FILE: deepctr/estimator/models/nfm.py
function NFMEstimator (line 18) | def NFMEstimator(linear_feature_columns, dnn_feature_columns, dnn_hidden...
FILE: deepctr/estimator/models/pnn.py
function PNNEstimator (line 19) | def PNNEstimator(dnn_feature_columns, dnn_hidden_units=(256, 128, 64), l...
FILE: deepctr/estimator/models/wdl.py
function WDLEstimator (line 18) | def WDLEstimator(linear_feature_columns, dnn_feature_columns, dnn_hidden...
FILE: deepctr/estimator/models/xdeepfm.py
function xDeepFMEstimator (line 18) | def xDeepFMEstimator(linear_feature_columns, dnn_feature_columns, dnn_hi...
FILE: deepctr/estimator/utils.py
function _summary_key (line 9) | def _summary_key(head_name, val):
class Head (line 13) | class Head(_Head):
method __init__ (line 15) | def __init__(self, task,
method name (line 21) | def name(self):
method logits_dimension (line 25) | def logits_dimension(self):
method _eval_metric_ops (line 28) | def _eval_metric_ops(self,
method create_loss (line 73) | def create_loss(self, features, mode, logits, labels):
method create_estimator_spec (line 82) | def create_estimator_spec(
function deepctr_model_fn (line 121) | def deepctr_model_fn(features, mode, logits, labels, task, linear_optimi...
function get_train_op_fn (line 134) | def get_train_op_fn(linear_optimizer, dnn_optimizer):
function variable_scope (line 165) | def variable_scope(name_or_scope):
function get_collection (line 171) | def get_collection(key, scope=None):
function get_GraphKeys (line 178) | def get_GraphKeys():
function get_losses (line 185) | def get_losses():
function input_layer (line 192) | def input_layer(features, feature_columns):
function get_metrics (line 199) | def get_metrics():
function to_float (line 206) | def to_float(x, name="ToFloat"):
function summary_scalar (line 213) | def summary_scalar(name, data):
FILE: deepctr/feature_column.py
class SparseFeat (line 17) | class SparseFeat(namedtuple('SparseFeat',
method __new__ (line 23) | def __new__(cls, name, vocabulary_size, embedding_dim=4, use_hash=Fals...
method __hash__ (line 39) | def __hash__(self):
class VarLenSparseFeat (line 43) | class VarLenSparseFeat(namedtuple('VarLenSparseFeat',
method __new__ (line 47) | def __new__(cls, sparsefeat, maxlen, combiner="mean", length_name=None...
method name (line 52) | def name(self):
method vocabulary_size (line 56) | def vocabulary_size(self):
method embedding_dim (line 60) | def embedding_dim(self):
method use_hash (line 64) | def use_hash(self):
method vocabulary_path (line 68) | def vocabulary_path(self):
method dtype (line 72) | def dtype(self):
method embeddings_initializer (line 76) | def embeddings_initializer(self):
method embedding_name (line 80) | def embedding_name(self):
method group_name (line 84) | def group_name(self):
method trainable (line 88) | def trainable(self):
method __hash__ (line 91) | def __hash__(self):
class DenseFeat (line 95) | class DenseFeat(namedtuple('DenseFeat', ['name', 'dimension', 'dtype', '...
method __new__ (line 108) | def __new__(cls, name, dimension=1, dtype="float32", transform_fn=None):
method __hash__ (line 111) | def __hash__(self):
function get_feature_names (line 123) | def get_feature_names(feature_columns):
function build_input_features (line 128) | def build_input_features(feature_columns, prefix=''):
function get_linear_logit (line 152) | def get_linear_logit(features, feature_columns, units=1, use_bias=False,...
function input_from_feature_columns (line 194) | def input_from_feature_columns(features, feature_columns, l2_reg, seed, ...
FILE: deepctr/inputs.py
function get_inputs_list (line 19) | def get_inputs_list(inputs):
function create_embedding_dict (line 23) | def create_embedding_dict(sparse_feature_columns, varlen_sparse_feature_...
function get_embedding_vec_list (line 48) | def get_embedding_vec_list(embedding_dict, input_dict, sparse_feature_co...
function create_embedding_matrix (line 63) | def create_embedding_matrix(feature_columns, l2_reg, seed, prefix="", se...
function embedding_lookup (line 75) | def embedding_lookup(sparse_embedding_dict, sparse_input_dict, sparse_fe...
function varlen_embedding_lookup (line 94) | def varlen_embedding_lookup(embedding_dict, sequence_input_dict, varlen_...
function get_varlen_pooling_list (line 107) | def get_varlen_pooling_list(embedding_dict, features, varlen_sparse_feat...
function get_dense_input (line 135) | def get_dense_input(features, feature_columns):
function mergeDict (line 149) | def mergeDict(a, b):
FILE: deepctr/layers/activation.py
class Dice (line 28) | class Dice(Layer):
method __init__ (line 46) | def __init__(self, axis=-1, epsilon=1e-9, **kwargs):
method build (line 51) | def build(self, input_shape):
method call (line 59) | def call(self, inputs, training=None, **kwargs):
method compute_output_shape (line 66) | def compute_output_shape(self, input_shape):
method get_config (line 69) | def get_config(self, ):
function activation_layer (line 75) | def activation_layer(activation):
FILE: deepctr/layers/core.py
class LocalActivationUnit (line 28) | class LocalActivationUnit(Layer):
method __init__ (line 55) | def __init__(self, hidden_units=(64, 32), activation='sigmoid', l2_reg...
method build (line 66) | def build(self, input_shape):
method call (line 94) | def call(self, inputs, training=None, **kwargs):
method compute_output_shape (line 110) | def compute_output_shape(self, input_shape):
method compute_mask (line 113) | def compute_mask(self, inputs, mask):
method get_config (line 116) | def get_config(self, ):
class DNN (line 123) | class DNN(Layer):
method __init__ (line 148) | def __init__(self, hidden_units, activation='relu', l2_reg=0, dropout_...
method build (line 160) | def build(self, input_shape):
method call (line 189) | def call(self, inputs, training=None, **kwargs):
method compute_output_shape (line 210) | def compute_output_shape(self, input_shape):
method get_config (line 218) | def get_config(self, ):
class PredictionLayer (line 226) | class PredictionLayer(Layer):
method __init__ (line 234) | def __init__(self, task='binary', use_bias=True, **kwargs):
method build (line 241) | def build(self, input_shape):
method call (line 250) | def call(self, inputs, **kwargs):
method compute_output_shape (line 261) | def compute_output_shape(self, input_shape):
method get_config (line 264) | def get_config(self, ):
class RegulationModule (line 270) | class RegulationModule(Layer):
method __init__ (line 287) | def __init__(self, tau=1.0, **kwargs):
method build (line 293) | def build(self, input_shape):
method call (line 304) | def call(self, inputs, **kwargs):
method compute_output_shape (line 314) | def compute_output_shape(self, input_shape):
method get_config (line 317) | def get_config(self):
FILE: deepctr/layers/interaction.py
class AFMLayer (line 33) | class AFMLayer(Layer):
method __init__ (line 59) | def __init__(self, attention_factor=4, l2_reg_w=0, dropout_rate=0, see...
method build (line 66) | def build(self, input_shape):
method call (line 110) | def call(self, inputs, training=None, **kwargs):
method compute_output_shape (line 142) | def compute_output_shape(self, input_shape):
method get_config (line 149) | def get_config(self, ):
class BiInteractionPooling (line 157) | class BiInteractionPooling(Layer):
method __init__ (line 171) | def __init__(self, **kwargs):
method build (line 175) | def build(self, input_shape):
method call (line 184) | def call(self, inputs, **kwargs):
method compute_output_shape (line 199) | def compute_output_shape(self, input_shape):
class CIN (line 203) | class CIN(Layer):
method __init__ (line 226) | def __init__(self, layer_size=(128, 128), activation='relu', split_hal...
method build (line 237) | def build(self, input_shape):
method call (line 271) | def call(self, inputs, **kwargs):
method compute_output_shape (line 321) | def compute_output_shape(self, input_shape):
method get_config (line 329) | def get_config(self, ):
class CrossNet (line 338) | class CrossNet(Layer):
method __init__ (line 361) | def __init__(self, layer_num=2, parameterization='vector', l2_reg=0, s...
method build (line 369) | def build(self, input_shape):
method call (line 399) | def call(self, inputs, **kwargs):
method get_config (line 420) | def get_config(self, ):
method compute_output_shape (line 428) | def compute_output_shape(self, input_shape):
class CrossNetMix (line 432) | class CrossNetMix(Layer):
method __init__ (line 458) | def __init__(self, low_rank=32, num_experts=4, layer_num=2, l2_reg=0, ...
method build (line 466) | def build(self, input_shape):
method call (line 505) | def call(self, inputs, **kwargs):
method get_config (line 545) | def get_config(self, ):
method compute_output_shape (line 553) | def compute_output_shape(self, input_shape):
class FM (line 557) | class FM(Layer):
method __init__ (line 571) | def __init__(self, **kwargs):
method build (line 575) | def build(self, input_shape):
method call (line 582) | def call(self, inputs, **kwargs):
method compute_output_shape (line 600) | def compute_output_shape(self, input_shape):
class InnerProductLayer (line 604) | class InnerProductLayer(Layer):
method __init__ (line 621) | def __init__(self, reduce_sum=True, **kwargs):
method build (line 625) | def build(self, input_shape):
method call (line 649) | def call(self, inputs, **kwargs):
method compute_output_shape (line 674) | def compute_output_shape(self, input_shape):
method get_config (line 684) | def get_config(self, ):
class InteractingLayer (line 691) | class InteractingLayer(Layer):
method __init__ (line 711) | def __init__(self, att_embedding_size=8, head_num=2, use_res=True, sca...
method build (line 721) | def build(self, input_shape):
method call (line 743) | def call(self, inputs, **kwargs):
method compute_output_shape (line 775) | def compute_output_shape(self, input_shape):
method get_config (line 779) | def get_config(self, ):
class OutterProductLayer (line 787) | class OutterProductLayer(Layer):
method __init__ (line 806) | def __init__(self, kernel_type='mat', seed=1024, **kwargs):
method build (line 813) | def build(self, input_shape):
method call (line 855) | def call(self, inputs, **kwargs):
method compute_output_shape (line 919) | def compute_output_shape(self, input_shape):
method get_config (line 924) | def get_config(self, ):
class FGCNNLayer (line 931) | class FGCNNLayer(Layer):
method __init__ (line 945) | def __init__(self, filters=(14, 16,), kernel_width=(7, 7,), new_maps=(...
method build (line 956) | def build(self, input_shape):
method call (line 988) | def call(self, inputs, **kwargs):
method compute_output_shape (line 1016) | def compute_output_shape(self, input_shape):
method get_config (line 1028) | def get_config(self, ):
method _conv_output_shape (line 1035) | def _conv_output_shape(self, input_shape, kernel_size):
method _pooling_output_shape (line 1049) | def _pooling_output_shape(self, input_shape, pool_size):
class SENETLayer (line 1061) | class SENETLayer(Layer):
method __init__ (line 1080) | def __init__(self, reduction_ratio=3, seed=1024, **kwargs):
method build (line 1086) | def build(self, input_shape):
method call (line 1107) | def call(self, inputs, training=None, **kwargs):
method compute_output_shape (line 1122) | def compute_output_shape(self, input_shape):
method compute_mask (line 1126) | def compute_mask(self, inputs, mask=None):
method get_config (line 1129) | def get_config(self, ):
class BilinearInteraction (line 1136) | class BilinearInteraction(Layer):
method __init__ (line 1155) | def __init__(self, bilinear_type="interaction", seed=1024, **kwargs):
method build (line 1161) | def build(self, input_shape):
method call (line 1184) | def call(self, inputs, **kwargs):
method compute_output_shape (line 1205) | def compute_output_shape(self, input_shape):
method get_config (line 1211) | def get_config(self, ):
class FieldWiseBiInteraction (line 1218) | class FieldWiseBiInteraction(Layer):
method __init__ (line 1237) | def __init__(self, use_bias=True, seed=1024, **kwargs):
method build (line 1243) | def build(self, input_shape):
method call (line 1277) | def call(self, inputs, **kwargs):
method compute_output_shape (line 1335) | def compute_output_shape(self, input_shape):
method get_config (line 1338) | def get_config(self, ):
class FwFMLayer (line 1345) | class FwFMLayer(Layer):
method __init__ (line 1363) | def __init__(self, num_fields=4, regularizer=0.000001, **kwargs):
method build (line 1368) | def build(self, input_shape):
method call (line 1385) | def call(self, inputs, **kwargs):
method compute_output_shape (line 1410) | def compute_output_shape(self, input_shape):
method get_config (line 1413) | def get_config(self):
class FEFMLayer (line 1422) | class FEFMLayer(Layer):
method __init__ (line 1440) | def __init__(self, regularizer, **kwargs):
method build (line 1444) | def build(self, input_shape):
method call (line 1463) | def call(self, inputs, **kwargs):
method compute_output_shape (line 1484) | def compute_output_shape(self, input_shape):
method get_config (line 1488) | def get_config(self):
class BridgeModule (line 1496) | class BridgeModule(Layer):
method __init__ (line 1515) | def __init__(self, bridge_type='hadamard_product', activation='relu', ...
method build (line 1521) | def build(self, input_shape):
method call (line 1536) | def call(self, inputs, **kwargs):
method compute_output_shape (line 1549) | def compute_output_shape(self, input_shape):
method get_config (line 1552) | def get_config(self):
FILE: deepctr/layers/normalization.py
class LayerNormalization (line 18) | class LayerNormalization(Layer):
method __init__ (line 19) | def __init__(self, axis=-1, eps=1e-9, center=True,
method build (line 27) | def build(self, input_shape):
method call (line 34) | def call(self, inputs):
method compute_output_shape (line 45) | def compute_output_shape(self, input_shape):
method get_config (line 48) | def get_config(self, ):
FILE: deepctr/layers/sequence.py
class SequencePoolingLayer (line 31) | class SequencePoolingLayer(Layer):
method __init__ (line 50) | def __init__(self, mode='mean', supports_masking=False, **kwargs):
method build (line 60) | def build(self, input_shape):
method call (line 66) | def call(self, seq_value_len_list, mask=None, **kwargs):
method compute_output_shape (line 98) | def compute_output_shape(self, input_shape):
method compute_mask (line 104) | def compute_mask(self, inputs, mask):
method get_config (line 107) | def get_config(self, ):
class WeightedSequenceLayer (line 113) | class WeightedSequenceLayer(Layer):
method __init__ (line 134) | def __init__(self, weight_normalization=True, supports_masking=False, ...
method build (line 139) | def build(self, input_shape):
method call (line 145) | def call(self, input_list, mask=None, **kwargs):
method compute_output_shape (line 175) | def compute_output_shape(self, input_shape):
method compute_mask (line 178) | def compute_mask(self, inputs, mask):
method get_config (line 184) | def get_config(self, ):
class AttentionSequencePoolingLayer (line 190) | class AttentionSequencePoolingLayer(Layer):
method __init__ (line 218) | def __init__(self, att_hidden_units=(80, 40), att_activation='sigmoid'...
method build (line 229) | def build(self, input_shape):
method call (line 251) | def call(self, inputs, mask=None, training=None, **kwargs):
method compute_output_shape (line 290) | def compute_output_shape(self, input_shape):
method compute_mask (line 296) | def compute_mask(self, inputs, mask):
method get_config (line 299) | def get_config(self, ):
class BiLSTM (line 308) | class BiLSTM(Layer):
method __init__ (line 331) | def __init__(self, units, layers=2, res_layers=0, dropout_rate=0.2, me...
method build (line 347) | def build(self, input_shape):
method call (line 365) | def call(self, inputs, mask=None, **kwargs):
method compute_output_shape (line 401) | def compute_output_shape(self, input_shape):
method compute_mask (line 410) | def compute_mask(self, inputs, mask):
method get_config (line 413) | def get_config(self, ):
class Transformer (line 421) | class Transformer(Layer):
method __init__ (line 451) | def __init__(self, att_embedding_size=1, head_num=8, dropout_rate=0.0,...
method build (line 471) | def build(self, input_shape):
method call (line 513) | def call(self, inputs, mask=None, training=None, **kwargs):
method compute_output_shape (line 627) | def compute_output_shape(self, input_shape):
method compute_mask (line 631) | def compute_mask(self, inputs, mask=None):
method get_config (line 634) | def get_config(self, ):
class PositionEncoding (line 644) | class PositionEncoding(Layer):
method __init__ (line 645) | def __init__(self, pos_embedding_trainable=True,
method build (line 653) | def build(self, input_shape):
method call (line 673) | def call(self, inputs, mask=None):
method compute_output_shape (line 681) | def compute_output_shape(self, input_shape):
method compute_mask (line 685) | def compute_mask(self, inputs, mask=None):
method get_config (line 688) | def get_config(self, ):
class BiasEncoding (line 696) | class BiasEncoding(Layer):
method __init__ (line 697) | def __init__(self, sess_max_count, seed=1024, **kwargs):
method build (line 702) | def build(self, input_shape):
method call (line 729) | def call(self, inputs, mask=None):
method compute_output_shape (line 740) | def compute_output_shape(self, input_shape):
method compute_mask (line 744) | def compute_mask(self, inputs, mask=None):
method get_config (line 747) | def get_config(self, ):
class DynamicGRU (line 754) | class DynamicGRU(Layer):
method __init__ (line 755) | def __init__(self, num_units=None, gru_type='GRU', return_sequence=Tru...
method build (line 762) | def build(self, input_shape):
method call (line 780) | def call(self, input_list):
method compute_output_shape (line 799) | def compute_output_shape(self, input_shape):
method get_config (line 806) | def get_config(self, ):
class KMaxPooling (line 812) | class KMaxPooling(Layer):
method __init__ (line 828) | def __init__(self, k=1, axis=-1, **kwargs):
method build (line 834) | def build(self, input_shape):
method call (line 847) | def call(self, inputs):
method compute_output_shape (line 860) | def compute_output_shape(self, input_shape):
method get_config (line 865) | def get_config(self, ):
FILE: deepctr/layers/utils.py
class NoMask (line 26) | class NoMask(Layer):
method __init__ (line 27) | def __init__(self, **kwargs):
method build (line 30) | def build(self, input_shape):
method call (line 34) | def call(self, x, mask=None, **kwargs):
method compute_mask (line 37) | def compute_mask(self, inputs, mask):
class Hash (line 41) | class Hash(Layer):
method __init__ (line 75) | def __init__(self, num_buckets, mask_zero=False, vocabulary_path=None,...
method build (line 85) | def build(self, input_shape):
method call (line 89) | def call(self, x, mask=None, **kwargs):
method compute_output_shape (line 114) | def compute_output_shape(self, input_shape):
method get_config (line 117) | def get_config(self, ):
class Linear (line 124) | class Linear(Layer):
method __init__ (line 126) | def __init__(self, l2_reg=0.0, mode=0, use_bias=False, seed=1024, **kw...
method build (line 137) | def build(self, input_shape):
method call (line 160) | def call(self, inputs, **kwargs):
method compute_output_shape (line 177) | def compute_output_shape(self, input_shape):
method compute_mask (line 180) | def compute_mask(self, inputs, mask):
method get_config (line 183) | def get_config(self, ):
class Concat (line 189) | class Concat(Layer):
method __init__ (line 190) | def __init__(self, axis, supports_masking=True, **kwargs):
method call (line 195) | def call(self, inputs):
method compute_mask (line 198) | def compute_mask(self, inputs, mask=None):
method get_config (line 230) | def get_config(self, ):
function concat_func (line 236) | def concat_func(inputs, axis=-1, mask=False):
function reduce_mean (line 245) | def reduce_mean(input_tensor,
function reduce_sum (line 263) | def reduce_sum(input_tensor,
function reduce_max (line 281) | def reduce_max(input_tensor,
function div (line 299) | def div(x, y, name=None):
function softmax (line 306) | def softmax(logits, dim=-1, name=None):
class _Add (line 313) | class _Add(Layer):
method __init__ (line 314) | def __init__(self, **kwargs):
method build (line 317) | def build(self, input_shape):
method call (line 321) | def call(self, inputs, **kwargs):
function add_func (line 328) | def add_func(inputs):
function combined_dnn_input (line 336) | def combined_dnn_input(sparse_embedding_list, dense_value_list):
FILE: deepctr/models/afm.py
function AFM (line 19) | def AFM(linear_feature_columns, dnn_feature_columns, fm_group=DEFAULT_GR...
FILE: deepctr/models/autoint.py
function AutoInt (line 21) | def AutoInt(linear_feature_columns, dnn_feature_columns, att_layer_num=3...
FILE: deepctr/models/ccpm.py
function CCPM (line 22) | def CCPM(linear_feature_columns, dnn_feature_columns, conv_kernel_width=...
FILE: deepctr/models/dcn.py
function DCN (line 22) | def DCN(linear_feature_columns, dnn_feature_columns, cross_num=2, cross_...
FILE: deepctr/models/dcnmix.py
function DCNMix (line 22) | def DCNMix(linear_feature_columns, dnn_feature_columns, cross_num=2,
FILE: deepctr/models/deepfefm.py
function DeepFEFM (line 25) | def DeepFEFM(linear_feature_columns, dnn_feature_columns, use_fefm=True,
FILE: deepctr/models/deepfm.py
function DeepFM (line 22) | def DeepFM(linear_feature_columns, dnn_feature_columns, fm_group=(DEFAUL...
FILE: deepctr/models/difm.py
function DIFM (line 20) | def DIFM(linear_feature_columns, dnn_feature_columns,
FILE: deepctr/models/edcn.py
function EDCN (line 18) | def EDCN(linear_feature_columns,
FILE: deepctr/models/fgcnn.py
function unstack (line 22) | def unstack(input_tensor):
function FGCNN (line 27) | def FGCNN(linear_feature_columns, dnn_feature_columns, conv_kernel_width...
FILE: deepctr/models/fibinet.py
function FiBiNET (line 19) | def FiBiNET(linear_feature_columns, dnn_feature_columns, bilinear_type='...
FILE: deepctr/models/flen.py
function FLEN (line 22) | def FLEN(linear_feature_columns,
FILE: deepctr/models/fnn.py
function FNN (line 17) | def FNN(linear_feature_columns, dnn_feature_columns, dnn_hidden_units=(2...
FILE: deepctr/models/fwfm.py
function FwFM (line 23) | def FwFM(linear_feature_columns, dnn_feature_columns, fm_group=(DEFAULT_...
FILE: deepctr/models/ifm.py
function IFM (line 21) | def IFM(linear_feature_columns, dnn_feature_columns, dnn_hidden_units=(2...
FILE: deepctr/models/mlr.py
function MLR (line 17) | def MLR(region_feature_columns, base_feature_columns=None, region_num=4,
function get_region_score (line 60) | def get_region_score(features, feature_columns, region_number, l2_reg, s...
function get_learner_score (line 67) | def get_learner_score(features, feature_columns, region_number, l2_reg, ...
FILE: deepctr/models/multitask/esmm.py
function ESMM (line 19) | def ESMM(dnn_feature_columns, tower_dnn_hidden_units=(256, 128, 64), l2_...
FILE: deepctr/models/multitask/mmoe.py
function MMOE (line 20) | def MMOE(dnn_feature_columns, num_experts=3, expert_dnn_hidden_units=(25...
FILE: deepctr/models/multitask/ple.py
function PLE (line 20) | def PLE(dnn_feature_columns, shared_expert_num=1, specific_expert_num=1,...
FILE: deepctr/models/multitask/sharedbottom.py
function SharedBottom (line 19) | def SharedBottom(dnn_feature_columns, bottom_dnn_hidden_units=(256, 128)...
FILE: deepctr/models/nfm.py
function NFM (line 18) | def NFM(linear_feature_columns, dnn_feature_columns, dnn_hidden_units=(2...
FILE: deepctr/models/onn.py
function ONN (line 32) | def ONN(linear_feature_columns, dnn_feature_columns, dnn_hidden_units=(2...
function feature_embedding (line 111) | def feature_embedding(fc_i, fc_j, embedding_dict, input_feature):
FILE: deepctr/models/pnn.py
function PNN (line 19) | def PNN(dnn_feature_columns, dnn_hidden_units=(256, 128, 64), l2_reg_emb...
FILE: deepctr/models/sequence/bst.py
function BST (line 21) | def BST(dnn_feature_columns, history_feature_list, transformer_num=1, at...
FILE: deepctr/models/sequence/dien.py
function auxiliary_loss (line 22) | def auxiliary_loss(h_states, click_seq, noclick_seq, mask, stag=None):
function interest_evolution (line 66) | def interest_evolution(concat_behavior, deep_input_item, user_behavior_l...
function DIEN (line 112) | def DIEN(dnn_feature_columns, history_feature_list,
FILE: deepctr/models/sequence/din.py
function DIN (line 20) | def DIN(dnn_feature_columns, history_feature_list, dnn_use_bn=False,
FILE: deepctr/models/sequence/dsin.py
function DSIN (line 26) | def DSIN(dnn_feature_columns, sess_feature_list, sess_max_count=5, bias_...
function sess_interest_division (line 145) | def sess_interest_division(sparse_embedding_dict, user_behavior_input_di...
function sess_interest_extractor (line 161) | def sess_interest_extractor(tr_input, sess_max_count, TR):
FILE: deepctr/models/wdl.py
function WDL (line 18) | def WDL(linear_feature_columns, dnn_feature_columns, dnn_hidden_units=(2...
FILE: deepctr/models/xdeepfm.py
function xDeepFM (line 18) | def xDeepFM(linear_feature_columns, dnn_feature_columns, dnn_hidden_unit...
FILE: deepctr/utils.py
function check_version (line 21) | def check_version(version):
FILE: examples/gen_tfrecords.py
function make_example (line 3) | def make_example(line, sparse_feature_name, dense_feature_name, label_na...
function write_tfrecord (line 12) | def write_tfrecord(filename, df, sparse_feature_names, dense_feature_nam...
FILE: examples/run_dien.py
function get_xy_fd (line 8) | def get_xy_fd(use_neg=False, hash_flag=False):
FILE: examples/run_din.py
function get_xy_fd (line 7) | def get_xy_fd():
FILE: examples/run_dsin.py
function get_xy_fd (line 8) | def get_xy_fd(hash_flag=False):
FILE: examples/run_multivalue_movielens.py
function split (line 10) | def split(x):
FILE: tests/feature_test.py
function test_long_dense_vector (line 6) | def test_long_dense_vector():
function test_feature_column_sparsefeat_vocabulary_path (line 23) | def test_feature_column_sparsefeat_vocabulary_path():
FILE: tests/layers/activations_test.py
function test_dice (line 10) | def test_dice():
FILE: tests/layers/core_test.py
function test_LocalActivationUnit (line 22) | def test_LocalActivationUnit(hidden_units, activation):
function test_DNN (line 39) | def test_DNN(hidden_units, use_bn):
function test_PredictionLayer (line 53) | def test_PredictionLayer(task, use_bias):
function test_test_PredictionLayer_invalid (line 60) | def test_test_PredictionLayer_invalid():
FILE: tests/layers/interaction_test.py
function test_FEFMLayer (line 17) | def test_FEFMLayer():
function test_FwFM (line 27) | def test_FwFM(reg_strength):
function test_CrossNet (line 40) | def test_CrossNet(layer_num, ):
function test_InnerProductLayer (line 59) | def test_InnerProductLayer(reduce_sum):
function test_OutterProductLayer (line 71) | def test_OutterProductLayer(kernel_type):
function test_BiInteractionPooling (line 77) | def test_BiInteractionPooling():
function test_FM (line 83) | def test_FM():
function test_AFMLayer (line 89) | def test_AFMLayer():
function test_CIN (line 100) | def test_CIN(layer_size, split_half):
function test_InteractingLayer (line 122) | def test_InteractingLayer(head_num, use_res, ):
function test_FGCNNLayer (line 129) | def test_FGCNNLayer():
function test_BilinearInteraction (line 146) | def test_BilinearInteraction(bilinear_type):
FILE: tests/layers/normalization_test.py
function test_LayerNormalization (line 17) | def test_LayerNormalization(axis):
FILE: tests/layers/sequence_test.py
function test_AttentionSequencePoolingLayer (line 30) | def test_AttentionSequencePoolingLayer(weight_normalization):
function test_SequencePoolingLayer (line 46) | def test_SequencePoolingLayer(mode, supports_masking, input_shape):
function test_BiLSTM (line 77) | def test_BiLSTM(merge_mode):
function test_Transformer (line 88) | def test_Transformer(attention_type):
function test_KMaxPooling (line 97) | def test_KMaxPooling():
function test_PositionEncoding (line 109) | def test_PositionEncoding(pos_embedding_trainable, zero_pad):
FILE: tests/layers/utils_test.py
function test_Hash (line 25) | def test_Hash(num_buckets, mask_zero, vocabulary_path, input_data, expec...
function test_Linear (line 36) | def test_Linear():
FILE: tests/models/AFM_test.py
function test_AFM (line 13) | def test_AFM(use_attention, sparse_feature_num, dense_feature_num):
function test_AFMEstimator (line 29) | def test_AFMEstimator(use_attention, sparse_feature_num, dense_feature_n...
FILE: tests/models/AutoInt_test.py
function test_AutoInt (line 14) | def test_AutoInt(att_layer_num, dnn_hidden_units, sparse_feature_num):
function test_AutoIntEstimator (line 31) | def test_AutoIntEstimator(att_layer_num, dnn_hidden_units, sparse_featur...
FILE: tests/models/BST_test.py
function test_BST (line 6) | def test_BST():
FILE: tests/models/CCPM_test.py
function test_CCPM (line 13) | def test_CCPM(sparse_feature_num, dense_feature_num):
function test_CCPM_without_seq (line 32) | def test_CCPM_without_seq(sparse_feature_num, dense_feature_num):
function test_CCPMEstimator_without_seq (line 51) | def test_CCPMEstimator_without_seq(sparse_feature_num, dense_feature_num):
FILE: tests/models/DCNMix_test.py
function test_DCNMix (line 12) | def test_DCNMix(cross_num, hidden_size, sparse_feature_num):
FILE: tests/models/DCN_test.py
function test_DCN (line 13) | def test_DCN(cross_num, hidden_size, sparse_feature_num, cross_parameter...
function test_DCN_2 (line 25) | def test_DCN_2():
function test_DCNEstimator (line 41) | def test_DCNEstimator(cross_num, hidden_size, sparse_feature_num):
FILE: tests/models/DIEN_test.py
function get_xy_fd (line 11) | def get_xy_fd(use_neg=False, hash_flag=False):
function test_DIEN (line 63) | def test_DIEN(gru_type):
function test_DIEN_neg (line 78) | def test_DIEN_neg():
FILE: tests/models/DIFM_test.py
function test_DIFM (line 11) | def test_DIFM(att_head_num, dnn_hidden_units, sparse_feature_num):
FILE: tests/models/DIN_test.py
function get_xy_fd (line 10) | def get_xy_fd(hash_flag=False):
function test_DIN (line 43) | def test_DIN():
FILE: tests/models/DSIN_test.py
function get_xy_fd (line 9) | def get_xy_fd(hash_flag=False):
function test_DSIN (line 56) | def test_DSIN(bias_encoding):
FILE: tests/models/DeepFEFM_test.py
function test_DeepFEFM (line 21) | def test_DeepFEFM(hidden_size, sparse_feature_num, use_fefm, use_linear,...
function test_DeepFEFMEstimator (line 40) | def test_DeepFEFMEstimator(hidden_size, sparse_feature_num):
FILE: tests/models/DeepFM_test.py
function test_DeepFM (line 13) | def test_DeepFM(hidden_size, sparse_feature_num):
function test_DeepFMEstimator (line 30) | def test_DeepFMEstimator(hidden_size, sparse_feature_num):
FILE: tests/models/EDCN_test.py
function test_EDCN (line 16) | def test_EDCN(bridge_type, cross_num, cross_parameterization, sparse_fea...
FILE: tests/models/FGCNN_test.py
function test_FGCNN (line 12) | def test_FGCNN(sparse_feature_num, dense_feature_num):
FILE: tests/models/FLEN_test.py
function test_FLEN (line 13) | def test_FLEN(hidden_size, sparse_feature_num):
FILE: tests/models/FNN_test.py
function test_FNN (line 13) | def test_FNN(sparse_feature_num, dense_feature_num):
function test_FNNEstimator (line 45) | def test_FNNEstimator(sparse_feature_num, dense_feature_num):
FILE: tests/models/FiBiNET_test.py
function test_FiBiNET (line 12) | def test_FiBiNET(bilinear_type):
function test_FiBiNETEstimator (line 26) | def test_FiBiNETEstimator(bilinear_type):
FILE: tests/models/FwFM_test.py
function test_FwFM (line 13) | def test_FwFM(hidden_size, sparse_feature_num):
function test_FwFMEstimator (line 28) | def test_FwFMEstimator(hidden_size, sparse_feature_num):
FILE: tests/models/IFM_test.py
function test_IFM (line 13) | def test_IFM(hidden_size, sparse_feature_num):
FILE: tests/models/MLR_test.py
function test_MLRs (line 17) | def test_MLRs(region_sparse, region_dense, base_sparse, base_dense, bias...
function test_MLR (line 32) | def test_MLR():
FILE: tests/models/MTL_test.py
function test_SharedBottom (line 8) | def test_SharedBottom():
function test_ESMM (line 19) | def test_ESMM():
function test_MMOE (line 30) | def test_MMOE():
function test_PLE (line 48) | def test_PLE(num_levels, gate_dnn_hidden_units):
FILE: tests/models/NFM_test.py
function test_NFM (line 11) | def test_NFM(hidden_size, sparse_feature_num):
function test_FNNEstimator (line 26) | def test_FNNEstimator(hidden_size, sparse_feature_num):
FILE: tests/models/ONN_test.py
function test_ONN (line 13) | def test_ONN(sparse_feature_num):
FILE: tests/models/PNN_test.py
function test_PNN (line 12) | def test_PNN(use_inner, use_outter, sparse_feature_num):
function test_PNNEstimator (line 26) | def test_PNNEstimator(use_inner, use_outter, sparse_feature_num):
FILE: tests/models/WDL_test.py
function test_WDL (line 14) | def test_WDL(sparse_feature_num, dense_feature_num):
function test_WDLEstimator (line 32) | def test_WDLEstimator(sparse_feature_num, dense_feature_num):
FILE: tests/models/xDeepFM_test.py
function test_xDeepFM (line 15) | def test_xDeepFM(dnn_hidden_units, cin_layer_size, cin_split_half, cin_a...
function test_xDeepFMEstimator (line 44) | def test_xDeepFMEstimator(dnn_hidden_units, cin_layer_size, cin_split_ha...
FILE: tests/utils.py
function test_estimator_version (line 22) | def test_estimator_version(tf_version):
function gen_sequence (line 33) | def gen_sequence(dim, max_len, sample_size):
function get_test_data (line 38) | def get_test_data(sample_size=1000, embedding_size=4, sparse_feature_num...
function layer_test (line 108) | def layer_test(layer_cls, kwargs=None, input_shape=None, input_dtype=None,
function has_arg (line 279) | def has_arg(fn, name, accept_all=False):
function check_model (line 356) | def check_model(model, model_name, x, y, check_model_io=True):
function get_test_data_estimator (line 384) | def get_test_data_estimator(sample_size=1000, embedding_size=4, sparse_f...
function check_estimator (line 416) | def check_estimator(model, input_fn):
FILE: tests/utils_mtl.py
function get_mtl_test_data (line 13) | def get_mtl_test_data(sample_size=10, embedding_size=4, sparse_feature_n...
function check_mtl_model (line 58) | def check_mtl_model(model, model_name, x, y_list, task_types, check_mode...
FILE: tests/utils_test.py
function test_check_version (line 4) | def test_check_version():
Condensed preview — 220 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (922K chars).
[
{
"path": ".gitattributes",
"chars": 378,
"preview": "# Auto detect text files and perform LF normalization\n* text=auto\n\n# Custom for Visual Studio\n*.cs diff=csharp\n\n# St"
},
{
"path": ".github/ISSUE_TEMPLATE/bug_report.md",
"chars": 585,
"preview": "---\nname: Bug report\nabout: Create a report to help us improve\ntitle: ''\nlabels: ''\nassignees: ''\n\n---\n\n**Describe the b"
},
{
"path": ".github/ISSUE_TEMPLATE/feature_request.md",
"chars": 620,
"preview": "---\nname: Feature request\nabout: Suggest an idea for this project\ntitle: ''\nlabels: enhancement&feature request\nassignee"
},
{
"path": ".github/ISSUE_TEMPLATE/question.md",
"chars": 598,
"preview": "---\nname: Question\nabout: Ask any question ~\ntitle: ''\nlabels: question\nassignees: ''\n\n---\nPlease refer to the [FAQ](htt"
},
{
"path": ".github/workflows/ci.yml",
"chars": 2601,
"preview": "name: CI_TF2\n\non:\n push:\n path:\n - 'deepctr/*'\n - 'tests/*'\n pull_request:\n path:\n - 'deepctr/*'\n"
},
{
"path": ".github/workflows/ci2.yml",
"chars": 2624,
"preview": "name: CI_TF1\n\non:\n push:\n path:\n - 'deepctr/*'\n - 'tests/*'\n pull_request:\n path:\n - 'deepctr/*'\n"
},
{
"path": ".gitignore",
"chars": 1784,
"preview": "*.h5\n*.ipynb\n.pytest_cache/\n.vscode/\ntests/unused/*\n# Byte-compiled / optimized / DLL files\n__pycache__/\n*.py[cod]\n*$py."
},
{
"path": ".readthedocs.yml",
"chars": 50,
"preview": "build:\n image: latest\n\npython:\n version: 3.6"
},
{
"path": ".travis.yml",
"chars": 1506,
"preview": "#sudo: required\n#dist: trusty xenial\nlanguage: python\n\npython:\n - \"2.7\" #time out\n #- \"3.4\"\n - \"3.5\"\n - \"3.6\"\n #- \""
},
{
"path": "CONTRIBUTING.md",
"chars": 1323,
"preview": "This project is under development and we need developers to participate in.\n# Join us\nIf you\n\n- familiar with and intere"
},
{
"path": "LICENSE",
"chars": 11349,
"preview": " Apache License\n Version 2.0, January 2004\n "
},
{
"path": "README.md",
"chars": 11845,
"preview": "# DeepCTR\n\n[](https://pypi.org/project/deepctr)\n[!"
},
{
"path": "deepctr/__init__.py",
"chars": 83,
"preview": "from .utils import check_version\n\n__version__ = '0.9.3'\ncheck_version(__version__)\n"
},
{
"path": "deepctr/contrib/__init__.py",
"chars": 0,
"preview": ""
},
{
"path": "deepctr/contrib/rnn.py",
"chars": 35500,
"preview": "# Copyright 2015 The TensorFlow Authors. All Rights Reserved.\n\n#\n\n# Licensed under the Apache License, Version 2.0 (the "
},
{
"path": "deepctr/contrib/rnn_v2.py",
"chars": 35834,
"preview": "# Copyright 2015 The TensorFlow Authors. All Rights Reserved.\n\n#\n\n# Licensed under the Apache License, Version 2.0 (the "
},
{
"path": "deepctr/contrib/utils.py",
"chars": 9026,
"preview": "from tensorflow.python.ops import array_ops\nfrom tensorflow.python.ops import init_ops\nfrom tensorflow.python.ops import"
},
{
"path": "deepctr/estimator/__init__.py",
"chars": 21,
"preview": "from .models import *"
},
{
"path": "deepctr/estimator/feature_column.py",
"chars": 2156,
"preview": "import tensorflow as tf\nfrom tensorflow.python.feature_column.feature_column import _EmbeddingColumn\n\nfrom .utils import"
},
{
"path": "deepctr/estimator/inputs.py",
"chars": 2326,
"preview": "import tensorflow as tf\n\n\ndef input_fn_pandas(df, features, label=None, batch_size=256, num_epochs=1, shuffle=False, que"
},
{
"path": "deepctr/estimator/models/__init__.py",
"chars": 434,
"preview": "from .afm import AFMEstimator\nfrom .autoint import AutoIntEstimator\nfrom .ccpm import CCPMEstimator\nfrom .dcn import DCN"
},
{
"path": "deepctr/estimator/models/afm.py",
"chars": 3749,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\n\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Xiao J, Ye H, He X, et al. "
},
{
"path": "deepctr/estimator/models/autoint.py",
"chars": 5578,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\n\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Song W, Shi C, Xiao Z, et a"
},
{
"path": "deepctr/estimator/models/ccpm.py",
"chars": 5183,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\n\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Liu Q, Yu F, Wu S, et al. A"
},
{
"path": "deepctr/estimator/models/dcn.py",
"chars": 5137,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Wang R, Fu B, Fu G, et al. D"
},
{
"path": "deepctr/estimator/models/deepfefm.py",
"chars": 4864,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Harshit Pande\n\nReference:\n [1] Field-Embedded Factorization Machines for Click"
},
{
"path": "deepctr/estimator/models/deepfm.py",
"chars": 4199,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Guo H, Tang R, Ye Y, et al. "
},
{
"path": "deepctr/estimator/models/fibinet.py",
"chars": 4665,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Huang T, Zhang Z, Zhang J. F"
},
{
"path": "deepctr/estimator/models/fnn.py",
"chars": 3813,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Zhang W, Du T, Wang J. Deep "
},
{
"path": "deepctr/estimator/models/fwfm.py",
"chars": 4617,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n Harshit Pande\n\nReference:\n [1] Field-weig"
},
{
"path": "deepctr/estimator/models/nfm.py",
"chars": 4311,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] He X, Chua T S. Neural facto"
},
{
"path": "deepctr/estimator/models/pnn.py",
"chars": 4963,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Qu Y, Cai H, Ren K, et al. P"
},
{
"path": "deepctr/estimator/models/wdl.py",
"chars": 3811,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Cheng H T, Koc L, Harmsen J,"
},
{
"path": "deepctr/estimator/models/xdeepfm.py",
"chars": 5021,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Lian J, Zhou X, Zhang F, et "
},
{
"path": "deepctr/estimator/utils.py",
"chars": 7507,
"preview": "import tensorflow as tf\nfrom tensorflow.python.estimator.canned.head import _Head\nfrom tensorflow.python.estimator.canne"
},
{
"path": "deepctr/feature_column.py",
"chars": 9348,
"preview": "import tensorflow as tf\nfrom collections import namedtuple, OrderedDict\nfrom copy import copy\nfrom itertools import chai"
},
{
"path": "deepctr/inputs.py",
"chars": 6695,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\n\nAuthor:\n Weichen Shen,weichenswc@163.com\n\n\"\"\"\n\nfrom collections import defaultdict\nfrom i"
},
{
"path": "deepctr/layers/__init__.py",
"chars": 2682,
"preview": "import tensorflow as tf\n\nfrom .activation import Dice\nfrom .core import DNN, LocalActivationUnit, PredictionLayer, Regul"
},
{
"path": "deepctr/layers/activation.py",
"chars": 3111,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\n\nAuthor:\n Weichen Shen,weichenswc@163.com\n\n\"\"\"\n\nimport tensorflow as tf\n\ntry:\n from ten"
},
{
"path": "deepctr/layers/core.py",
"chars": 12758,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\n\nAuthor:\n Weichen Shen,weichenswc@163.com\n\n\"\"\"\n\nimport tensorflow as tf\nfrom tensorflow.py"
},
{
"path": "deepctr/layers/interaction.py",
"chars": 61769,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\n\nAuthors:\n Weichen Shen,weichenswc@163.com,\n Harshit Pande,\n Yi He, heyi_jack@163.co"
},
{
"path": "deepctr/layers/normalization.py",
"chars": 1729,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\n\nAuthor:\n Weichen Shen,weichenswc@163.com\n\n\"\"\"\n\nfrom tensorflow.python.keras import backen"
},
{
"path": "deepctr/layers/sequence.py",
"chars": 36162,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\n\nAuthor:\n Weichen Shen,weichenswc@163.com\n\n\"\"\"\n\nimport numpy as np\nimport tensorflow as tf"
},
{
"path": "deepctr/layers/utils.py",
"chars": 12531,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\n\nAuthor:\n Weichen Shen,weichenswc@163.com\n\n\"\"\"\nimport tensorflow as tf\nfrom tensorflow.pyt"
},
{
"path": "deepctr/models/__init__.py",
"chars": 884,
"preview": "from .afm import AFM\nfrom .autoint import AutoInt\nfrom .ccpm import CCPM\nfrom .dcn import DCN\nfrom .dcnmix import DCNMix"
},
{
"path": "deepctr/models/afm.py",
"chars": 3029,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\n\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Xiao J, Ye H, He X, et al. "
},
{
"path": "deepctr/models/autoint.py",
"chars": 4218,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\n\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Song W, Shi C, Xiao Z, et a"
},
{
"path": "deepctr/models/ccpm.py",
"chars": 3884,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\n\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Liu Q, Yu F, Wu S, et al. A"
},
{
"path": "deepctr/models/dcn.py",
"chars": 4203,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\n Shuxun Zan, zanshuxun@aliyun.com\n\nReference"
},
{
"path": "deepctr/models/dcnmix.py",
"chars": 4358,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\n Shuxun Zan, zanshuxun@aliyun.com\n\nReference"
},
{
"path": "deepctr/models/deepfefm.py",
"chars": 5727,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Harshit Pande\n\nReference:\n [1] Field-Embedded Factorization Machines for Click"
},
{
"path": "deepctr/models/deepfm.py",
"chars": 3257,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Guo H, Tang R, Ye Y, et al. "
},
{
"path": "deepctr/models/difm.py",
"chars": 4203,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n zanshuxun, zanshuxun@aliyun.com\nReference:\n [1] Lu W, Yu Y, Chang Y, et al. A "
},
{
"path": "deepctr/models/edcn.py",
"chars": 4316,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Yi He, heyi_jack@163.com\n\nReference:\n [1] Chen, B., Wang, Y., Liu, et al. Enha"
},
{
"path": "deepctr/models/fgcnn.py",
"chars": 4330,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\n\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Liu B, Tang R, Chen Y, et a"
},
{
"path": "deepctr/models/fibinet.py",
"chars": 3449,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Huang T, Zhang Z, Zhang J. F"
},
{
"path": "deepctr/models/flen.py",
"chars": 3280,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Tingyi Tan, 5636374@qq.com\n\nReference:\n [1] Chen W, Zhan L, Ci Y, Lin C. FLEN:"
},
{
"path": "deepctr/models/fnn.py",
"chars": 2717,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Zhang W, Du T, Wang J. Deep "
},
{
"path": "deepctr/models/fwfm.py",
"chars": 3616,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Harshit Pande\n\nReference:\n [1] Field-weighted Factorization Machines for Click"
},
{
"path": "deepctr/models/ifm.py",
"chars": 3686,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n zanshuxun, zanshuxun@aliyun.com\nReference:\n [1] Yu Y, Wang Z, Yuan B. An Input"
},
{
"path": "deepctr/models/mlr.py",
"chars": 3353,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Gai K, Zhu X, Li H, et al. L"
},
{
"path": "deepctr/models/multitask/__init__.py",
"chars": 105,
"preview": "from .esmm import ESMM\nfrom .mmoe import MMOE\nfrom .ple import PLE\nfrom .sharedbottom import SharedBottom"
},
{
"path": "deepctr/models/multitask/esmm.py",
"chars": 3300,
"preview": "\"\"\"\nAuthor:\n Mincai Lai, laimc@shanghaitech.edu.cn\n\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Ma X, Zh"
},
{
"path": "deepctr/models/multitask/mmoe.py",
"chars": 5081,
"preview": "\"\"\"\nAuthor:\n Mincai Lai, laimc@shanghaitech.edu.cn\n\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Ma J, Zh"
},
{
"path": "deepctr/models/multitask/ple.py",
"chars": 8325,
"preview": "\"\"\"\nAuthor:\n Mincai Lai, laimc@shanghaitech.edu.cn\n\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Tang H, "
},
{
"path": "deepctr/models/multitask/sharedbottom.py",
"chars": 3529,
"preview": "\"\"\"\nAuthor:\n Mincai Lai, laimc@shanghaitech.edu.cn\n\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Ruder S."
},
{
"path": "deepctr/models/nfm.py",
"chars": 3159,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] He X, Chua T S. Neural facto"
},
{
"path": "deepctr/models/onn.py",
"chars": 5815,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Yang Y, Xu B, Shen F, et al."
},
{
"path": "deepctr/models/pnn.py",
"chars": 3383,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Qu Y, Cai H, Ren K, et al. P"
},
{
"path": "deepctr/models/sequence/__init__.py",
"chars": 88,
"preview": "from .bst import BST\nfrom .dien import DIEN\nfrom .din import DIN\nfrom .dsin import DSIN\n"
},
{
"path": "deepctr/models/sequence/bst.py",
"chars": 6080,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Zichao Li, 2843656167@qq.com\n\nReference:\n Qiwei Chen, Huan Zhao, Wei Li, Pipei"
},
{
"path": "deepctr/models/sequence/dien.py",
"chars": 10903,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Zhou G, Mou N, Fan Y, et al."
},
{
"path": "deepctr/models/sequence/din.py",
"chars": 5236,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Zhou G, Zhu X, Song C, et al"
},
{
"path": "deepctr/models/sequence/dsin.py",
"chars": 8233,
"preview": "# coding: utf-8\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Feng Y, Lv F, Shen W, et al. Deep S"
},
{
"path": "deepctr/models/wdl.py",
"chars": 2720,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Cheng H T, Koc L, Harmsen J,"
},
{
"path": "deepctr/models/xdeepfm.py",
"chars": 3692,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\nAuthor:\n Weichen Shen, weichenswc@163.com\n\nReference:\n [1] Lian J, Zhou X, Zhang F, et "
},
{
"path": "deepctr/utils.py",
"chars": 1520,
"preview": "# -*- coding:utf-8 -*-\n\"\"\"\n\nAuthor:\n Weichen Shen,weichenswc@163.com\n\n\"\"\"\n\nimport json\nimport logging\nfrom threading "
},
{
"path": "docs/Makefile",
"chars": 608,
"preview": "# Minimal makefile for Sphinx documentation\n#\n\n# You can set these variables from the command line.\nSPHINXOPTS =\nSPHI"
},
{
"path": "docs/make.bat",
"chars": 779,
"preview": "@ECHO OFF\n\npushd %~dp0\n\nREM Command file for Sphinx documentation\n\nif \"%SPHINXBUILD%\" == \"\" (\n\tset SPHINXBUILD=sphinx-bu"
},
{
"path": "docs/requirements.readthedocs.txt",
"chars": 37,
"preview": "tensorflow==2.6.2\nrecommonmark==0.7.1"
},
{
"path": "docs/source/Estimators.rst",
"chars": 499,
"preview": "DeepCTR Estimators API\n======================\n\n.. toctree::\n CCPM<deepctr.estimator.models.ccpm>\n FNN<deepctr.estima"
},
{
"path": "docs/source/Examples.md",
"chars": 27347,
"preview": "# Examples\n\n## Classification: Criteo\n\nThe Criteo Display Ads dataset is for the purpose of predicting ads click-through"
},
{
"path": "docs/source/FAQ.md",
"chars": 5647,
"preview": "# FAQ\n\n## 1. Save or load weights/models\n----------------------------------------\nTo save/load weights,you can write cod"
},
{
"path": "docs/source/Features.md",
"chars": 22459,
"preview": "# Features\n\n## Overview\n\nWith the great success of deep learning,DNN-based techniques have been widely used in CTR predi"
},
{
"path": "docs/source/History.md",
"chars": 7175,
"preview": "# History\n- 11/10/2022 : [v0.9.3](https://github.com/shenweichen/DeepCTR/releases/tag/v0.9.3) released.Add [EDCN](./Feat"
},
{
"path": "docs/source/Layers.rst",
"chars": 323,
"preview": "DeepCTR Layers API\n======================\n\n\n.. toctree::\n :maxdepth: 3\n :caption: API:\n\n Core Layers<deepctr.layer"
},
{
"path": "docs/source/Model_Methods.md",
"chars": 23163,
"preview": "# Methods\n## compile\n```python\ncompile(optimizer, loss=None, metrics=None, loss_weights=None, sample_weight_mode=None, w"
},
{
"path": "docs/source/Models.rst",
"chars": 1011,
"preview": "DeepCTR Models API\n======================\n\n.. toctree::\n Model Methods<Model_Methods.md>\n CCPM<deepctr.models.ccpm>\n"
},
{
"path": "docs/source/Quick-Start.md",
"chars": 6614,
"preview": "# Quick-Start\n[](https://dsw-dev.data.aliyun.com/"
},
{
"path": "docs/source/conf.py",
"chars": 5046,
"preview": "# -*- coding: utf-8 -*-\n#\n# Configuration file for the Sphinx documentation builder.\n#\n# This file does only contain a s"
},
{
"path": "docs/source/deepctr.contrib.rnn.rst",
"chars": 148,
"preview": "deepctr.contrib.rnn module\n==========================\n\n.. automodule:: deepctr.contrib.rnn\n :members:\n :undoc-memb"
},
{
"path": "docs/source/deepctr.contrib.rst",
"chars": 257,
"preview": "deepctr.contrib package\n=======================\n\nSubmodules\n----------\n\n.. toctree::\n\n deepctr.contrib.rnn\n deepctr."
},
{
"path": "docs/source/deepctr.contrib.utils.rst",
"chars": 154,
"preview": "deepctr.contrib.utils module\n============================\n\n.. automodule:: deepctr.contrib.utils\n :members:\n :undo"
},
{
"path": "docs/source/deepctr.estimator.feature_column.rst",
"chars": 189,
"preview": "deepctr.estimator.feature\\_column module\n========================================\n\n.. automodule:: deepctr.estimator.fea"
},
{
"path": "docs/source/deepctr.estimator.inputs.rst",
"chars": 163,
"preview": "deepctr.estimator.inputs module\n===============================\n\n.. automodule:: deepctr.estimator.inputs\n :members:\n"
},
{
"path": "docs/source/deepctr.estimator.models.afm.rst",
"chars": 181,
"preview": "deepctr.estimator.models.afm module\n===================================\n\n.. automodule:: deepctr.estimator.models.afm\n "
},
{
"path": "docs/source/deepctr.estimator.models.autoint.rst",
"chars": 193,
"preview": "deepctr.estimator.models.autoint module\n=======================================\n\n.. automodule:: deepctr.estimator.model"
},
{
"path": "docs/source/deepctr.estimator.models.ccpm.rst",
"chars": 184,
"preview": "deepctr.estimator.models.ccpm module\n====================================\n\n.. automodule:: deepctr.estimator.models.ccpm"
},
{
"path": "docs/source/deepctr.estimator.models.dcn.rst",
"chars": 181,
"preview": "deepctr.estimator.models.dcn module\n===================================\n\n.. automodule:: deepctr.estimator.models.dcn\n "
},
{
"path": "docs/source/deepctr.estimator.models.deepfefm.rst",
"chars": 194,
"preview": "deepctr.estimator.models.deepfefm module\n======================================\n\n.. automodule:: deepctr.estimator.model"
},
{
"path": "docs/source/deepctr.estimator.models.deepfm.rst",
"chars": 190,
"preview": "deepctr.estimator.models.deepfm module\n======================================\n\n.. automodule:: deepctr.estimator.models."
},
{
"path": "docs/source/deepctr.estimator.models.fibinet.rst",
"chars": 193,
"preview": "deepctr.estimator.models.fibinet module\n=======================================\n\n.. automodule:: deepctr.estimator.model"
},
{
"path": "docs/source/deepctr.estimator.models.fnn.rst",
"chars": 181,
"preview": "deepctr.estimator.models.fnn module\n===================================\n\n.. automodule:: deepctr.estimator.models.fnn\n "
},
{
"path": "docs/source/deepctr.estimator.models.fwfm.rst",
"chars": 188,
"preview": "deepctr.estimator.models.fwfm module\n========================================\n\n.. automodule:: deepctr.estimator.models."
},
{
"path": "docs/source/deepctr.estimator.models.nfm.rst",
"chars": 181,
"preview": "deepctr.estimator.models.nfm module\n===================================\n\n.. automodule:: deepctr.estimator.models.nfm\n "
},
{
"path": "docs/source/deepctr.estimator.models.pnn.rst",
"chars": 181,
"preview": "deepctr.estimator.models.pnn module\n===================================\n\n.. automodule:: deepctr.estimator.models.pnn\n "
},
{
"path": "docs/source/deepctr.estimator.models.rst",
"chars": 641,
"preview": "deepctr.estimator.models package\n================================\n\nSubmodules\n----------\n\n.. toctree::\n\n deepctr.estim"
},
{
"path": "docs/source/deepctr.estimator.models.wdl.rst",
"chars": 181,
"preview": "deepctr.estimator.models.wdl module\n===================================\n\n.. automodule:: deepctr.estimator.models.wdl\n "
},
{
"path": "docs/source/deepctr.estimator.models.xdeepfm.rst",
"chars": 193,
"preview": "deepctr.estimator.models.xdeepfm module\n=======================================\n\n.. automodule:: deepctr.estimator.model"
},
{
"path": "docs/source/deepctr.estimator.rst",
"chars": 375,
"preview": "deepctr.estimator package\n=========================\n\nSubpackages\n-----------\n\n.. toctree::\n\n deepctr.estimator.models"
},
{
"path": "docs/source/deepctr.estimator.utils.rst",
"chars": 160,
"preview": "deepctr.estimator.utils module\n==============================\n\n.. automodule:: deepctr.estimator.utils\n :members:\n "
},
{
"path": "docs/source/deepctr.feature_column.rst",
"chars": 159,
"preview": "deepctr.feature\\_column module\n==============================\n\n.. automodule:: deepctr.feature_column\n :members:\n "
},
{
"path": "docs/source/deepctr.inputs.rst",
"chars": 133,
"preview": "deepctr.inputs module\n=====================\n\n.. automodule:: deepctr.inputs\n :members:\n :undoc-members:\n :show-"
},
{
"path": "docs/source/deepctr.layers.activation.rst",
"chars": 172,
"preview": "deepctr.layers.activation module\n================================\n\n.. automodule:: deepctr.layers.activation\n :member"
},
{
"path": "docs/source/deepctr.layers.core.rst",
"chars": 154,
"preview": "deepctr.layers.core module\n==========================\n\n.. automodule:: deepctr.layers.core\n :members:\n :no-undoc-m"
},
{
"path": "docs/source/deepctr.layers.interaction.rst",
"chars": 175,
"preview": "deepctr.layers.interaction module\n=================================\n\n.. automodule:: deepctr.layers.interaction\n :mem"
},
{
"path": "docs/source/deepctr.layers.normalization.rst",
"chars": 181,
"preview": "deepctr.layers.normalization module\n===================================\n\n.. automodule:: deepctr.layers.normalization\n "
},
{
"path": "docs/source/deepctr.layers.rst",
"chars": 371,
"preview": "deepctr.layers package\n======================\n\nSubmodules\n----------\n\n.. toctree::\n\n deepctr.layers.activation\n deep"
},
{
"path": "docs/source/deepctr.layers.sequence.rst",
"chars": 166,
"preview": "deepctr.layers.sequence module\n==============================\n\n.. automodule:: deepctr.layers.sequence\n :members:\n "
},
{
"path": "docs/source/deepctr.layers.utils.rst",
"chars": 151,
"preview": "deepctr.layers.utils module\n===========================\n\n.. automodule:: deepctr.layers.utils\n :members:\n :undoc-m"
},
{
"path": "docs/source/deepctr.models.afm.rst",
"chars": 151,
"preview": "deepctr.models.afm module\n=========================\n\n.. automodule:: deepctr.models.afm\n :members:\n :no-undoc-memb"
},
{
"path": "docs/source/deepctr.models.autoint.rst",
"chars": 163,
"preview": "deepctr.models.autoint module\n=============================\n\n.. automodule:: deepctr.models.autoint\n :members:\n :n"
},
{
"path": "docs/source/deepctr.models.ccpm.rst",
"chars": 154,
"preview": "deepctr.models.ccpm module\n==========================\n\n.. automodule:: deepctr.models.ccpm\n :members:\n :no-undoc-m"
},
{
"path": "docs/source/deepctr.models.dcn.rst",
"chars": 151,
"preview": "deepctr.models.dcn module\n=========================\n\n.. automodule:: deepctr.models.dcn\n :members:\n :no-undoc-memb"
},
{
"path": "docs/source/deepctr.models.dcnmix.rst",
"chars": 157,
"preview": "deepctr.models.dcnmix module\n=========================\n\n.. automodule:: deepctr.models.dcnmix\n :members:\n :no-undo"
},
{
"path": "docs/source/deepctr.models.deepfefm.rst",
"chars": 166,
"preview": "deepctr.models.deepfefm module\n==============================\n\n.. automodule:: deepctr.models.deepfefm\n :members:\n "
},
{
"path": "docs/source/deepctr.models.deepfm.rst",
"chars": 160,
"preview": "deepctr.models.deepfm module\n============================\n\n.. automodule:: deepctr.models.deepfm\n :members:\n :no-u"
},
{
"path": "docs/source/deepctr.models.deepfwfm.rst",
"chars": 166,
"preview": "deepctr.models.deepfwfm module\n==============================\n\n.. automodule:: deepctr.models.deepfwfm\n :members:\n "
},
{
"path": "docs/source/deepctr.models.difm.rst",
"chars": 157,
"preview": "deepctr.models.difm module\n=============================\n\n.. automodule:: deepctr.models.difm\n :members:\n :no-undo"
},
{
"path": "docs/source/deepctr.models.edcn.rst",
"chars": 153,
"preview": "deepctr.models.edcn module\n=========================\n\n.. automodule:: deepctr.models.edcn\n :members:\n :no-undoc-me"
},
{
"path": "docs/source/deepctr.models.fgcnn.rst",
"chars": 157,
"preview": "deepctr.models.fgcnn module\n===========================\n\n.. automodule:: deepctr.models.fgcnn\n :members:\n :no-undo"
},
{
"path": "docs/source/deepctr.models.fibinet.rst",
"chars": 163,
"preview": "deepctr.models.fibinet module\n=============================\n\n.. automodule:: deepctr.models.fibinet\n :members:\n :n"
},
{
"path": "docs/source/deepctr.models.flen.rst",
"chars": 157,
"preview": "deepctr.models.flen module\n=============================\n\n.. automodule:: deepctr.models.flen\n :members:\n :no-undo"
},
{
"path": "docs/source/deepctr.models.fnn.rst",
"chars": 151,
"preview": "deepctr.models.fnn module\n=========================\n\n.. automodule:: deepctr.models.fnn\n :members:\n :no-undoc-memb"
},
{
"path": "docs/source/deepctr.models.ifm.rst",
"chars": 155,
"preview": "deepctr.models.ifm module\n=============================\n\n.. automodule:: deepctr.models.ifm\n :members:\n :no-undoc-"
},
{
"path": "docs/source/deepctr.models.mlr.rst",
"chars": 151,
"preview": "deepctr.models.mlr module\n=========================\n\n.. automodule:: deepctr.models.mlr\n :members:\n :no-undoc-memb"
},
{
"path": "docs/source/deepctr.models.multitask.esmm.rst",
"chars": 177,
"preview": "deepctr.models.multitask.esmm module\n=============================\n\n.. automodule:: deepctr.models.multitask.esmm\n :m"
},
{
"path": "docs/source/deepctr.models.multitask.mmoe.rst",
"chars": 177,
"preview": "deepctr.models.multitask.mmoe module\n=============================\n\n.. automodule:: deepctr.models.multitask.mmoe\n :m"
},
{
"path": "docs/source/deepctr.models.multitask.ple.rst",
"chars": 175,
"preview": "deepctr.models.multitask.ple module\n=============================\n\n.. automodule:: deepctr.models.multitask.ple\n :mem"
},
{
"path": "docs/source/deepctr.models.multitask.sharedbottom.rst",
"chars": 193,
"preview": "deepctr.models.multitask.sharedbottom module\n=============================\n\n.. automodule:: deepctr.models.multitask.sha"
},
{
"path": "docs/source/deepctr.models.nfm.rst",
"chars": 151,
"preview": "deepctr.models.nfm module\n=========================\n\n.. automodule:: deepctr.models.nfm\n :members:\n :no-undoc-memb"
},
{
"path": "docs/source/deepctr.models.onn.rst",
"chars": 152,
"preview": "deepctr.models.onn module\n==========================\n\n.. automodule:: deepctr.models.onn\n :members:\n :no-undoc-mem"
},
{
"path": "docs/source/deepctr.models.pnn.rst",
"chars": 151,
"preview": "deepctr.models.pnn module\n=========================\n\n.. automodule:: deepctr.models.pnn\n :members:\n :no-undoc-memb"
},
{
"path": "docs/source/deepctr.models.rst",
"chars": 883,
"preview": "deepctr.models package\n======================\n\nSubmodules\n----------\n\n.. toctree::\n\n deepctr.models.afm\n deepctr.mod"
},
{
"path": "docs/source/deepctr.models.sequence.bst.rst",
"chars": 169,
"preview": "deepctr.models.sequence.bst module\n=========================\n\n.. automodule:: deepctr.models.sequence.bst\n :members:\n"
},
{
"path": "docs/source/deepctr.models.sequence.dien.rst",
"chars": 172,
"preview": "deepctr.models.sequence.dien module\n==========================\n\n.. automodule:: deepctr.models.sequence.dien\n :member"
},
{
"path": "docs/source/deepctr.models.sequence.din.rst",
"chars": 169,
"preview": "deepctr.models.sequence.din module\n=========================\n\n.. automodule:: deepctr.models.sequence.din\n :members:\n"
},
{
"path": "docs/source/deepctr.models.sequence.dsin.rst",
"chars": 172,
"preview": "deepctr.models.sequence.dsin module\n==========================\n\n.. automodule:: deepctr.models.sequence.dsin\n :member"
},
{
"path": "docs/source/deepctr.models.wdl.rst",
"chars": 151,
"preview": "deepctr.models.wdl module\n=========================\n\n.. automodule:: deepctr.models.wdl\n :members:\n :no-undoc-memb"
},
{
"path": "docs/source/deepctr.models.xdeepfm.rst",
"chars": 163,
"preview": "deepctr.models.xdeepfm module\n=============================\n\n.. automodule:: deepctr.models.xdeepfm\n :members:\n :n"
},
{
"path": "docs/source/deepctr.rst",
"chars": 318,
"preview": "deepctr package\n===============\n\nSubpackages\n-----------\n\n.. toctree::\n\n deepctr.contrib\n deepctr.layers\n deepc"
},
{
"path": "docs/source/deepctr.utils.rst",
"chars": 130,
"preview": "deepctr.utils module\n====================\n\n.. automodule:: deepctr.utils\n :members:\n :undoc-members:\n :show-inh"
},
{
"path": "docs/source/index.rst",
"chars": 3225,
"preview": ".. DeepCTR documentation master file, created by\n sphinx-quickstart on Fri Nov 23 21:08:54 2018.\n You can adapt this"
},
{
"path": "docs/source/modules.rst",
"chars": 58,
"preview": "deepctr\n=======\n\n.. toctree::\n :maxdepth: 4\n\n deepctr\n"
},
{
"path": "examples/avazu_sample.txt",
"chars": 15694,
"preview": "id,click,hour,C1,banner_pos,site_id,site_domain,site_category,app_id,app_domain,app_category,device_id,device_ip,device_"
},
{
"path": "examples/census-income.sample",
"chars": 102656,
"preview": "138481,62, Private,43,23, High school graduate,0, Not in universe, Married-civilian spouse present, Education, Adm suppo"
},
{
"path": "examples/criteo_sample.txt",
"chars": 52518,
"preview": "label,I1,I2,I3,I4,I5,I6,I7,I8,I9,I10,I11,I12,I13,C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13,C14,C15,C16,C17,C18,C19,C20,"
},
{
"path": "examples/gen_tfrecords.py",
"chars": 1079,
"preview": "import tensorflow as tf\n\ndef make_example(line, sparse_feature_name, dense_feature_name, label_name):\n features = {fe"
},
{
"path": "examples/movielens_age_vocabulary.csv",
"chars": 34,
"preview": "1,1\n2,18\n3,25\n4,35\n5,45\n6,50\n7,56\n"
},
{
"path": "examples/movielens_sample.txt",
"chars": 15155,
"preview": "user_id,movie_id,rating,timestamp,title,genres,gender,age,occupation,zip\n3299,235,4,968035345,Ed Wood (1994),Comedy|Dram"
},
{
"path": "examples/run_all.sh",
"chars": 772,
"preview": "#!/usr/bin/env bash\n\nfunction run_py(){\n\n code_path=./\n for file in $(ls)\n do\n if [[ $file =~ .py ]]\n "
},
{
"path": "examples/run_classification_criteo.py",
"chars": 2389,
"preview": "import pandas as pd\nfrom sklearn.metrics import log_loss, roc_auc_score\nfrom sklearn.model_selection import train_test_s"
},
{
"path": "examples/run_classification_criteo_hash.py",
"chars": 2196,
"preview": "import pandas as pd\nfrom sklearn.metrics import log_loss, roc_auc_score\nfrom sklearn.model_selection import train_test_s"
},
{
"path": "examples/run_classification_criteo_multi_gpu.py",
"chars": 2463,
"preview": "import pandas as pd\nfrom sklearn.metrics import log_loss, roc_auc_score\nfrom sklearn.model_selection import train_test_s"
},
{
"path": "examples/run_dien.py",
"chars": 2985,
"preview": "import numpy as np\nimport tensorflow as tf\n\nfrom deepctr.feature_column import SparseFeat, VarLenSparseFeat, DenseFeat,g"
},
{
"path": "examples/run_din.py",
"chars": 2151,
"preview": "import numpy as np\n\nfrom deepctr.models import DIN\nfrom deepctr.feature_column import SparseFeat, VarLenSparseFeat, Dens"
},
{
"path": "examples/run_dsin.py",
"chars": 2802,
"preview": "import numpy as np\nimport tensorflow as tf\n\nfrom deepctr.feature_column import SparseFeat, VarLenSparseFeat, DenseFeat,g"
},
{
"path": "examples/run_estimator_pandas_classification.py",
"chars": 2560,
"preview": "import pandas as pd\nimport tensorflow as tf\nfrom sklearn.metrics import log_loss, roc_auc_score\nfrom sklearn.model_selec"
},
{
"path": "examples/run_estimator_tfrecord_classification.py",
"chars": 1992,
"preview": "import tensorflow as tf\n\nfrom tensorflow.python.ops.parsing_ops import FixedLenFeature\nfrom deepctr.estimator import Dee"
},
{
"path": "examples/run_flen.py",
"chars": 3024,
"preview": "import pandas as pd\nfrom sklearn.metrics import log_loss, roc_auc_score\nfrom sklearn.model_selection import train_test_s"
},
{
"path": "examples/run_mtl.py",
"chars": 4002,
"preview": "import pandas as pd\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.model_selection import train_test_split\nfrom "
},
{
"path": "examples/run_multivalue_movielens.py",
"chars": 3039,
"preview": "import numpy as np\nimport pandas as pd\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.python.keras.prepr"
},
{
"path": "examples/run_multivalue_movielens_hash.py",
"chars": 2173,
"preview": "import numpy as np\nimport pandas as pd\nfrom tensorflow.python.keras.preprocessing.sequence import pad_sequences\n\nfrom de"
},
{
"path": "examples/run_multivalue_movielens_vocab_hash.py",
"chars": 2790,
"preview": "from deepctr.models import DeepFM\nfrom deepctr.feature_column import SparseFeat, VarLenSparseFeat, get_feature_names\nimp"
},
{
"path": "examples/run_regression_movielens.py",
"chars": 1817,
"preview": "import pandas as pd\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.model_selection import train_test_split\n"
},
{
"path": "setup.cfg",
"chars": 703,
"preview": "[metadata]\ndesciption-file = README.md\n\n#[coverage:run]\n#branch = True\n\n[coverage:report]\nexclude_lines =\n # Have to "
},
{
"path": "setup.py",
"chars": 2130,
"preview": "import sys\n\nimport setuptools\n\nwith open(\"README.md\", \"r\") as fh:\n long_description = fh.read()\n\nREQUIRED_PACKAGES = "
},
{
"path": "tests/README.md",
"chars": 0,
"preview": ""
},
{
"path": "tests/__init__.py",
"chars": 0,
"preview": ""
},
{
"path": "tests/feature_test.py",
"chars": 1249,
"preview": "from deepctr.models import DeepFM\nfrom deepctr.feature_column import SparseFeat, DenseFeat, VarLenSparseFeat, get_featur"
},
{
"path": "tests/layers/__init__.py",
"chars": 0,
"preview": ""
},
{
"path": "tests/layers/activations_test.py",
"chars": 401,
"preview": "from deepctr.layers import activation\n\ntry:\n from tensorflow.python.keras.utils.generic_utils import CustomObjectScop"
},
{
"path": "tests/layers/core_test.py",
"chars": 2360,
"preview": "import pytest\nimport tensorflow as tf\nfrom tensorflow.python.keras.layers import PReLU\n\ntry:\n from tensorflow.python."
},
{
"path": "tests/layers/interaction_test.py",
"chars": 4753,
"preview": "import pytest\n\ntry:\n from tensorflow.python.keras.utils.generic_utils import CustomObjectScope\nexcept ImportError:\n "
},
{
"path": "tests/layers/normalization_test.py",
"chars": 638,
"preview": "import pytest\n\ntry:\n from tensorflow.python.keras.utils.generic_utils import CustomObjectScope\nexcept ImportError:\n "
},
{
"path": "tests/layers/sequence_test.py",
"chars": 4281,
"preview": "import pytest\nfrom packaging import version\n\ntry:\n from tensorflow.python.keras.utils.generic_utils import CustomObje"
},
{
"path": "tests/layers/utils_test.py",
"chars": 1468,
"preview": "import numpy as np\nimport pytest\nimport tensorflow as tf\n\nfrom deepctr.layers.utils import Hash, Linear\nfrom tests.layer"
},
{
"path": "tests/layers/vocabulary_example.csv",
"chars": 25,
"preview": "1,lake\n2,merson\n3,johnson"
},
{
"path": "tests/models/AFM_test.py",
"chars": 1561,
"preview": "import pytest\n\nfrom deepctr.models import AFM\nfrom ..utils import check_model, check_estimator, get_test_data, get_test_"
},
{
"path": "tests/models/AutoInt_test.py",
"chars": 1959,
"preview": "import pytest\nimport tensorflow as tf\nfrom packaging import version\n\nfrom deepctr.models import AutoInt\nfrom ..utils imp"
},
{
"path": "tests/models/BST_test.py",
"chars": 477,
"preview": "from deepctr.models import BST\nfrom ..utils import check_model\nfrom .DIN_test import get_xy_fd\n\n\ndef test_BST():\n mod"
},
{
"path": "tests/models/CCPM_test.py",
"chars": 2338,
"preview": "import pytest\nimport tensorflow as tf\n\nfrom deepctr.models import CCPM\nfrom ..utils import check_model, get_test_data, S"
},
{
"path": "tests/models/DCNMix_test.py",
"chars": 731,
"preview": "import pytest\n\nfrom deepctr.models import DCNMix\nfrom ..utils import check_model, get_test_data, SAMPLE_SIZE\n\n\n@pytest.m"
},
{
"path": "tests/models/DCN_test.py",
"chars": 2641,
"preview": "import pytest\n\nfrom deepctr.models import DCN\nfrom ..utils import check_model, get_test_data, SAMPLE_SIZE, get_test_data"
},
{
"path": "tests/models/DIEN_test.py",
"chars": 3664,
"preview": "import numpy as np\nimport pytest\nimport tensorflow as tf\nfrom packaging import version\n\nfrom deepctr.feature_column impo"
},
{
"path": "tests/models/DIFM_test.py",
"chars": 721,
"preview": "import pytest\n\nfrom deepctr.models import DIFM\nfrom ..utils import check_model, get_test_data, SAMPLE_SIZE\n\n\n@pytest.mar"
},
{
"path": "tests/models/DIN_test.py",
"chars": 2534,
"preview": "import numpy as np\nimport tensorflow as tf\nfrom packaging import version\n\nfrom deepctr.feature_column import SparseFeat,"
},
{
"path": "tests/models/DSIN_test.py",
"chars": 2744,
"preview": "import numpy as np\nimport pytest\n\nfrom deepctr.feature_column import SparseFeat, VarLenSparseFeat, DenseFeat, get_featur"
},
{
"path": "tests/models/DeepFEFM_test.py",
"chars": 2223,
"preview": "import pytest\nimport tensorflow as tf\n\nfrom deepctr.models import DeepFEFM\nfrom ..utils import check_model, get_test_dat"
},
{
"path": "tests/models/DeepFM_test.py",
"chars": 1748,
"preview": "import pytest\n\nfrom deepctr.models import DeepFM\nfrom ..utils import check_model, get_test_data, SAMPLE_SIZE, get_test_d"
}
]
// ... and 20 more files (download for full content)
About this extraction
This page contains the full source code of the shenweichen/DeepCTR GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 220 files (867.4 KB), approximately 253.3k tokens, and a symbol index with 463 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.