<jupyter_start><jupyter_text>Reload Saved Models and Test Accuracy for 5000 samples [FNN]<jupyter_code>import numpy as np
from scipy.optimize import linear_sum_assignment
import tensorflow as tf 
from tensorflow import keras
from google.colab import drive 
drive.mount('/content/drive', force_remount=True)

import os 

# Set your working directory to a folder in your Google Drive. This way, if your notebook times out,
# your files will be saved in your Google Drive!

# the base Google Drive directory
root_dir = "/content/drive/My Drive/"

# choose where you want your project files to be saved
project_folder = "LSAP/FNN/"

def create_and_set_working_directory(project_folder):
  # check if your project folder exists. if not, it will be created.
  if os.path.isdir(root_dir + project_folder) == False:
    os.mkdir(root_dir + project_folder)
    print(root_dir + project_folder + ' did not exist but was created.')

  # change the OS to use your project folder as the working directory
  os.chdir(root_dir + project_folder)

  # # create a test file to make sure it shows up in the right place
  # !touch 'new_file_in_working_directory.txt'
  # print('\nYour working directory was changed to ' + root_dir + project_folder + \
  #       "\n\nAn empty text file was created there. You can also run !pwd to confirm the current working directory." )

create_and_set_working_directory(project_folder)
# # Reload Saved Data Set inorder to avoid retraining model
# from google.colab import files
# uploaded = files.upload()

with np.load('/content/drive/My Drive/LSAP/data_f.npz') as data:
  C = data['C']
  C_test = data['C_test']
  X_t1=data['X_t1']
  X_t2=data['X_t2']
  X_t3=data['X_t3']
  X_t4=data['X_t4']
  X_1=data['X_1']
  X_2=data['X_2']
  X_3=data['X_3']
  X_4=data['X_4']
  X_test = data['X_test']

BATCH_SIZE = 1024
SHUFFLE_BUFFER_SIZE = 256

test_dataset1 = tf.data.Dataset.from_tensor_slices((C_test/100, X_t1))
test_dataset1 = test_dataset1.batch(BATCH_SIZE)

test_dataset2 = tf.data.Dataset.from_tensor_slices((C_test/100, X_t2))
test_dataset2 = test_dataset2.batch(BATCH_SIZE)

test_dataset3 = tf.data.Dataset.from_tensor_slices((C_test/100, X_t3))
test_dataset3 = test_dataset3.batch(BATCH_SIZE)

test_dataset4 = tf.data.Dataset.from_tensor_slices((C_test/100, X_t4))
test_dataset4 = test_dataset4.batch(BATCH_SIZE)

# Saved Changes on Dec 1 
# Things to clarified; Training multpile models on the same graph and session and loading in the same graph and session
# Will this lead to wrong results
# To be done
# Interpret ouputs [[g1 g2 g3 g4]] 5000*4*4 and Get Xj and Apply Greedy Collision Rule{if Cij1 < Cij2: assign job i to person j1 } and Get the Inferred Final Ouput
# Now get accuracy of the results with corresponding Hungarain Algo's Output
# Saving Model will find its use when implemented on PC than on colab. As colab runs as if everything is on same script.

#1
n_model1 = tf.keras.models.load_model('saved_model/model1')

# Check its architecture
n_model1.summary()
g1= n_model1.predict(test_dataset1)

#2
n_model2 = tf.keras.models.load_model('saved_model/model2')

# Check its architecture
n_model2.summary()
g2= n_model2.predict(test_dataset2)

#3
n_model3 = tf.keras.models.load_model('saved_model/model3')

# Check its architecture
n_model3.summary()
g3= n_model3.predict(test_dataset3)


#4
n_model4 = tf.keras.models.load_model('saved_model/model4')
# Check its architecture
n_model4.summary()
g4 = n_model4.predict(test_dataset4)
g41= n_model4.evaluate(test_dataset4)
n_model1.evaluate(test_dataset1)
n_model2.evaluate(test_dataset2)
n_model3.evaluate(test_dataset3)

print('Predict:{}'.format(g4[0]))
print('Evaluate:{}'.format(g41[0]))
print(g41)

print(g1.shape)
print(g2.shape)
print(g3.shape)
print(g1[0])
print(g2)
n=4
test = 5000
X_cap = np.zeros((test,n,n),dtype=int)
job = np.zeros((test,n),dtype=int)

p= [0, 1, 2, 3] # Persons {0..j}

count = np.zeros((test,1),dtype=int)

for s in range(test):
    job[s,p[0]] = np.argmax(g1[s])
    job[s,p[1]] = np.argmax(g2[s])
    job[s,p[2]] = np.argmax(g3[s])
    job[s,p[3]] = np.argmax(g4[s])

for s in range(test):
  for i in range(n-1):
    for j in range(i+1,n):
      if (job[s,p[i]]==job[s,p[j]]):
        count[s]+=1

k=0
sum1=0
sum2=0
sum3=0
sum4=0
sum5=0
sum6=0

for s in range(test):
  if(count[s]==0):
    k+=1
  if(count[s]==1):
    sum1+=1
  if(count[s]==2):
    sum2+=1
  if(count[s]==3):
    sum3+=1
  if(count[s]==4):
    sum4+=1
  if(count[s]==5):
    sum5+=1
  if(count[s]==6):
    sum6+=1

print('Collsion Count--> 0: {}| 1: {} | 2: {} | 3: {} | 4: {} | 5: {} | 6: {}'.format(k,sum1,sum2,sum3,sum4,sum5,sum6))

for s in range(test):
  for i in range(n):
    X_cap[s,job[s,p[i]],p[i]] = 1

print('Before Greedy:\n {}'.format(X_cap[0]))
r = np.zeros((test,1),dtype=int)
for s in range(test):
  if count[s]==1:

    for ind1 in range(n):
      r_s = X_cap[s,ind1,:] == np.zeros((n,),dtype=int)
      if (r_s.all()):
        r[s]=ind1
  else: 
    r[s] = -1

print('Before:\n {}'.format(X_cap[0:10]))
# print('Rows----------')
# print(r[0:10])
print(count[0:10])
print('Zero row {}'.format(r[0]))
print('Before Greedy:\n {}'.format(X_cap[0]))
for s in range(test):
  if count[s]==1:
    for i in range(n-1):
      for j in range(i+1,n):
        #Greedy Collison Rule  
        if (job[s,p[i]]==job[s,p[j]]):
            if (C_test[s,job[s,p[i]],p[i]] + C_test[s,r[s],p[j]] < C_test[s,job[s,p[j]],p[j]] + C_test[s,r[s],p[i]]):
                X_cap[s,job[s,p[i]],p[i]] = 1
                X_cap[s,job[s,p[j]],p[j]] = 0
                X_cap[s,r[s],p[j]]        = 1
                X_cap[s,r[s],p[i]]        = 0
            else:
                X_cap[s,job[s,p[j]],p[j]] = 1
                X_cap[s,job[s,p[i]],p[i]] = 0
                X_cap[s,r[s],p[i]]        = 1
                X_cap[s,r[s],p[j]]        = 0

print('After Greedy:\n {}'.format(X_cap[0]))

# Testing Accuracy 
# error = 0
# for s in range(test):
#   comparison = X_cap[s]  == X_test[s]
#   ind = comparison.all()
#   if not ind:
#     error+=1
err = np.zeros((n,1),dtype=int)
# ind = np.zeros((test,1),dtype=bool)
accuracy = np.ones((n,1),dtype=float)
for s in range(test):
  for i in range(n):
    comparison = X_cap[s,:,i]  == X_test[s,:,i]
    ind = comparison.all()
    if not ind:
      err[i]+=1

for i in range(n):
  accuracy[i] = (test-err[i])/test  
  
print('Error:\n{}'.format(err))
print('Accuracy:\n{}'.format(accuracy))
print('Accuracy Avg:{}'.format(sum(accuracy)/len(accuracy)))
print('Jobs for each person:{}'.format(job[0]))
print('Original Sol:\n{}'.format(X_test[0]))
print('Cost matrix 0th:\n{}'.format(C_test[0]))<jupyter_output>[[1]
 [0]
 [0]
 [0]
 [0]
 [0]
 [1]
 [0]
 [1]
 [1]]
Zero row [2]
Before Greedy:
 [[0 1 1 0]
 [1 0 0 0]
 [0 0 0 0]
 [0 0 0 1]]
After Greedy:
 [[0 1 0 0]
 [1 0 0 0]
 [0 0 1 0]
 [0 0 0 1]]
Error:
[[406]
 [438]
 [485]
 [465]]
Accuracy:
[[0.9188]
 [0.9124]
 [0.903 ]
 [0.907 ]]
Accuracy Avg:[0.9103]
Jobs for each person:[1 0 0 3]
Original Sol:
[[0 1 0 0]
 [1 0 0 0]
 [0 0 0 1]
 [0 0 1 0]]
Cost matrix 0th:
[[81  2 14 99]
 [ 4 48 74 58]
 [34 77 59 34]
 [97 59 70 50]]<jupyter_text>Computing Time for Hungarian Algorithm > First Approach Involving Average<jupyter_code># Saved Changes on Dec 1 ; 9.43PM
import timeit

    
setup_code = '''
from scipy.optimize import linear_sum_assignment
import numpy as np
n    = 4      # No. of Jobs, People 
test = 5000   # No, of test samples
C_test = np.random.randint(1,100, size=(test,n,n)) 
X_test = np.zeros((test,n,n),dtype=int)
'''
statement = """
for ii in range(test):
    row_ind, col_ind = linear_sum_assignment(C_test[ii])
    X_test[ii,row_ind,col_ind] = 1
"""
num = 100
time = timeit.timeit(setup = setup_code, stmt = statement, number = num)
avg = time/num
print("Execution time for {} iterations is: {}".format(num,time))
print("Avg Time:{}".format(avg))<jupyter_output>Execution time for 100 iterations is: 15.30944810600002
Avg Time:0.1530944810600002<jupyter_text>> Second One Using Subtraction of time between epoch<jupyter_code>import time

# starting time
start = time.time()

from scipy.optimize import linear_sum_assignment
import numpy as np
n    = 4      # No. of Jobs, People 
test = 5000   # No, of test samples

C_test = np.random.randint(1,100, size=(test,n,n)) 
X_test = np.zeros((test,n,n),dtype=int)
for ii in range(test):
    row_ind, col_ind = linear_sum_assignment(C_test[ii])
    X_test[ii,row_ind,col_ind] = 1
# program body ends

# end time
end = time.time()

# total time taken
print(f"Runtime of the program is {end - start}")<jupyter_output>Runtime of the program is 0.16564273834228516<jupyter_text>Time taken for FNN 1.  Model prediction (In Sequence) and Greedy Collision Rule<jupyter_code>import time
# starting time
start = time.time()
X_cap = np.zeros((test,n,n),dtype=int)
job = np.zeros((test,n),dtype=int)
p= [0, 1, 2, 3] # Persons {0..j}
count = np.zeros((test,1),dtype=int)
for s in range(test):
    job[s,p[0]] = np.argmax(g1[s])
    job[s,p[1]] = np.argmax(g2[s])
    job[s,p[2]] = np.argmax(g3[s])
    job[s,p[3]] = np.argmax(g4[s])
for s in range(test):
  for i in range(n-1):
    for j in range(i+1,n):
      if (job[s,p[i]]==job[s,p[j]]):
        count[s]+=1
for s in range(test):
  for i in range(n):
    X_cap[s,job[s,p[i]],p[i]] = 1
for s in range(test):
  if count[s]==1:
    for i in range(n-1):
      for j in range(i+1,n):
        #Greedy Collison Rule  
        if (job[s,p[i]]==job[s,p[j]]):
            if (C_test[s,job[s,p[i]],p[i]] + C_test[s,r[s],p[j]] < C_test[s,job[s,p[j]],p[j]] + C_test[s,r[s],p[i]]):
                X_cap[s,job[s,p[i]],p[i]] = 1
                X_cap[s,job[s,p[j]],p[j]] = 0
                X_cap[s,r[s],p[j]]        = 1
                X_cap[s,r[s],p[i]]        = 0
            else:
                X_cap[s,job[s,p[j]],p[j]] = 1
                X_cap[s,job[s,p[i]],p[i]] = 0
                X_cap[s,r[s],p[i]]        = 1
                X_cap[s,r[s],p[j]]        = 0
end = time.time()
# total time taken
print(f"Runtime of the program is {end - start}")
import time
# starting time
start = time.time()
g1= n_model1.predict(test_dataset1)
end = time.time()
# total time taken
print(f"Runtime of the program is {end - start}")<jupyter_output>Runtime of the program is 0.023828983306884766
{'fraction_non_alphanumeric': 0.16757228798758006, 'fraction_numerical': 0.0476421502037648, 'mean_word_length': 3.6157635467980294, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '179912', 'license': 'CC0-1.0', 'n_tokens_mistral': 4527, 'n_tokens_neox': 4245, 'n_words': 992, 'path': 'FNN4_test.ipynb', 'repo_name': 'tejaram1729/LSAP_usingColab', 'size': '10306'}