Skip to content

Commit 085f437

Browse files
committed
Work in progress
1 parent 17bc04e commit 085f437

File tree

2 files changed

+29
-45
lines changed

2 files changed

+29
-45
lines changed

code/scoring/score.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
ARISING IN ANY WAY OUT OF THE USE OF THE SOFTWARE CODE, EVEN IF ADVISED OF THE
2424
POSSIBILITY OF SUCH DAMAGE.
2525
"""
26-
import pickle
2726
import json
2827
import numpy
29-
from sklearn.ensemble import RandomForestClassifier
3028
from azureml.core.model import Model
3129

3230

code/training/train.py

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
ARISING IN ANY WAY OUT OF THE USE OF THE SOFTWARE CODE, EVEN IF ADVISED OF THE
2424
POSSIBILITY OF SUCH DAMAGE.
2525
"""
26-
import pickle
27-
from azureml.core import Workspace
2826
from azureml.core.run import Run
2927
import os
3028
from sklearn.datasets import load_diabetes
@@ -33,55 +31,43 @@
3331
from sklearn.model_selection import train_test_split
3432
from sklearn.externals import joblib
3533
import numpy as np
36-
import json
37-
import subprocess
38-
from typing import Tuple, List
39-
40-
# run_history_name = 'devops-ai'
41-
# os.makedirs('./outputs', exist_ok=True)
42-
# #ws.get_details()
43-
# Start recording results to AML
44-
# run = Run.start_logging(workspace = ws, history_name = run_history_name)
45-
run = Run.get_submitted_run()
46-
4734

35+
# using diabetes dataset from scikit-learn
4836
X, y = load_diabetes(return_X_y=True)
49-
columns = ["age", "gender", "bmi", "bp", "s1", "s2", "s3", "s4", "s5", "s6"]
5037
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
5138
data = {"train": {"X": X_train, "y": y_train}, "test": {"X": X_test, "y": y_test}}
5239

53-
print("Running train.py")
54-
55-
# Randomly pic alpha
56-
alphas = np.arange(0.0, 1.0, 0.05)
57-
alpha = alphas[np.random.choice(alphas.shape[0], 1, replace=False)][0]
58-
print(alpha)
59-
run.log("alpha", alpha)
60-
reg = Ridge(alpha=alpha)
61-
reg.fit(data["train"]["X"], data["train"]["y"])
62-
preds = reg.predict(data["test"]["X"])
63-
run.log("mse", mean_squared_error(preds, data["test"]["y"]))
64-
65-
66-
# Save model as part of the run history
67-
model_name = "sklearn_regression_model.pkl"
68-
# model_name = "."
6940

70-
with open(model_name, "wb") as file:
71-
joblib.dump(value=reg, filename=model_name)
41+
def experiment_code(data_split):
42+
run = Run.get_submitted_run()
43+
# Randomly pic alpha
44+
alphas = np.arange(0.0, 1.0, 0.05)
45+
alpha = alphas[np.random.choice(alphas.shape[0], 1, replace=False)][0]
46+
print(alpha)
47+
# Log alpha metric
48+
run.log("alpha", alpha)
49+
# train the model with selected value of alpha and log mse
50+
reg = Ridge(alpha=alpha)
51+
reg.fit(data["train"]["X"], data_split["train"]["y"])
52+
preds = reg.predict(data["test"]["X"])
53+
run.log("mse", mean_squared_error(preds, data_split["test"]["y"]))
7254

73-
# upload the model file explicitly into artifacts
74-
run.upload_file(name="./outputs/" + model_name, path_or_stream=model_name)
75-
print("Uploaded the model {} to experiment {}".format(model_name, run.experiment.name))
76-
dirpath = os.getcwd()
77-
print(dirpath)
55+
# Write model name to the config file
56+
model_name = "sklearn_regression_model.pkl"
57+
with open(model_name, "wb"):
58+
joblib.dump(value=reg, filename=model_name)
7859

60+
# upload the model file explicitly into artifacts
61+
run.upload_file(name="./outputs/" + model_name, path_or_stream=model_name)
62+
print("Uploaded the model {} to experiment {}".format(model_name, run.experiment.name))
63+
dirpath = os.getcwd()
64+
print(dirpath)
7965

80-
# register the model
81-
# run.log_model(file_name = model_name)
82-
# print('Registered the model {} to run history {}'.format(model_name, run.history.name))
66+
print("Following files are uploaded ")
67+
print(run.get_file_names())
68+
run.complete()
8369

8470

85-
print("Following files are uploaded ")
86-
print(run.get_file_names())
87-
run.complete()
71+
if __name__ == "__main__":
72+
print("Running train.py")
73+
experiment_code(data)

0 commit comments

Comments
 (0)