Skip to content
This repository has been archived by the owner on Mar 3, 2024. It is now read-only.

Commit

Permalink
Compatible with Keras 2.4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberZHG committed Jul 11, 2020
1 parent cca6ddf commit 0c9c0ce
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 44 deletions.
34 changes: 22 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
dist: xenial
language: python
python:
- "2.7"
- "3.6"
python: "3.6"
env:
global:
- COVERALLS_PARALLEL=true
matrix:
- KERAS_BACKEND=tensorflow
- KERAS_BACKEND=tensorflow TF_KERAS=1
install:
- wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh;
- bash miniconda.sh -b -p $HOME/miniconda
- export PATH="$HOME/miniconda/bin:$PATH"
- conda config --set always_yes yes --set changeps1 no
- conda update -q conda
- conda info -a
- conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION
- source activate test-environment
- export LD_LIBRARY_PATH=$HOME/miniconda/envs/test-environment/lib/:$LD_LIBRARY_PATH
- pip install --upgrade pip
- pip install numpy
- pip install tensorflow
- pip install keras
- pip install pycodestyle
- pip install coverage
- pip install -r requirements.txt
- pip install -r requirements-dev.txt
- pip install coveralls
- pip install keras-word-char-embd
before_script:
- bash lint.sh
script:
- bash test.sh
- ./test.sh
after_success:
coveralls
notifications:
webhooks: https://coveralls.io/webhook
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 PoW
Copyright (c) 2020 Zhao HG

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 2 additions & 0 deletions keras_wc_embd/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from .word_char_embd import *
from .wrapper import WordCharEmbd

__version__ = '0.21.0'
33 changes: 33 additions & 0 deletions keras_wc_embd/backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
from distutils.util import strtobool

__all__ = [
'keras', 'utils', 'activations', 'applications', 'backend', 'datasets',
'layers', 'preprocessing', 'wrappers', 'callbacks', 'constraints', 'initializers',
'metrics', 'models', 'losses', 'optimizers', 'regularizers', 'TF_KERAS',
]

TF_KERAS = strtobool(os.environ.get('TF_KERAS', '0'))

if TF_KERAS:
import tensorflow as tf
keras = tf.keras
else:
import keras

utils = keras.utils
activations = keras.activations
applications = keras.applications
backend = keras.backend
datasets = keras.datasets
layers = keras.layers
preprocessing = keras.preprocessing
wrappers = keras.wrappers
callbacks = keras.callbacks
constraints = keras.constraints
initializers = keras.initializers
metrics = keras.metrics
models = keras.models
losses = keras.losses
optimizers = keras.optimizers
regularizers = keras.regularizers
4 changes: 2 additions & 2 deletions keras_wc_embd/word_char_embd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import codecs
import numpy
import keras
import keras.backend as K
from .backend import keras
from .backend import backend as K

__all__ = [
'MaskedConv1D', 'MaskedFlatten',
Expand Down
2 changes: 0 additions & 2 deletions lint.sh

This file was deleted.

33 changes: 23 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
import os
import re
import codecs
from setuptools import setup, find_packages

current_path = os.path.abspath(os.path.dirname(__file__))

with codecs.open('README.md', 'r', 'utf8') as reader:
long_description = reader.read()

def read_file(*parts):
with codecs.open(os.path.join(current_path, *parts), 'r', 'utf8') as reader:
return reader.read()

with codecs.open('requirements.txt', 'r', 'utf8') as reader:
install_requires = list(map(lambda x: x.strip(), reader.readlines()))

def get_requirements(*parts):
with codecs.open(os.path.join(current_path, *parts), 'r', 'utf8') as reader:
return list(map(lambda x: x.strip(), reader.readlines()))


def find_version(*file_paths):
version_file = read_file(*file_paths)
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M)
if version_match:
return version_match.group(1)
raise RuntimeError('Unable to find version string.')


setup(
name='keras-word-char-embd',
version='0.20.0',
version=find_version('keras_wc_embd', '__init__.py'),
packages=find_packages(),
url='https://github.com/CyberZHG/keras-word-char-embd',
license='MIT',
author='CyberZHG',
author_email='CyberZHG@gmail.com',
author_email='CyberZHG@users.noreply.github.com',
description='Concatenate word and character embeddings in Keras',
long_description=long_description,
long_description=read_file('README.md'),
long_description_content_type='text/markdown',
install_requires=install_requires,
install_requires=get_requirements('requirements.txt'),
classifiers=(
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
),
Expand Down
3 changes: 2 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#!/usr/bin/env bash
nosetests --with-coverage --cover-erase --cover-html --cover-html-dir=htmlcov --cover-package="keras_wc_embd" tests
pycodestyle --max-line-length=120 keras_wc_embd tests && \
nosetests --nocapture --with-coverage --cover-erase --cover-html --cover-html-dir=htmlcov --cover-package=keras_wc_embd --with-doctest
30 changes: 15 additions & 15 deletions tests/test_get_embedding_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def test_shape_rnn(self):
char_hidden_dim=75,
)
self.assertEqual(len(inputs), 2)
self.assertEqual(inputs[0]._keras_shape, (None, None))
self.assertEqual(inputs[1]._keras_shape, (None, None, 5))
self.assertEqual(embd_layer._keras_shape, (None, None, 300))
self.assertEqual(inputs[0].shape.as_list(), [None, None])
self.assertEqual(inputs[1].shape.as_list(), [None, None, 5])
self.assertEqual(embd_layer.shape.as_list(), [None, None, 300])
inputs, embd_layer = get_embedding_layer(
word_dict_len=3,
char_dict_len=5,
Expand All @@ -29,9 +29,9 @@ def test_shape_rnn(self):
char_hidden_layer_type='gru',
)
self.assertEqual(len(inputs), 2)
self.assertEqual(inputs[0]._keras_shape, (None, None))
self.assertEqual(inputs[1]._keras_shape, (None, None, 7))
self.assertEqual(embd_layer._keras_shape, (None, None, 600))
self.assertEqual(inputs[0].shape.as_list(), [None, None])
self.assertEqual(inputs[1].shape.as_list(), [None, None, 7])
self.assertEqual(embd_layer.shape.as_list(), [None, None, 600])

def test_pre_trained_shape(self):
get_embedding_layer(
Expand Down Expand Up @@ -85,9 +85,9 @@ def test_shape_cnn(self):
char_mask_zero=True,
)
self.assertEqual(len(inputs), 2)
self.assertEqual(inputs[0]._keras_shape, (None, None))
self.assertEqual(inputs[1]._keras_shape, (None, None, 7))
self.assertEqual(embd_layer._keras_shape, (None, None, 450))
self.assertEqual(inputs[0].shape.as_list(), [None, None])
self.assertEqual(inputs[1].shape.as_list(), [None, None, 7])
self.assertEqual(embd_layer.shape.as_list(), [None, None, 450])

def test_custom(self):
layers = [
Expand Down Expand Up @@ -117,9 +117,9 @@ def test_custom(self):
char_mask_zero=False,
)
self.assertEqual(len(inputs), 2)
self.assertEqual(inputs[0]._keras_shape, (None, None))
self.assertEqual(inputs[1]._keras_shape, (None, None, 7))
self.assertEqual(embd_layer._keras_shape, (None, None, 350))
self.assertEqual(inputs[0].shape.as_list(), [None, None])
self.assertEqual(inputs[1].shape.as_list(), [None, None, 7])
self.assertEqual(embd_layer.shape.as_list(), [None, None, 350])
inputs, embd_layer = get_embedding_layer(
word_dict_len=3,
char_dict_len=5,
Expand All @@ -130,9 +130,9 @@ def test_custom(self):
char_mask_zero=False,
)
self.assertEqual(len(inputs), 2)
self.assertEqual(inputs[0]._keras_shape, (None, None))
self.assertEqual(inputs[1]._keras_shape, (None, None, 7))
self.assertEqual(embd_layer._keras_shape, (None, None, 330))
self.assertEqual(inputs[0].shape.as_list(), [None, None])
self.assertEqual(inputs[1].shape.as_list(), [None, None, 7])
self.assertEqual(embd_layer.shape.as_list(), [None, None, 330])

def test_not_implemented(self):
with self.assertRaises(NotImplementedError):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import os
import keras
import numpy as np
from keras_wc_embd.backend import keras
from keras_wc_embd import WordCharEmbd


Expand Down

0 comments on commit 0c9c0ce

Please sign in to comment.