Unable to save tensorflow model containing the DNNRegressor estimator - python

I am a beginner in Machine Learning, and I've got a small doubt!
I have been working on a machine learning code, where I am using tensorflow for predictions of future values. Now, the code uses a dataset which implements One Hot Encoding initially, then the OHE columns are combined and the estimator function is created. For the estimator model, I have used DNN Regressor. There is no Keras used anywhere in the code.
model=tf.estimator.DNNRegressor(hidden_units=[100,100,100],feature_columns=feature_columns,
optimizer=tf.optimizers.Adam(learning_rate=0.01),
activation_fn=tf.nn.relu )
Now, I tried using Pickle for saving. However, I get this error:
AttributeError: Can't pickle local object 'DNNRegressorV2.__init__.<locals>._model_fn'
I tried the same using joblib, but I got the following issue:
PicklingError: Can't pickle <function DNNRegressorV2.__init__.<locals>._model_fn at 0x00000175F1A0E948>: it's not found as tensorflow_estimator.python.estimator.canned.dnn.DNNRegressorV2.__init__.<locals>._model_fn
Following this, I tried this code
tf.keras.models.save_model( model, filepath, overwrite=True,
include_optimizer=True, save_format=None, signatures=None, options=None )
But I got the error:
AttributeError: 'DNNRegressorV2' object has no attribute 'built'
I have also tried other methods such as model.save(), model.to_json(), and also tried saving using API - none of them worked out.
Can someone help me out with the same?

Related

I wonder if model.tar.gz from Sagemaker can be used for inference in another notebook?

Little background: I successfully ran a regression experiment on AWS and saved the best model from that experiment. I have downloaded my best model as model.tar.gz. to use it for inference on my dataset elsewhere. I extracted it and uploaded the 'xgboost-model' file into my Jupyter Lab workspace, where my dataset is.
regression_model = 'xgboost-model'
predictions = regression_model.predict(X_test)
The error I'm getting is:
----> 1 predictions = regression_model.predict(X_test)
AttributeError: 'str' object has no attribute 'predict'
I know that XGBRegressor has predict attribute, but my model doesn't seem to have it though it's exported as an xgboost model. Any suggesstions on what I'm supposed to be doing instead?
Hey so for your model data, you can use it in another notebook, but you need to make sure the dataset you're predicting on has the same attributes as the data you trained on so that you can predict accurately with the model. Second point to try out is using the boto3 invoke_endpoint call, the predict attribute is from the SageMaker Python SDK. The boto3 SDK is the general AWS Python SDK. I've attached an example of deploying your endpoint with this model.tar.gz, simply passing in the string is not adequate you need to create an endpoint and then perform inference with either the predict call or the invoke_endpoint call.
Example: https://github.com/RamVegiraju/Pre-Trained-Sklearn-SageMaker

How to find out which version of Keras was used for saved model

I've got a saved LSTM model which I want to use. Unfortunately my foreman was kinda sloppy and didn't wrote down which version of Keras he used for training and saving the model.
Is the Keras version saved in the model? Is there a way to find out which Keras version was used for training and saving?
I get this error when I try to load it:
File "/home/ebike/.local/lib/python3.6/site-packages/keras/models.py", line 238, in load_model
model_config = json.loads(model_config.decode('utf-8'))
AttributeError: 'str' object has no attribute 'decode'
As I had read on an other post there might be a issue with the h5py package version. How can I find out which version is the correct one for Keras 2.0.8?
Thank you for your time and help!

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)

Loading pre-trained resnet model to cleverhans model format

I am trying to load a pre-trained ResNet model from the MadryLab CIFAR-10 challenge into CleverHans to compute transfer attacks.
However restoring the saved models into the model_zoo.madry_lab_challenges.cifar10_model.ResNet object does not work. It appears to restore fine initially, but when I try to actually use the model, I get an error such as:
Attempting to use uninitialized value
ResNet/unit_3_1/residual_only_activation/BatchNorm/moving_mean
The easiest way to reproduce this error is to actually just run the provided attack_model.py example included in CleverHans here:
https://github.com/tensorflow/cleverhans/blob/master/examples/madry_lab_challenges/cifar10/attack_model.py
It encounters the same error after loading the model when it tries to use it, on both adv_trained and naturally_trained models.
Is there a workaround to this problem?
It seems the other option is to use the cleverhans.model.CallableModelWrapper instead, but I haven't been able to find an example of how to use that.

AttributeError: 'hyperopt_estimator' object has no attribute '_best_preprocs'

I'm trying to optimize the performance of a sklearn.svm.LinearSVC using hyperopt-sklearn. I'm using the LinearSVC to predict image labels of the CIFAR-10 dataset, and am hoping through hyperopt I'll get improved performance.
However, despite multiple runs, when I run the following:
model = HyperoptEstimator(classifier=hpc.svc_linear('mySVC'),
max_evals=10,trial_timeout=10)
model.fit(x,y)
I get this error:
AttributeError: 'hyperopt_estimator' object has no attribute '_best_preprocs'
This is a bug, as noted in this open issue on the hyperopt-sklearn GitHub, but has no solution. Anyone been able to bypass this error?

Categories