Skip to content

Commit

Permalink
Remove tensorflow from h1st dependencies (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiro-v authored Feb 24, 2023
1 parent b8f00eb commit e60f33e
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 71 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ jobs:
- name: Install dependencies
run: |
poetry install --no-interaction
poetry run python -m pip install tensorflow
- name: Run tests
run: poetry run pytest
Expand Down
2 changes: 1 addition & 1 deletion h1st/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def persist(self, version=None) -> str:
Persist this model's properties to the ModelRepository. Currently, only `stats`, `metrics`, `model` properties are supported.
`model` property could be single model, list or dict of models
Currently, only sklearn and tensorflow-keras are supported.
Currently, only sklearn are supported, but you can extend this method to support any framework.
:param version: model version, leave blank for autogeneration
:returns: model version
Expand Down
10 changes: 1 addition & 9 deletions h1st/model/repository/model_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import ulid
import joblib
import skfuzzy
# import tensorflow
import sklearn

from h1st.model.repository.storage.s3 import S3Storage
Expand Down Expand Up @@ -54,8 +53,7 @@ def _get_rule_engine_type(self, rules):
def _get_model_type(self, model):
if isinstance(model, sklearn.base.BaseEstimator):
return "sklearn"
# if isinstance(model, tensorflow.keras.Model):
# return "tensorflow-keras"

if model is None:
return "custom"

Expand Down Expand Up @@ -87,10 +85,6 @@ def _serialize_single_model(self, model, path, model_name="model"):
if model_type == "sklearn":
model_path = "%s.joblib" % model_name
joblib.dump(model, path + "/%s" % model_path)
# elif model_type == "tensorflow-keras":
# model_path = model_name
# os.makedirs(path + "/%s" % model_path, exist_ok=True)
# model.save_weights(path + "/%s/weights" % model_path)
elif model_type == "custom":
model_path = model_name # XXX
else:
Expand All @@ -103,8 +97,6 @@ def _deserialize_single_model(self, model, path, model_type, model_path):
# This is a sklearn model
model = joblib.load(path + "/%s" % model_path)
# print(str(type(model)))
# elif model_type == "tensorflow-keras":
# model.load_weights(path + "/%s/weights" % model_path).expect_partial()
elif model_type == "custom":
model = None

Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pyarrow = ">=9.0.0"

# Machine Learning / Deep Learning
scikit-learn = ">=1.1.2"
xgboost = ">=1.7.2"

# Trustworthy AI
graphviz = ">=0.20.1"
Expand Down
59 changes: 0 additions & 59 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import tempfile

import tensorflow as tf
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
Expand Down Expand Up @@ -59,9 +58,6 @@ def assert_models(self, modeler_class, model_class, model_type, model_path, coll
if model_type == 'sklearn':
import joblib
assert model_type == model_serde._get_model_type(joblib.load('%s/%s' % (path, model_path)))
elif model_type == 'tensorflow-keras':
# Currently, we save/load weights => nothing do assert here
pass

model_serde.deserialize(model_2, path)

Expand Down Expand Up @@ -140,61 +136,6 @@ class MyModel(MLModel):

self.assert_models(MyModeler, MyModel, 'sklearn', 'model_Iris.joblib', 'dict')

def test_serialize_tensorflow_model(self):
class MyModeler(MLModeler):
def load_data(self) -> dict:
data = load_iris()
return {'X': data.data, 'y': data.target}

def train_base_model(self, prepared_data):
X, y = prepared_data['X'], prepared_data['y']
model = self.model_class.get_model_arch()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, verbose=2, batch_size=5, epochs=20)
return model

class MyModel(MLModel):
def __init__(self):
self.base_model = self.get_model_arch()

@staticmethod
def get_model_arch():
model = tf.keras.Sequential([
tf.keras.layers.Dense(8, input_dim=4, activation='relu'),
tf.keras.layers.Dense(1, activation='softmax')
])
return model

self.assert_models(MyModeler, MyModel, 'tensorflow-keras', 'model')

def test_serialize_dict_tensorflow_model(self):
class MyModeler(MLModeler):
def load_data(self) -> dict:
data = load_iris()
return {'X': data.data, 'y': data.target}

def train_base_model(self, prepared_data):
X, y = prepared_data['X'], prepared_data['y']
model = self.model_class.get_model_arch()['Iris']
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, verbose=2, batch_size=5, epochs=20)
return {'Iris': model}

class MyModel(MLModel):
def __init__(self):
self.base_model = self.get_model_arch()

@staticmethod
def get_model_arch():
model = tf.keras.Sequential([
tf.keras.layers.Dense(8, input_dim=4, activation='relu'),
tf.keras.layers.Dense(1, activation='softmax')
])
return {'Iris': model}

self.assert_models(MyModeler, MyModel, 'tensorflow-keras', 'model_Iris', 'dict')


class TestModelStatsSerDe:
def test_serialize_dict(self):
class MyModeler(MLModeler):
Expand Down

0 comments on commit e60f33e

Please sign in to comment.