Comments (7)
Hi @faradawn
PiML only supports sklearn style models.
You can wrap your tensorflow models by following codes.
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
model = KerasClassifier(build_fn = lambda : dnn_model, verbose=False)
model.fit(train_x, train_y)
model._estimator_type = "classifier"
pipeline_1 = exp.make_pipeline(model=model, train_x=train_x, train_y=train_y.ravel(),
test_x=test_x, test_y=test_y.ravel(),
feature_names=feature_names, target_name="latency")
exp.register(pipeline_1, "my-dnn")
After that, you will be able to do model_explain and model_diagnose. However, this model cannot be interpreted by exp.model_interpret, as it is not a built-in model of PiML.
from piml-toolbox.
Hi Zebin,
Thank you for suggesting a scikit_learn wrapper!
normalizer = layers.Normalization()
# normalizer.adapt(np.array(train_x)) # this causes model_to_estimator to fail
dnn_model = keras.Sequential([
normalizer,
layers.Dense(512, activation='relu', input_dim=train_x.shape[1]),
layers.Dense(512, activation='relu'),
layers.Dense(1, activation='sigmoid')
])
dnn_model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.Adam(learning_rate=0.0001), metrics=['accuracy'])
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
model = KerasClassifier(build_fn = lambda : dnn_model, verbose=False)
model.fit(train_x, train_y)
model._estimator_type = "classifier"
from piml import Experiment
exp = Experiment()
pipeline_1 = exp.make_pipeline(model=model, train_x=train_x, train_y=train_y.ravel(),
test_x=test_x, test_y=test_y.ravel(),
feature_names=feature_names, target_name="latency")
exp.register(pipeline_1, "my-dnn")
I tried but received a normalization layer problem:
ValueError Traceback (most recent call last)
[<ipython-input-8-99a663406776>](https://localhost:8080/#) in <cell line: 23>()
21 test_x=test_x, test_y=test_y.ravel(),
22 feature_names=feature_names, target_name="latency")
---> 23 exp.register(pipeline_1, "my-dnn")
24
25
4 frames
[/usr/local/lib/python3.9/dist-packages/keras/engine/training.py](https://localhost:8080/#) in tf__predict_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 2169, in predict_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 2155, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 2143, in run_step **
outputs = model.predict_step(data)
File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 2111, in predict_step
return self(x, training=False)
File "/usr/local/lib/python3.9/dist-packages/keras/utils/traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
ValueError: Exception encountered when calling layer 'normalization_4' (type Normalization).
Dimensions must be equal, but are 8 and 9 for '{{node sequential_3/normalization_4/sub}} = Sub[T=DT_FLOAT](IteratorGetNext, sequential_3/normalization_4/sub/y)' with input shapes: [32,8], [1,9].
Call arguments received by layer 'normalization_4' (type Normalization):
• inputs=tf.Tensor(shape=(32, 8), dtype=float32)
I can try to debug more. But the method you suggested was making progress!
Thank you very much, and will let you know if the issue is solved!
from piml-toolbox.
Hi,
The tensorflow.keras.wrappers.scikit_learn
wrapper allowed me to register a TensorFlow model! After removing the normalization layer, the problem above was resolved.
dnn_model = keras.Sequential([
layers.Dense(512, activation='relu', input_dim=train_x.shape[1]),
layers.Dense(512, activation='relu'),
layers.Dense(1, activation='sigmoid')
])
dnn_model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.Adam(learning_rate=0.0001), metrics=['accuracy'])
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
model = KerasClassifier(build_fn = lambda : dnn_model, verbose=False)
model.fit(train_x, train_y)
model._estimator_type = "classifier"
from piml import Experiment
exp = Experiment()
pipeline_1 = exp.make_pipeline(model=model, train_x=train_x, train_y=train_y.ravel(),
test_x=test_x, test_y=test_y.ravel(),
feature_names=feature_names, target_name="latency")
exp.register(pipeline_1, "my-dnn")
And the output is
model = KerasClassifier(build_fn = lambda : dnn_model, verbose=False)
156/156 [==============================] - 0s 2ms/step
156/156 [==============================] - 0s 3ms/step
Register my-dnn Done
May I ask two more questions:
- Why global-explainability is not showing?
- Is there a way to silence the training output during
exp.model_explain()
?
I read that we can set model.fit(verbose = 0)
during training. But how to specify this parameter when PIML calls this model?
Thanks!
from piml-toolbox.
Hi @faradawn,
It seems that the verbose parameter in KerasClassifier is not passed to its predict function.
To turn off the messages, a workaround is to override the predict functions manually, see below.
def wrap_predict(x):
proba = model.model.predict(x, verbose=False)
if proba.shape[-1] > 1:
classes = proba.argmax(axis=-1)
else:
classes = (proba > 0.5).astype("int32")
return model.classes_[classes]
def wrap_predict_proba(x):
probs = model.model.predict(x, verbose=False)
# check if binary classification
if probs.shape[1] == 1:
# first column is probability of class 0 and second is of class 1
probs = np.hstack([1 - probs, probs])
return probs
model.predict = wrap_predict
model.predict_proba = wrap_predict_proba
The explainability output can be empty if the model output is constant. I tried on my side the built-in 'CoCircles' dataset and the output looks good. Can you double-check the data and model you used? Or just put a reproducible Colab link here.
from piml import Experiment
exp = Experiment()
exp.data_loader("CoCircles", silent=True)
exp.data_summary(silent=True)
exp.data_prepare(silent=True)
feature_names = exp.get_feature_names()
train_x, train_y, train_sample_weight = exp.get_data(train=True)
test_x, test_y, test_sample_weight = exp.get_data(test=True)
from piml-toolbox.
Hi Zebin,
The wrap_predict worked perfectly in that it suppressed the training output!
Regarding the empty permutation feature importance, I created a Colab Notebook that produces the empty graph.
I re-checked the dataset and the model which uses 8 features to make a 0/1 binary prediction.
You already helped so much. Truly appreciate any guidance!
Thanks!
from piml-toolbox.
Hi @faradawn
It seems your response variable only has one class
Also, for neural network models, we usually standardize x before model training.
from piml-toolbox.
Hi Zebin,
Got that it was due to y having a single class! Thanks for finding that out!
The issue is resolved. Appreciate your numerous help, such as teaching how to wrap a tensor flow model into skilearn!
Can close issue anytime!
from piml-toolbox.
Related Issues (20)
- Output coefficients for glm HOT 1
- Error with training classification models HOT 2
- Interpretation of L1 and L2 values in GLM options HOT 2
- GAMI-Net for Multiple Response Regression HOT 1
- Replicating sklearn elastic net results in PiML HOT 4
- GLM with cross validation HOT 1
- Elastic net removes all predictors HOT 3
- ERROR: No matching distribution found for PiML HOT 9
- Colab cannot import piml: initialization of _internal failed HOT 3
- VSCode cannot display some graphs HOT 2
- Example codes do not run on Colab HOT 2
- GAMI-Tree implementation
- Where are the source codes HOT 1
- exp.data_prepare() not working HOT 1
- Link to OpenTeams Videos somewhere here HOT 3
- Consider model ranking HOT 1
- High-code grid search examples? HOT 2
- Support residual by level or bin of feature? HOT 1
- Support Spearman correlation? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from piml-toolbox.