predict_onnx_fl()

La funzione è una funzione predict_onnx_fl()definita dall'utente che prevede l'uso di un modello di Machine Learning con training esistente. Questo modello è stato convertito in formato ONNX , serializzato in stringa e salvato in una tabella standard.

Prerequisiti

  • Il plug-in Python deve essere abilitato nel cluster. Questa operazione è necessaria per Python inline usato nella funzione .
  • Il plug-in Python deve essere abilitato nel database. Questa operazione è necessaria per Python inline usato nella funzione .

Sintassi

T | invoke predict_onnx_fl(, models_tbl, model_name, features_colspred_col)

Altre informazioni sulle convenzioni di sintassi.

Parametri

Nome Tipo Obbligatoria Descrizione
models_tbl string ✔️ Nome della tabella che contiene tutti i modelli serializzati. La tabella deve contenere le colonne seguenti:
name: il nome del modello
timestamp: ora del training del modello
model: rappresentazione di stringa del modello serializzato
model_name string ✔️ Nome del modello specifico da usare.
features_cols synamic ✔️ Matrice contenente i nomi delle colonne delle funzionalità usate dal modello per la stima.
pred_col string ✔️ Nome della colonna in cui sono archiviate le stime.

Definizione di funzione

È possibile definire la funzione incorporando il codice come funzione definita da query o creandola come funzione archiviata nel database, come indicato di seguito:

Definire la funzione usando l'istruzione let seguente. Non sono necessarie autorizzazioni.

Importante

Un'istruzione let non può essere eseguita autonomamente. Deve essere seguita da un'istruzione di espressione tabulare. Per eseguire un esempio funzionante di predict_onnx_fl(), vedere Esempio.

let predict_onnx_fl=(samples:(*), models_tbl:(name:string, timestamp:datetime, model:string), model_name:string, features_cols:dynamic, pred_col:string)
{
    let model_str = toscalar(models_tbl | where name == model_name | top 1 by timestamp desc | project model);
    let kwargs = bag_pack('smodel', model_str, 'features_cols', features_cols, 'pred_col', pred_col);
    let code = ```if 1:

    import binascii

    smodel = kargs["smodel"]
    features_cols = kargs["features_cols"]
    pred_col = kargs["pred_col"]
    bmodel = binascii.unhexlify(smodel)

    features_cols = kargs["features_cols"]
    pred_col = kargs["pred_col"]

    import onnxruntime as rt
    sess = rt.InferenceSession(bmodel)
    input_name = sess.get_inputs()[0].name
    label_name = sess.get_outputs()[0].name
    df1 = df[features_cols]
    predictions = sess.run([label_name], {input_name: df1.values.astype(np.float32)})[0]

    result = df
    result[pred_col] = pd.DataFrame(predictions, columns=[pred_col])

    ```;
    samples | evaluate python(typeof(*), code, kwargs)
};
// Write your query to use the function here.

Esempio

Nell'esempio seguente viene utilizzato l'operatore invoke per eseguire la funzione .

Per usare una funzione definita da query, richiamarla dopo la definizione della funzione incorporata.

let predict_onnx_fl=(samples:(*), models_tbl:(name:string, timestamp:datetime, model:string), model_name:string, features_cols:dynamic, pred_col:string)
{
    let model_str = toscalar(models_tbl | where name == model_name | top 1 by timestamp desc | project model);
    let kwargs = bag_pack('smodel', model_str, 'features_cols', features_cols, 'pred_col', pred_col);
    let code = ```if 1:

    import binascii

    smodel = kargs["smodel"]
    features_cols = kargs["features_cols"]
    pred_col = kargs["pred_col"]
    bmodel = binascii.unhexlify(smodel)

    features_cols = kargs["features_cols"]
    pred_col = kargs["pred_col"]

    import onnxruntime as rt
    sess = rt.InferenceSession(bmodel)
    input_name = sess.get_inputs()[0].name
    label_name = sess.get_outputs()[0].name
    df1 = df[features_cols]
    predictions = sess.run([label_name], {input_name: df1.values.astype(np.float32)})[0]

    result = df
    result[pred_col] = pd.DataFrame(predictions, columns=[pred_col])

    ```;
    samples | evaluate python(typeof(*), code, kwargs)
};
//
// Predicts room occupancy from sensors measurements, and calculates the confusion matrix
//
// Occupancy Detection is an open dataset from UCI Repository at https://archive.ics.uci.edu/ml/datasets/Occupancy+Detection+
// It contains experimental data for binary classification of room occupancy from Temperature,Humidity,Light and CO2.
// Ground-truth labels were obtained from time stamped pictures that were taken every minute
//
OccupancyDetection 
| where Test == 1
| extend pred_Occupancy=bool(0)
| invoke predict_onnx_fl(ML_Models, 'ONNX-Occupancy', pack_array('Temperature', 'Humidity', 'Light', 'CO2', 'HumidityRatio'), 'pred_Occupancy')
| summarize n=count() by Occupancy, pred_Occupancy

Output

Occupazione pred_Occupancy n
TRUE true 3006
FALSE true 112
true FALSE 15
FALSE FALSE 9284

Questa funzionalità non è supportata.