0

I would like to use my PC to train a model using Azure AutoML Forecasting, rather than paying for a compute cluster.

I am using the Azure ML Python SDK v2, and when setting the compute to local, I get an error NoneType' object does not support item assignment

forecasting_job = automl.forecasting(
    compute = "local",
    ...
)

returned_job = ml_client.jobs.create_or_update(forecasting_job)

If I set the compute to a cluster in Azure ML workspace, I get no error, and the job is submitted.

Is using a local PC for training possible?

michaelmsm89
  • 475
  • 7
  • 20

1 Answers1

0

You can train model locally using custom training script and setting up the environment configuration. Follow below steps, First, import required libraries.

from  azureml.core.workspace  import  Workspace
from  azureml.core  import  Experiment
from  azureml.core  import  Environment
from  azureml.core  import  ScriptRunConfig

initialize your workspace object.

ws = Workspace(workspace_name="yourWorkspace",resource_group="yourResourcegroup",subscription_id="your subscription id")
print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep='\n')

Create an experiment.

exp = Experiment(workspace=ws, name='trainl')

Know create user managed environment.

userEnv = Environment("env")
userEnv.python.user_managed_dependencies = True

You can also choose your interpreter as below

userEnv.python.interpreter_path = '/home/xxxx/envs/myenv/bin/python'

Then submit your forecast script along with the folder having dependencies and above created environment as below.

src = ScriptRunConfig(source_directory='./scripts', script='train.py', environment=userEnv)

Here, train.py is the main script and the depending datasets and scripts can be in scripts folder.

In my case it is only mylib.py as shown below.

enter image description here

Code in train.py is on ridge regression model, you can customize to your forecasting. Here is the sample code,

from  sklearn.datasets  import  load_diabetes
from  sklearn.linear_model  import  Ridge
from  sklearn.metrics  import  mean_squared_error
from  sklearn.model_selection  import  train_test_split
from  azureml.core.run  import  Run
import  os
import  mylib
import  joblib

os.makedirs('./outputs', exist_ok=True)
X, y = load_diabetes(return_X_y=True)
run = Run.get_context()
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.2,random_state=0)
alphas = mylib.get_alphas()

for  alpha  in  alphas:
    reg = Ridge(alpha=alpha)
    reg.fit(X_train, y_train)
    preds = reg.predict(X_test)
    mse = mean_squared_error(preds, y_test)
    
    run.log('alpha', alpha)
    run.log('mse', mse)
   
    model_file_name = 'ridge_{0:.2f}.pkl'.format(alpha)
    
    with  open(model_file_name, "wb") as  file:
        joblib.dump(value=reg, filename=os.path.join('./outputs/',model_file_name))
        print('alpha is {0:.2f}, and mse is {1:0.2f}'.format(alpha, mse))

You can see run.log() command ,which is used to log the computation result.

Now, submit the experiment.

run = exp.submit(src)
run.wait_for_completion(show_output = True)

And below is the result.

Execution Summary ================= 
RunId:trainl_1682316457_53f4f8d3
{'runId': 'trainl_1682316457_53f4f8d3',
 'target': 'local', 
 'status': 'Completed', 
 'startTimeUtc': '2023-04-24T06:10:59.353841Z', 
 'endTimeUtc': '2023-04-24T06:16:56.892061Z',
 'services': {}, 
 'properties': {'_azureml.ComputeTargetType': 'local', 
 'ContentSnapshotId': '0a26ea0d-0e81-43a4-8c67-64282d09d72d'}, 
 'inputDatasets': [], 
 'outputDatasets': [], 
 'runDefinition': {'script': 'train.py', 
 'command': '', 
 'useAbsolutePath': False, 
 'arguments': [], 
 'sourceDirectoryDataStore': None, 
 'framework': 'Python', 
 'communicator': 'None', 
 'target': 'local', 
 'dataReferences': {}, 
 'data': {}, 
 'outputData': {}, 
 'datacaches': [], 
 'jobName': None, 
 'maxRunDurationSeconds': 2592000, 'nodeCount': 1,
 ...
 'parameters': []},

You can try this with your custom training script. Refer below documentation for more information. Local-train

JayashankarGS
  • 1,501
  • 2
  • 2
  • 6
  • This doesn't look like AutoML? – michaelmsm89 Apr 25 '23 at 10:03
  • Yes. This is one way to train locally. Using AutoML you need to specify the `compute-target` which i tried as per your question, adding `local`. Even i got the same error. As per this [documentation](https://learn.microsoft.com/en-us/azure/machine-learning/migrate-to-v2-execution-automl?view=azureml-api-2) the `compute-target` need to provide for both SDK v1 and v2. So in AutoML it may not possible to submit job giving as `local`. For more information on local runs refer this [documentation](https://learn.microsoft.com/en-us/azure/machine-learning/migrate-to-v2-local-runs?view=azureml-api-2) – JayashankarGS Apr 25 '23 at 10:39