<jupyter_start><jupyter_text><jupyter_code>import numpy as np
from scipy.optimize import linear_sum_assignment
import tensorflow as tf 
from tensorflow.keras import datasets, layers, models

n    = 4      # No. of Jobs and People 
m    = 45000   # No, of training samples 
test = 5000   # No, of test samples

# >>>To be run when running for the first time and to be commented after the dataset is fixed<<<

# C = np.random.randint(1,100, size=(m,n,n)) 
# X = np.zeros((m,n,n),dtype=int)
# for i in range(m):
#     row_ind, col_ind = linear_sum_assignment(C[i])
#     X[i,row_ind,col_ind] = 1

# 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

# # Building C(m) and X(m)-->dataset m--training sample index
# # Need to check if training and test data need to be separated before saving
# # Check https://www.tensorflow.org/tutorials/load_data/numpy for model creation

# # print('Cost matrix [i/p]:\n',C)
# # print('Decision Matrix [o/p]:\n',X)
# # print('-'*80)
# X_1 = X[:,:,0]
# X_2 = X[:,:,1]
# X_3 = X[:,:,2]
# X_4 = X[:,:,3]

# X_t1 = X_test[:,:,0]
# X_t2 = X_test[:,:,1]
# X_t3 = X_test[:,:,2]
# X_t4 = X_test[:,:,3]



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/CNN4/"

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)
#Loading the saved dataset generated before
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']
C = np.repeat(C[..., np.newaxis], 1, -1)
C_test = np.repeat(C_test[..., np.newaxis], 1, -1)

model1 = models.Sequential()
model1.add(layers.Conv2D(2, (1, 1), activation='relu', input_shape=(n,n,1)))
model1.add(layers.Conv2D(4, (1, 1), activation='relu'))
model1.add(layers.Flatten())
model1.add(layers.Dense(256, activation='relu'))
model1.add(layers.Dense(n,activation='softmax'))
model1.summary()
opt = tf.keras.optimizers.Adam(learning_rate=0.0001)

model1.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])

history = model1.fit(C/100, X_1, epochs=500,  validation_data=(C_test/100, X_t1))
test_loss, test_acc = model1.evaluate(C_test/100,  X_t1, verbose=2)

!mkdir -p -v saved_model
model1.save('saved_model/model1')

print(test_acc)
C_test.shape
model2 = models.Sequential()
model2.add(layers.Conv2D(2, (1, 1), activation='relu', input_shape=(n,n,1)))
model2.add(layers.Conv2D(4, (1, 1), activation='relu'))
model2.add(layers.Flatten())
model2.add(layers.Dense(256, activation='relu'))
model2.add(layers.Dense(n,activation='softmax'))
model2.summary()
opt = tf.keras.optimizers.Adam(learning_rate=0.0001)

model2.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])

history = model2.fit(C/100, X_2, epochs=500,  validation_data=(C_test/100, X_t2))
test_loss, test_acc = model2.evaluate(C_test/100,  X_t2, verbose=2)

model2.save('saved_model/model2')

print(test_acc)

model3 = models.Sequential()
model3.add(layers.Conv2D(2, (1, 1), activation='relu', input_shape=(n,n,1)))
model3.add(layers.Conv2D(4, (1, 1), activation='relu'))
model3.add(layers.Flatten())
model3.add(layers.Dense(256, activation='relu'))
model3.add(layers.Dense(n,activation='softmax'))
model3.summary()
opt = tf.keras.optimizers.Adam(learning_rate=0.0001)

model3.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])

history = model3.fit(C/100, X_3, epochs=500,  validation_data=(C_test/100, X_t3))
test_loss, test_acc = model3.evaluate(C_test/100,  X_t3, verbose=2)


model3.save('saved_model/model3')
print(test_acc)

model4 = models.Sequential()
model4.add(layers.Conv2D(2, (1, 1), activation='relu', input_shape=(n,n,1)))
model4.add(layers.Conv2D(4, (1, 1), activation='relu'))
model4.add(layers.Flatten())
model4.add(layers.Dense(256, activation='relu'))
model4.add(layers.Dense(n,activation='softmax'))
model4.summary()
opt = tf.keras.optimizers.Adam(learning_rate=0.0001)

model4.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])

history = model4.fit(C/100, X_4, epochs=500,  validation_data=(C_test/100, X_t4))
test_loss, test_acc = model4.evaluate(C_test/100,  X_t4, verbose=2)


model4.save('saved_model/model4')
print(test_acc)

from tensorflow.python import keras
print(keras.__version__)<jupyter_output>2.4.0
{'fraction_non_alphanumeric': 0.16099126403993583, 'fraction_numerical': 0.043144945623105724, 'mean_word_length': 5.508120649651972, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '424140', 'license': 'CC0-1.0', 'n_tokens_mistral': 2280, 'n_tokens_neox': 2180, 'n_words': 453, 'path': 'CNN4.ipynb', 'repo_name': 'tejaram1729/LSAP_usingColab', 'size': '5609'}