I deploy a keras model (in python) to ML Azure. The deployment ends with the unhealthy state. What does that mean?
I deploy my model with this code :
image_config = ContainerImage.image_configuration(execution_script='script.py',
runtime='python',
conda_file='config_conda.yml')
aciconfig = AciWebservice.deploy_configuration(cpu_cores=1,
memory_gb=1,
description='')
service = Webservice.deploy_from_model(workspace=ws,
name=model_name,
deployment_config=aciconfig,
models=[model],
image_config=image_config)
service.wait_for_deployment(show_output=True)
In the config_conda.yml file, what is the difference between the "pip" section and the "dependencies" section ?
I use the following packages in my script.py:
import pandas as pd
import numpy as np
import string
#scikit-learn
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import LabelEncoder
import nltk
from nltk.corpus import stopwords
from nltk.corpus import wordnet
from nltk.stem import WordNetLemmatizer
from nltk.stem.porter import PorterStemmer
# Word2vec
import gensim
# Keras
from tensorflow import keras
from keras import metrics
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Embedding
from keras.layers import LSTM
from keras.layers import GlobalMaxPool1D
from keras import utils
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
import multiprocessing
See https://learn.microsoft.com/en-us/azure/machine-learning/how-to-deploy-and-where?tabs=azcli#understanding-service-state for understanding service state.
Also see troubleshooting steps
"dependencies" will be installed with conda install x whereas things listed under "pip" will be installed with pip install x. Try to use the conda version whenever possible as it uses precompiled binaries that are less likely to cause issues.
In my experience, the endpoint ends up in Unhealthy state when there is something wrong with either the scoring script or the .yml file (script.py and config_conda.yml, in your case). You can use this command to see the logs and this normally tells you the issue:
print(service.get_logs())
Another debugging method is to try to deploy the service as a LocalService first:
myenv = Environment.from_conda_specification(name = "myenv", file_path = "config_conda.yml")
inference_config = InferenceConfig(entry_script = "script.py", environment = myenv)
deployment_config = LocalWebservice.deploy_configuration(port=6789)
local_service = Model.deploy(ws, "local-test", [model], inference_config, deployment_config)
local_service.wait_for_deployment(show_output = True)
In this way, all steps of the Docker container building process (incl. packages installation) are printed out. You can then delete the local service (local_service.delete()) when you're done.
By the way, you can also deploy ACI web services using Model.deploy instead of Webservice.deploy_from_model (see https://learn.microsoft.com/en-us/python/api/azureml-core/azureml.core.webservice(class)?view=azure-ml-py). This is usually faster, as far as I've seen
Related
I am on my second day of re-taking Python for the gazillionth time!
I am doing a tutorial on ML in Python, using the following code:
import sklearn.tree
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import tree
music_data = pd.read_csv('music.csv')
x = music_data.drop(columns=['genre'])
y = music_data['genre']
model = DecisionTreeClassifier()
model.fit(x,y)
tree.export_graphviz(model, out_file='music-recommender.dot',
feature_names=['age','gender'],
class_names= sorted(y.unique()),
label='all',
rounded=True,
filled=True)
I keep getting the following error:
ImportError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13088/3820271611.py in <module>
2 import pandas as pd
3 from sklearn.tree import DecisionTreeClassifier
----> 4 from sklearn.tree import tree
5
6 music_data = pd.read_csv('music.csv')
ImportError: cannot import name 'tree' from 'sklearn.tree' (C:\Anaconda\lib\site-packages\sklearn\tree\__init__.py)
I've tried to find a solution online, but I don't think it's the version of Python/Anaconda because I literally just installed both. I also don't think it's the sklearn.tree since I was able to import DecisionClassifer.
As this answer indicates, you're looking at some older code; this is always a risk with programming. But there's another thing you need to know about your code.
First off, scikit-learn contains several modules, and almost everything you need from it is in one of those. In my experience, most people import things like this:
from sklearn.tree import DecisionTreeRegressor # A regressor class.
from sklearn.tree import plot_tree # A helpful function.
from sklearn.metrics import mean_squared_error # An evaluation function.
It looks like the tutorial wants something similar to plot_tree(). This new-ish function is much easier to use than the older Graphviz visualization. So unless you really need the DOT file for some reasons, you should be able to do this:
from sklearn.tree import plot_tree
sklearn.tree.plot_tree(model)
Bottom line: there will probably be more broken things in that material. So if I were you I'd either make a new environment with a version of sklearn matching whatever material you're using... or ditch that material and look for something newer.
from sklearn.tree import tree looks wrong. Did you mean from sklearn import tree ?
According to the official Scikit Learn Decision Trees Documentation you really do not need too much of importing.
It can be done simply as follows:
from sklearn import tree
import pandas as pd
music_data = pd.read_csv('music.csv')
X = music_data.drop(columns=['genre'])
y = music_data['genre']
model = tree.DecisionTreeClassifier()
model.fit(X,y)
I'm new to keras and trying to work with this, however, I have problem in the imports.
I can import all the following packages:
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Nadam
from tensorflow.keras.layers import Input, LSTM, Dense
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, TerminateOnNaN
from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator
but when I try to import the time series generatore I get an error:
from keras.preprocessing.sequence import TimeSeriesGenerator
>>>mportError: cannot import name 'TimeSeriesGenerator' from 'keras.preprocessing.sequence' (C:\path\myuser\anaconda3\envs\keras1\lib\site-packages\keras\preprocessing\sequence.py)
This happens after I have created new environment, installed first tensorflow, but nothing changes and I keep getting this error.
What am I missing? how can I solve it and use the timeseries generator?
You misspelled the import, it should be TimeseriesGenerator (lowercase s)
when i run this code
import os
from autokeras import
StructuredDataClassifier
import stellargraph as sg
from stellargraph.mapper import
FullBatchNodeGenerator
from tensorflow.keras import layers,
optimizers, losses, metrics, Model
from sklearn import preprocessing,
model_selection
from IPython.display import display, HTML
import matplotlib.pyplot as plt
%matplotlib inline
i have this error
AttributeError: module 'keras.layers.normalization' has no attribute 'BatchNormalizationBase'
Knowing that this code has been run many times without any problems
In my case which had a structure like this:
I added these two lines to the__init__.py file:
from keras.layers.normalization.layer_normalization import *
from keras.layers.normalization.batch_normalization import *
And problem fixed.
restart runtime then reinstall keras library
I have an issue with calling Python code in MATLAB. My Python code involves predicting the battery state of charge using LSTM with attention ANN based on the inputs sent from MATLAB. The prediction is then sent back to MATLAB. I already have previously trained weights and biases saved in an h5 file, which is loaded and used in the Python code. Below is the Python code:
import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dense
from tensorflow.keras import optimizers
import matplotlib.pyplot as plt
from tensorflow.keras.layers import *
from tensorflow.keras.models import *
from tensorflow.keras import backend as K
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from sklearn.model_selection import train_test_split
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.models import load_model
from tensorflow.keras.layers import Dropout, InputLayer
import h5py
#to create sequential data
def create_inout_sequences(input_data, tw):
inout_seq = []
L = len(input_data)
for i in range(L-tw):
train_seq = input_data[i:i+tw]
#train_out = output_data[i:i+tw]
inout_seq.append(train_seq)
return inout_seq
def search(inputs):
class attention(Layer):
def __init__(self, return_sequences=True,**kwargs):
self.return_sequences = return_sequences
super(attention,self).__init__()
def build(self, input_shape):
self.W=self.add_weight(name="att_weight", shape=(input_shape[-1],1),
initializer="normal")
self.b=self.add_weight(name="att_bias", shape=(input_shape[1],1),
initializer="zeros")
super(attention,self).build(input_shape)
def call(self, x):
e=(K.dot(x,self.W)+self.b)
a = K.softmax(e, axis=1)
output = x*a
if self.return_sequences:
return output
return K.sum(output, axis=1)
def get_config(self):
# For serialization with 'custom_objects'
config = super().get_config()
config['return_sequences'] = self.return_sequences
return config
#convert test data to sequential form
inputs=np.array(inputs)
inputs=np.tile(inputs, (36, 1))
inputs_new=create_inout_sequences(inputs, 35)
inputs_new=np.array(inputs_new)
model1 = Sequential()
model1.add(InputLayer(input_shape=(35,5)))
model1.add((LSTM(22, return_sequences=True)))
model1.add(attention(return_sequences=False))
model1.add(Dense(104, activation="relu"))
model1.add(Dropout(0.2))
model1.add(Dense(1, activation="sigmoid"))
lr_schedule = keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate=0.01,
decay_steps=10000,
decay_rate=0.99)
model1.compile(optimizer=tf.keras.optimizers.Adam(epsilon=1e-08,learning_rate=lr_schedule),loss='mse')
#call previously trained weights
model1.load_weights('SOC_weights.h5')
x=float(model1.predict(inputs_new, batch_size=100,verbose=0))
return x # send prediction to Matlab
Note: I am using Python 3.6, tensorflow version: 2.5, keras version: 2.4.3, h5py version: 3.1.0, cython version: 0.28
I am able to run this code without any error on Python, but have issues when used in MATLAB 2020a... below is my MATLAB code:
pyenv('Version','3.6');
py.importlib.import_module('tensorflow');
py.importlib.import_module('testingSOC'); % file containing the Python codes
inputs=[0.555555556,0.435139205,0.68313128,0.499987472,0.241225578];% test inputs
SOC_output=py.testingSOC.search(inputs)
Below is the error received on Matlab:
Error using training>load_weights (line 2312)
Python Error: ImportError: `load_weights` requires h5py when loading weights from HDF5.
Error in testingSOC>search (line 87)
the error looks like h5py is not identified by MATLAB, so I have tried reinstalling h5py by using the command prompt (I am using Windows 10):
pip uninstall h5py
pip install h5py
but no changes...
I have also tried with tensorflow version: 2.2, keras version 2.4.3, h5py version 2.10 and cython version 0.29 but still get the same error.
I would really appreciate if you guys can provide an insight in solving this issue, and if there are any fundamental parts that I have missed. I would be glad to share more details if required.
Thanks!
Thanks to #TimRoberts for pointing out about including 'py.importlib.import_module('h5py')' which helped me in resolving this issue.Below is my solution, for those who would like to refer:
When I included 'py.importlib.import_module('h5py')' in my matlab codes, I received the following error:
Error using h5>init h5py.h5 (line 1)
Python Error: ImportError: DLL load failed: The specified procedure could not be found.
It looks like Python environment seems to use Matlab's h5 library in my case, which does not have the same features as Python's h5 library...I found that there is an option of running Python codes as a separate process which seems to be working for me (as seen in this link):
https://www.mathworks.com/help/matlab/matlab_external/out-of-process-execution-of-python-functionality.html?searchHighlight=out%20of%20process%20python&s_tid=srchtitle
I want to plot the loss_curve by using the following code:
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPRegressor
def plotCurves(Xtrain,ytrain,Xval,yval):
solver=["lbfgs", "sgd", "adam"]
for i in solver:
mlp=MLPRegressor(activation='relu',max_iter=1000,solver=i)
mlp.fit(Xtrain,ytrain)
pred=mlp.predict(Xval)
print (mlp.score(Xval,yval))
pd.DataFrame(mlp.loss_curve_).plot()
However, when I run my code the following error appears:
'MLPRegressor' object has no attribute 'loss_curve_'
and in the Anaconda IDE version 1.9.7 it appears this method when I am coding.
What can I try to solve this?
Only the stochastic solvers will expose a loss_curve_ attribute on the estimator after fit, so in your first iteration it fails with the lbfgs solver. You can verify this with the following:
from sklearn.datasets import make_classification
from sklearn.neural_network import MLPRegressor
X, y = make_classification(n_samples=5)
solver=[
"lbfgs",
"sgd",
"adam"
]
for i in solver:
mlp = MLPRegressor(activation='relu',solver=i)
mlp.fit(X,y)
print(hasattr(mlp, "loss_curve_"))
False
True
True
If you want to access this attribute, you'll want to stick with either the adam or sgd solver.