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.

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