How to find the sklearn version of pickled model? - python

I have a pickled sklearn model, which I need to get to run. This model, however, is trained in unknown version of sklearn.
When I look up the model in debugger, I find that there is a bunch of strange tracebacks inside, instead of the keys you'd expect, for example:
decision_function -> 'RandomForestClassifier' object has no attribute 'decision_function'
fit_predict -> 'RandomForestClassifier' object has no attribute 'fit_predict'
score_samples -> 'RandomForestClassifier' object has no attribute 'score_samples'
How can I get this model to run? Does these error message hint you anything?
EDIT: The solution is to brute force search the sklearn version. In my case when I got to the correct major version, the error message pointed me to the correct minor version.

Just like #rickhg12hs suggested, the python -m pickletools your_pickled_model_file does the job!
The output is quite long, so I recommend using head:
python -m pickletools your_pickled_model_file | head -100

You can know the version of a pickled model after scikit-learn 0.18. Using
model.__getstate__()['_sklearn_version']
So at least you will know if it is pre 0.18 or newer.

Related

Getting 'AttributeError: Can't get attribute 'DeprecationDict' on <module 'sklearn.utils.deprecation'' this error while executing ML project

AttributeError: Can't get attribute 'DeprecationDict' on
Showing error in model = pickle.load(open('rf_regression_model.pkl', 'rb')) this line.
You used a new version of sklearn to load a model which was trained by an old version of sklearn.
So, the options are:
Retrain the model with current version of sklearn if you have the
training script and data
Or fall back to the lower sklearn version
reported in the warning message
Try this
with open('rf_regression_model','rb') as f:
model=pickle.load(f)

How to apply Undersampling or oversampling to a dataset in Python?

Here's the thing, I have an imbalanced data and I'm trying to use Undersampling.
Perhaps people don't have the solution to my error, but if this is the case, any alternative would be appreciated.
This is what I've done:
from imblearn.under_sampling import RandomUnderSampler
rus = RandomUnderSampler(random_state=0)
X_train_resampled, y_train_resampled = rus.fit_sample(X_train, y_train)
However, I keep getting the error:
AttributeError: 'RandomUnderSampler' object has no attribute '_validate_data'
I saw this post RandomUnderSampler' object has no attribute 'fit_resample', but the answer didn't work. I upgraded the library, it didn't work. I also tried using fit_resample and I got the exact same error.
Any ideas on how to fix this error OR other way of applying Undersampling?
UPDATE: The whole error below (can't show the real data, privacy concerns)
Regarding the version: my Python is 3.7 and scikit-learn 0.23.1

module 'tensorflow.python.keras.api._v2.keras.layers' has no attribute 'CuDNNLSTM'

When I write tf.keras.layers.LSTM, I get the warning
Note that this layer is not optimized for performance. Please use tf.keras.layers.CuDNNLSTM for better performance on GPU.
But when I change the layer to tf.keras.layers.CuDNNLSTM, I get the error
AttributeError: module 'tensorflow.python.keras.api._v2.keras.layers' has no attribute 'CuDNNLSTM'
Tensorflow version is 2.0.0-alpha0, Keras version is 2.2.4-tf.
How can I fix this problem?
In general, in TensorFlow 2.0 we should just use:
tf.keras.layers.LSTM
which, despite the warning, will use the GPU.
The warning message incorrectly existed in the 2.0.0-alpha0 version but has since been removed in 2.0.0-beta1
If for some reason you specifically need the original implementation of tf.keras.layers.CuDNNLSTM then you can use tf.compat.v1.keras.layers.CuDNNLSTM but this would be an edge case.

Tensorflowjs Conversion Error: "ValueError: Unsupported Ops"

I was converting my tensorflow model to tensorflowjs form, using the following command
tensorflowjs_converter
--input_format=tf_saved_model
--output_node_names="my_output_node" \
--saved_model_tags=serve my_saved_model_dir \
./web_model
I encourtered the following mysterious error:
ValueError: Unsupported Ops in the model before optimization NonMaxSuppression, ResizeArea
I do have those operations in my graph. Do I need to swap them out for something more tensorflowjs friendly?
I went deep google, and only came across a reference to the following flag I could add to the tensorflowjs_converter command --skip_op_check=SKIP_OP_CHECK \
This did indeed compile, but then when trying to serve the js model I encountered a js error similar to the above:
Error: Tensorflow Op is not supported: ResizeArea
Any ideas how to modify my graph or my command to navigate this?
Thank you
The short answer is yes, you will need to change them.
TensorflowJS will change the ops for optimisation purposes, but not all the ops have an equivalent TFJS version.
The full list of supported ops is here: https://github.com/tensorflow/tfjs-converter/blob/master/docs/supported_ops.md
Oddly 'NonMaxSuppression' does seem to be on the list, but ResizeArea is not, and will 100% not work.
An alternative is to create a custom operation yourself, and use that code, but I'm not sure how to do that in TFJS.

WARNING:tensorflow:TensorFlowDNNRegressor class is deprecated. Please consider using DNNRegressor as an alternative

Yesterday, I had updated Tensorflow to version 0.9 from 0.8. After which, I am observing the following warning with my Skflow models. Could anyone please let me know what this means? Is there a way to fix it? Are there alternate methods to model DNN Regressor with Skflow?
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py:1197:
VisibleDeprecationWarning: converting an array with ndim > 0 to an
index will result in an error in the future result_shape.insert(dim,
1)
WARNING:tensorflow:TensorFlowDNNRegressor class is deprecated.
Please consider using DNNRegressor as an alternative.
This is just a Warning so your model will train fine. Skflow (or TFLearn same thing) advises you to use skflow.DNNRegressor instead of skflow.TensorFlowDNNRegressor.
Here are the arguments of DNNRegressor and the doc:
tf.contrib.learn.DNNClassifier.__init__(
hidden_units,
feature_columns=None,
model_dir=None,
n_classes=2,
weight_column_name=None,
optimizer=None,
activation_fn=relu,
dropout=None,
config=None)
However, according to this previous post, the new function does not work yet so I will advise you to stay with the old one for now !

Categories