diff --git a/samples/R/iris/caret/README.md b/samples/R/iris/caret/README.md new file mode 100644 index 0000000..ba8c20c --- /dev/null +++ b/samples/R/iris/caret/README.md @@ -0,0 +1,84 @@ +# Operationalizing R Models in AzureML + +## Setup + +Create AzureML cluster + +``` +az ml env setup -k +``` + +## Create Bundle + +```R +# import bundleService +source('https://raw.githubusercontent.com/danhartl/Machine-Learning-Operationalization/master/utils/BundleService.R') + +library(caret) +data(iris) +set.seed(12345) +inTrain<-createDataPartition(iris$Species,p=0.7,list=FALSE) +training<-iris[inTrain,] +testing<-iris[-inTrain,] + +# train model + +irisModel<-train(Species~.,method="rpart",data=iris) + +# define operationalization functions + +init <- function() { + library(caret) +} + +predictIris <- function(SepalLength, SepalWidth, PetalLength, PetalWidth) { + input <- data.frame(Sepal.Length = c(SepalLength), Sepal.Width = c(SepalWidth), Petal.Length = c(PetalLength), Petal.Width = c(PetalWidth)) + result <<- as.character(predict(model, input)) +} + +bundleService( + init, + predictIris, + list(model = irisModel), + inputs = list(SepalLength = "numeric", SepalWidth = "numeric", PetalLength = "numeric", PetalWidth = "numeric"), + outputs = list(result = "character"), + outputFolder = "/tmp") + +``` + +## Deploy + +``` +az ml service create realtime -n myservice1 -r mrs -f service.json -d init -d run -d model +``` + +## Test + +``` +az ml service run realtime -n myservice1 -d '{ "SepalLength": 4.7, "SepalWidth": 3.2, "PetalLength": 1.3, "PetalWidth": 0.2 }' +``` + +## Generate client code + +You can download the swagger.json metadata from /swagger.json + +Store it in a file and you can use [autorest](https://www.nuget.org/packages/AutoRest) to generate the client code: + +``` +.\AutoRest.exe -input 'swagger.json' -ClientName Service -CodeGenerator CSharp -Namespace 'AzureML' -OutputDirectory '/tmp' +``` + +With this you can create a sample C# application + +```csharp +internal class Program +{ + private static void Main(string[] args) + { + var service = new Service(new Uri("")); + + var webServiceResult = service.RunMLService(new InputParameters(4.7, 3.2, 1.3, 0.2)); + Console.WriteLine(webServiceResult.OutputParameters.Result); + } +} +``` \ No newline at end of file diff --git a/samples/R/iris/microsoft-r/README.md b/samples/R/iris/microsoft-r/README.md new file mode 100644 index 0000000..370320b --- /dev/null +++ b/samples/R/iris/microsoft-r/README.md @@ -0,0 +1,82 @@ +# Operationalizing R Models in AzureML + +## Setup + +Create AzureML cluster + +``` +az ml env setup -k +``` + +## Create Bundle + +```R +# import bundleService +source('https://raw.githubusercontent.com/danhartl/Machine-Learning-Operationalization/master/utils/BundleService.R') + +library(RevoScaleR); + +# train model + +trainData <- iris; formula <- Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width; +irisModel <- rxBTrees(formula, data = trainData, lossFunction = 'multinomial', nTree = 3,learningRate = 0.1, sampRate = 0.5, maxdepth = 1, minBucket = 1,seed = 1234, replace = FALSE, mTry = 0, maxNumBins = 200); + +# define operationalization functions + +init <- function() { + library(RevoScaleR) +} + +predictIris <- function(SepalLength, SepalWidth, PetalLength, PetalWidth) { + input <- data.frame(Sepal.Length = c(SepalLength), Sepal.Width = c(SepalWidth), Petal.Length = c(PetalLength), Petal.Width = c(PetalWidth)) + + prediction <- rxPredict(model, data = input) + result <<- as.character(prediction$Species_Pred) +} + +bundleService( + init, + predictIris, + list(model = irisModel), + inputs = list(SepalLength = "numeric", SepalWidth = "numeric", PetalLength = "numeric", PetalWidth = "numeric"), + outputs = list(result = "character"), + outputFolder = "/tmp") + +``` + +## Deploy + +``` +az ml service create realtime -n myservice1 -r mrs -f service.json -d init -d run -d model +``` + +## Test + +``` +az ml service run realtime -n myservice1 -d '{ "SepalLength": 4.7, "SepalWidth": 3.2, "PetalLength": 1.3, "PetalWidth": 0.2 }' +``` + +## Generate client code + +You can download the swagger.json metadata from /swagger.json + +Store it in a file and you can use [autorest](https://www.nuget.org/packages/AutoRest) to generate the client code: + +``` +.\AutoRest.exe -input 'swagger.json' -ClientName Service -CodeGenerator CSharp -Namespace 'AzureML' -OutputDirectory '/tmp' +``` + +With this you can create a sample C# application + +```csharp +internal class Program +{ + private static void Main(string[] args) + { + var service = new Service(new Uri("")); + + var webServiceResult = service.RunMLService(new InputParameters(4.7, 3.2, 1.3, 0.2)); + Console.WriteLine(webServiceResult.OutputParameters.Result); + } +} +``` \ No newline at end of file diff --git a/utils/BundleService.R b/utils/BundleService.R new file mode 100644 index 0000000..d2c0357 --- /dev/null +++ b/utils/BundleService.R @@ -0,0 +1,57 @@ +library(jsonlite) + +bundleService <- function(init, run, objects, inputs, outputs, outputFolder) +{ + saveToFile <- function(name, obj) { + binFile <- file(paste(outputFolder, '/', name, sep = ''), 'wb') + binObj <- serialize(obj, NULL) + writeBin(con = binFile, object = binObj) + close(binFile) + } + + saveToFile('init', init) + saveToFile('run', run) + + preloadedObjects <- list(unbox('init'), unbox('run')) + + mapply(function(value, name) { + assign(name,value) + saveToFile(name, value) + }, objects, names(objects)) + + for(objName in names(objects)) { + preloadedObjects[[length(preloadedObjects)+1]] <- unbox(objName) + } + + argumentList = '' + + for(arg in names(inputs)) { + if(argumentList != '') { + argumentList = paste(argumentList, ', ', sep = '') + } + + argumentList = paste(argumentList, arg, sep = '') + } + + convertToParameterDefinition <- function(parameters) { + result <- list() + + for(key in names(parameters)) { + result[[length(result)+1]] <- structure(list(name=unbox(key), type=unbox(parameters[[key]]))) + } + + result + } + + svcmetadata <- structure(list( + runtimeType = unbox("R"), + initCode = unbox("init()"), + code = unbox(paste("run(", argumentList, ")", sep = '')), + inputParameterDefinitions = convertToParameterDefinition(inputs), + outputParameterDefinitions = convertToParameterDefinition(outputs), + preloadedObjects = preloadedObjects + )) + + json <- toJSON(svcmetadata, pretty=TRUE) + write(json, paste(outputFolder, '/service.json', sep = '')) +} \ No newline at end of file