ARB Security Solutions, LLC.

Use PyTorch and SharePoint Online to Train a Simple Neural Network on the Iris dataset

This example shows how to train a simple neural network on the Iris dataset using PyTorch, save the trained model to SharePoint Online, load the trained model from SharePoint Online, and use it to make predictions on new data. This is a common workflow in machine learning, and SharePoint Online can be a useful tool for storing and sharing trained models with others in your organization.

import torch
import torch.nn as nn
import pandas as pd
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.client_request import ClientRequest
from office365.runtime.utilities.request_options import RequestOptions
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File

# Load the Iris dataset
df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None)
X = torch.tensor(df.iloc[:, :-1].values).float()
y = torch.tensor(df.iloc[:, -1].replace({'Iris-setosa': 0, 'Iris-versicolor': 1, 'Iris-virginica': 2}).values).long()

# Define the neural network architecture
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(4, 16)
        self.fc2 = nn.Linear(16, 3)
    
    def forward(self, x):
        x = torch.relu(self.fc1(x))
        x = self.fc2(x)
        return x

# Train the neural network
def train_model():
    model = Net()
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
    for epoch in range(1000):
        y_pred = model(X)
        loss = criterion(y_pred, y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if epoch % 100 == 0:
            print(f"Epoch {epoch}: Loss = {loss.item():.4f}")
    return model

# Save the trained model to SharePoint Online
def save_model_to_sharepoint(model, filename):
    ctx_auth = AuthenticationContext(url='https://yourcompany.sharepoint.com/sites/yourlistsite/')
    ctx_auth.acquire_token_for_user(username='[email protected]', password='yourpassword')
    ctx = ClientContext('https://yourcompany.sharepoint.com/sites/yourlistsite/', ctx_auth)
    file = File.from_url(f"{ctx.service_root_url()}web/getfilebyserverrelativeurl('/sites/yourlistsite/Models/{filename}')")
    with file.get_content_stream() as stream:
        model_bytes = torch.save(model.state_dict(), stream)
    return

# Load the trained model from SharePoint Online
def load_model_from_sharepoint(filename):
    ctx_auth = AuthenticationContext(url='https://yourcompany.sharepoint.com/sites/yourlistsite/')
    ctx_auth.acquire_token_for_user(username='[email protected]', password='yourpassword')
    ctx = ClientContext('https://yourcompany.sharepoint.com/sites/yourlistsite/', ctx_auth)
    file = File.from_url(f"{ctx.service_root_url()}web/getfilebyserverrelativeurl('/sites/yourlistsite/Models/{filename}')")
    with file.get_content_stream() as stream:
        model_bytes = stream.read()
    model = Net()
    model.load_state_dict(torch.load(model_bytes))
    return model

# Make predictions on new data
def predict_species(model, sepal_length, sepal_width, petal_length, petal_width):
    x = torch.tensor([[sepal_length, sepal_width, petal_length, petal_width]]).float()
    y_pred = model(x)
    species_idx = torch.argmax(y_pred).item()
    species_dict = {0: 'Iris-setosa', 1: 'Iris-versicolor', 2 'Iris-virginica'}
return species_dict[species_idx]

Train the model and save it to SharePoint Online
model = train_model()
save_model_to_sharepoint(model, 'iris_model.pt')

Load the model from SharePoint Online and make predictions on new data
loaded_model = load_model_from_sharepoint('iris_model.pt')
print(predict_species(loaded_model, 5.1, 3.5, 1.4, 0.2)) # Expected output: Iris-setosa
print(predict_species(loaded_model, 7.0, 3.2, 4.7, 1.4)) # Expected output: Iris-versicolor
print(predict_species(loaded_model, 6.3, 3.3, 6.0, 2.5)) # Expected output: Iris-virginica

Comments are closed.