top of page

Updated: Feb 23, 2022

What is TensorFlow?


TensorFlow is a free and open-source end-to-end machine learning library for preprocessing data, modeling data, and serving models. The core open-source library to help you develop and train ML models. Get started quickly by running Colab notebooks directly in your browser.


Why use Tensorflow?


It would be the jump start of your machine learning and deep learning career. Rather than building machine learning and deep learning models from scratch, it's more likely you'll use a library such as TensorFlow. This is because it contains many of the most common machine learning functions you'll want to use.


What is Regression Problem


There are many definitions of the regression problem, however, we would simplify the process to predicting a number, we might want to do the following


For example,

  • Predict the selling price of a house with the given parameters(rooms, house size, number of bathrooms, etc.)

  • Predict the medical insurance price for an individual person using their demographics(age, sex, medical condition, etc.)

We're going to set the foundation for how we can take a sample input dataset, build a neural network and discover patterns of these inputs and then make a prediction(number format) based on those inputs.


The architecture of a regression neural network(Typical)


Why typical? There is an infinite number of ways you can write a neural network, however, the following is a generic setup for ingesting a collection of numbers, fining patterns in them and output would be some target number.


Hyperparameter Typical value

Input layer shape The same shape as number of features (e.g. 3 for # bedrooms, # bathrooms, # car spaces in housing price prediction)

Hidden layer(s) Problem specific, minimum = 1, maximum = unlimited


Neurons per hidden layer Problem specific, generally 10 to 100


Output layer shape The same shape as desired prediction shape (e.g. 1 for house price)


Hidden activation Usually ReLU (rectified linear unit)


Output activation None, ReLU, logistic/tanh


Loss function MSE (mean square error) or MAE (mean absolute error)/Huber (combination of MAE/MSE) if outliers.


Optimizer SGD (stochastic gradient descent), Adam


Creating data to view and fit


We can start our coding now.

Since we're working on a regression problem (predicting a number) let's create some linear data (a straight line) to model.


import numpy as np
import matplotlib.pyplot as plt

# Create features
X = np.array([-7.0, -4.0, -1.0, 2.0, 5.0, 8.0, 11.0, 14.0])

# Create labels
y = np.array([3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0])

# Visualize it
plt.scatter(X, y);
ree












Our goal is to use X as input and predict output y.


In fact, you need to spend the most time on when you work with neural networks: making sure your input and outputs are in the correct shape.


Modeling steps with TensorFlow


Now we know the input and output of the data, let's build a neural network to model it

In TensorFlow, there are typically 3 fundamental steps to creating and training a model.

  1. Creating a model - piece together the layers of a neural network yourself (using the Functional or Sequential API) or import a previously built model (known as transfer learning).

  2. Compiling a model - defining how a models performance should be measured (loss/metrics) as well as defining how it should improve (optimizer).

  3. Fitting a model - letting the model try to find patterns in the data (how does X get to y).

We would build a model using the Keras Sequential API for our regression data, and here are the steps



 import tensorflow as tf
 # Set random seed
 tf.random.set_seed(42)
 
 # Create a model using the Sequential API
 model = tf.keras.Sequential([
  tf.keras.layers.Dense(1)
 ])
 
 # Compile the model
 model.compile(loss=tf.keras.losses.mae,# mae is short for mean abs error 
               # SGD is short for stochastic gradient descent
               optimizer=tf.keras.optimizers.SGD(), 
               metrics=["mae"])
 
 # Fit the model
 # model.fit(X, y, epochs=5) 
 # this will break with TensorFlow 2.7.0+
 model.fit(tf.expand_dims(X, axis=-1), y, epochs=5)
ree

What do you think the outcome should be if we passed our model with an X value of 17.0?


# Make a prediction with the model
model.predict([17.0])
ree


Now y=12.716..., It doesn't go well, it should be something close to 27.0.


How to improve a Model


Common ways to improve a deep model:

  • Adding Layers

  • Increase the number of hidden units

  • Change the activation function

  • Change the optimization function

  • Change the learning rate

  • Fitting on more data

  • Fitting for longer


First step: let's keep it simple, all we'll do is train our model for longer (everything else will stay the same).


 import tensorflow as tf
 # Set random seed
 tf.random.set_seed(42)
 
 # Create a model using the Sequential API
 model = tf.keras.Sequential([
  tf.keras.layers.Dense(1)
 ])
 
 # Compile the model
 model.compile(loss=tf.keras.losses.mae,# mae is short for mean abs error 
               # SGD is short for stochastic gradient descent
               optimizer=tf.keras.optimizers.SGD(), 
               metrics=["mae"])
 
 # Fit the model
 # model.fit(X, y, epochs=5) 
 # this will break with TensorFlow 2.7.0+
 model.fit(tf.expand_dims(X, axis=-1), y, epochs=100) # train for 100 epochs

........

........

ree

How about we try to predict 17.0 again?


# Try and predict what y would be if X was 17.0
model.predict([17.0]) # the right answer is 27.0 (y = X + 10)
ree


Now y=30.158..., much better! Now it is closer to 27.


In my next post, we would work on a bigger dataset to predict the model and provide more ways to improve the deep model.







 
 
 

Updated: Feb 24, 2022

This post is specially designed for uploading the file(s) into the SharePoint document library using Graph API and Python. Here is the proposed architecture



ree

There is no clear documentation from Microsoft to upload documents into Sharepoint sites using python script. Here are steps for uploading documents into the Sharepoint site


1) Register app in Azure Portal

a) First, need to register the app in Azure Portal.
ree

b) While creating the app, please copy the client id and secret for future reference.
c) Please configure the implicit grant and hybrid flows
ree
d) Please provide API permission for files.Readwrite.All and sites.ReadWrite.All

ree


ree

e) Define the scope of the API

ree

f) In the Manifest file, please make sure the following configuration would be true
ree


2) Python script with Graph API to upload files into Sharepoint site.


a) Install Python package Frist needs to install Microsoft Authentication Library (MSAL) for Python

pip install msal


b) Find out site Id and Document Library ID using Graph explorer

Get site ID using Graph explorer and query the following URL(GET method)

https://graph.microsoft.com/v1.0/sites/<tenant name>.sharepoint.com:/sites/<Site Name> i.e. https://graph.microsoft.com/v1.0/sites/sss.sharepoint.com:/sites/pythonsite

ree

Get Sharepoint Document Library ID using Graph explorer and query the following URL(GET method)

https://graph.microsoft.com/v1.0/sites/<site id>/drives

site id - Please use the site id from the previous query

ree

c) Get the access token using the client id, client secret, and tenant id

Here is the python code to get access token import msal

import json

import requests

import os


client_id = '<client id>'

client_secret = '<Client Secret>'

tenant_id = '<Tenant id>'

authority = f"https://login.microsoftonline.com/{tenant_id}"


app = msal.ConfidentialClientApplication(

client_id=client_id,

client_credential=client_secret,

authority=authority)


scopes = ["https://graph.microsoft.com/.default"]

result = None

result = app.acquire_token_silent(scopes, account=None)


if not result: # No access token exists

print(

"No suitable token exists in cache. Let's get a new one from Azure Active

Directory.")

result = app.acquire_token_for_client(scopes=scopes)


if "access_token" in result: # access token exists

print("Access token is " + result["access_token"])



d) Upload files into Sharepoint server

There are two processes to upload files into Sharepoint sites, according to the Microsoft documentation, need to create a session if the file is more than 4MB. i) Upto 4MB file


filename = 'TestUpload.docx'

filepath = '/python/'

file_size = os.path.getsize(filepath+filename)

if (file_size <= 4194304): # Less than 4 MB file

endpoint = f'https://graph.microsoft.com/v1.0/sites/{site_id}/drives/{list_id}/root:/{filename}:/content'

data = open(filepath+filename, 'rb').read()

requests.put(endpoint, data=data,

headers={'Authorization': 'Bearer ' + result['access_token'],

'Content-Type':'application/binary'},)

else: # More than 4 MB file

endpoint = f'https://graph.microsoft.com/v1.0/sites/{site_id}/drives/{list_id}/root:/{filename}:/createUploadSession'

graph_data = requests.post(endpoint,

headers={'Authorization': 'Bearer ' + result['access_token']},).json()

print("Upload URL: %s" % graph_data['uploadUrl'])

data = open(filepath+filename, 'rb').read()

content_range = 'bytes 0-'+str(file_size-1)+'/'+ str(file_size)

f_data = requests.put(graph_data['uploadUrl'], data=data,

headers={'Authorization': 'Bearer ' + result['access_token'],

'Content-Range': content_range,

'Content-Type':'application/binary'},).json()


e) Update Metadata

# Update Metadata

requestURL = f'https://graph.microsoft.com/v1.0/sites/{site_id}/drives/{list_id}/root:/{filename}:/listItem/fields'

metadata = {

"Title": "Test Title",

"DocumentType": "Word Document" # Custom metadata

}

jsondata = json.dumps(metadata)

graph_data_metadata = requests.patch(requestURL, data=jsondata,

headers={'Authorization': 'Bearer ' + result['access_token'],

'Content-Type':'application/json'},).json()


Please email(sumitdeyonline@gmail.com) me if you have any issues or concerns.

 
 
 

Technology Blog - Python - Graph API and SharePoint 

© 2023 by T-MARKET. Proudly created with Wix.com

  • Facebook - Black Circle
  • Twitter - Black Circle
  • Google+ - Black Circle
bottom of page