This script uses the office365
Python library to connect to SharePoint Online and retrieve data from a specified list. The data is then converted to PyTorch tensors for use in the machine learning model. The model architecture, loss function, and optimizer are defined and used to train the model on the data. The trained model is used to make predictions on test data, and the predictions are printed to the console.
# Import required libraries
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
import torch
import torch.nn as nn
import torch.optim as optim
# Set up SharePoint Online connection
url = 'https://yourcompany.sharepoint.com/sites/sitename'
username = '[email protected]'
password = 'yourpassword'
auth_context = AuthenticationContext(url)
auth_context.acquire_token_for_user(username, password)
ctx = ClientContext(url, auth_context)
# Get data from SharePoint Online list
list_name = 'YourListName'
data = []
list_items = ctx.web.lists.get_by_title(list_name).get_items()
ctx.load(list_items)
ctx.execute_query()
for item in list_items:
data.append([item.field1, item.field2, item.field3, item.field4, item.field5, item.field6])
# Prepare your data
tensor_data = torch.tensor(data).float()
labels = torch.tensor([0, 1, 0, 1, 1, 0])
# Define your model architecture
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.fc1 = nn.Linear(6, 3)
self.fc2 = nn.Linear(3, 1)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
# Create an instance of your model
model = MyModel()
# Define your loss function and optimizer
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
# Train your model
for epoch in range(100):
optimizer.zero_grad()
output = model(tensor_data)
loss = criterion(output.squeeze(), labels.float())
loss.backward()
optimizer.step()
# Use your trained model for prediction
test_data = torch.tensor([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [2.0, 3.0, 4.0, 5.0, 6.0, 7.0]]).float()
predictions = torch.sigmoid(model(test_data)).round().tolist()
# Print the predictions
print(predictions)