Skip to content

Refactored the code for recommenders and nlp_class2 #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
420b2bc
modified: rnn_class/util.py
Saif178 Sep 20, 2024
c3ef3c2
added encoder for open function
Saif178 Sep 22, 2024
026d82b
formatting the files
Saif178 Sep 22, 2024
59723de
formatted files
Saif178 Sep 23, 2024
7f05321
removed importing of unwanted libraries
Saif178 Sep 23, 2024
b61a59c
modified files
Saif178 Sep 23, 2024
ea51aed
making code tf 2.0 compatible and cleaning up the script
Saif178 Sep 24, 2024
09b1177
refractored code for tf 2.0
Saif178 Sep 24, 2024
74d86cc
created glove50 model
Saif178 Sep 24, 2024
6d23dea
made the code compatible with tf2
Saif178 Sep 24, 2024
b399e07
refactoring code
Saif178 Oct 14, 2024
f8dc8cc
refactored code and created user-movie relations
Saif178 Oct 14, 2024
8a0ba49
refactored code
Saif178 Oct 14, 2024
b84ac78
Delete movie2user.json
Saif178 Oct 14, 2024
130ccc2
Delete user2movie.json
Saif178 Oct 14, 2024
19f0f8f
Delete usermovie2rating.json
Saif178 Oct 14, 2024
98926ce
Delete usermovie2rating_test.json
Saif178 Oct 14, 2024
1082734
changed saving movie ratings files
Saif178 Oct 14, 2024
9c5372a
refractored code
Saif178 Oct 14, 2024
627d94b
refactoring the code
Saif178 Oct 15, 2024
e7c9eeb
refacoring code
Saif178 Oct 15, 2024
b3c57b3
refactoring code
Saif178 Oct 16, 2024
8a214b8
refactored the code
Saif178 Oct 24, 2024
827272e
refactoring code
Saif178 Oct 25, 2024
edc38b3
refactoring code
Saif178 Oct 26, 2024
8643fe7
refactoring code
Saif178 Oct 26, 2024
b2b3cf9
refactor codes
Saif178 Oct 28, 2024
7f8931b
refactoring code
Saif178 Oct 28, 2024
2391032
refactoring code
Saif178 Oct 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions ann_class2/batch_norm_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
# sudo pip install -U future

import numpy as np
import pandas as pd
#import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
#from sklearn.model_selection import train_test_split
from util import get_normalized_data

if tf.__version__.startswith('2'):
tf.compat.v1.disable_eager_execution()

def init_weight(M1, M2):
return np.random.randn(M1, M2) * np.sqrt(2.0 / M1)
Expand Down Expand Up @@ -38,13 +40,11 @@ def forward(self, X, is_training, decay=0.9):
activation = tf.matmul(X, self.W)
if is_training:
batch_mean, batch_var = tf.nn.moments(activation, [0])
update_running_mean = tf.assign(
self.running_mean,
self.running_mean * decay + batch_mean * (1 - decay)
update_running_mean = self.running_mean.assign(
self.running_mean * decay + batch_mean * (1 - decay)
)
update_running_var = tf.assign(
self.running_var,
self.running_var * decay + batch_var * (1 - decay)
update_running_var = self.running_var.assign(
self.running_var * decay + batch_var * (1 - decay)
)

with tf.control_dependencies([update_running_mean, update_running_var]):
Expand Down Expand Up @@ -115,8 +115,8 @@ def fit(self, X, Y, Xtest, Ytest, activation=tf.nn.relu, learning_rate=1e-2, epo
# for train and test (prediction)

# set up theano functions and variables
tfX = tf.placeholder(tf.float32, shape=(None, D), name='X')
tfY = tf.placeholder(tf.int32, shape=(None,), name='Y')
tfX = tf.compat.v1.placeholder(tf.float32, shape=(None, D), name='X')
tfY = tf.compat.v1.placeholder(tf.int32, shape=(None,), name='Y')

# for later use
self.tfX = tfX
Expand All @@ -131,7 +131,7 @@ def fit(self, X, Y, Xtest, Ytest, activation=tf.nn.relu, learning_rate=1e-2, epo
)
# train_op = tf.train.AdamOptimizer(learning_rate).minimize(cost)
# train_op = tf.train.RMSPropOptimizer(learning_rate, decay=0.99, momentum=0.9).minimize(cost)
train_op = tf.train.MomentumOptimizer(learning_rate, momentum=0.9, use_nesterov=True).minimize(cost)
train_op = tf.compat.v1.train.MomentumOptimizer(learning_rate, momentum=0.9, use_nesterov=True).minimize(cost)
# train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# for testing
Expand All @@ -141,7 +141,7 @@ def fit(self, X, Y, Xtest, Ytest, activation=tf.nn.relu, learning_rate=1e-2, epo
# accuracy = tf.reduce_mean(1.0*(tfY == tf.argmax(logits, 1)))

# init the variables
self.session.run(tf.global_variables_initializer())
self.session.run(tf.compat.v1.global_variables_initializer())

n_batches = N // batch_sz
costs = []
Expand Down Expand Up @@ -187,7 +187,7 @@ def main():

ann = ANN([500, 300])

session = tf.InteractiveSession()
session = tf.compat.v1.InteractiveSession()
ann.set_session(session)

ann.fit(Xtrain, Ytrain, Xtest, Ytest, show_fig=True)
Expand Down
12 changes: 7 additions & 5 deletions ann_class2/dropout_tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from util import get_normalized_data
from sklearn.utils import shuffle

if tf.__version__.startswith('2'):
tf.compat.v1.disable_eager_execution()

class HiddenLayer(object):
def __init__(self, M1, M2):
Expand Down Expand Up @@ -59,8 +61,8 @@ def fit(self, X, Y, Xvalid, Yvalid, lr=1e-4, mu=0.9, decay=0.9, epochs=15, batch
self.params += h.params

# set up theano functions and variables
inputs = tf.placeholder(tf.float32, shape=(None, D), name='inputs')
labels = tf.placeholder(tf.int64, shape=(None,), name='labels')
inputs = tf.compat.v1.placeholder(tf.float32, shape=(None, D), name='inputs')
labels = tf.compat.v1.placeholder(tf.int64, shape=(None,), name='labels')
logits = self.forward(inputs)

cost = tf.reduce_mean(
Expand All @@ -69,7 +71,7 @@ def fit(self, X, Y, Xvalid, Yvalid, lr=1e-4, mu=0.9, decay=0.9, epochs=15, batch
labels=labels
)
)
train_op = tf.train.RMSPropOptimizer(lr, decay=decay, momentum=mu).minimize(cost)
train_op = tf.compat.v1.train.RMSPropOptimizer(lr, decay=decay, momentum=mu).minimize(cost)
# train_op = tf.train.MomentumOptimizer(lr, momentum=mu).minimize(cost)
# train_op = tf.train.AdamOptimizer(lr).minimize(cost)
prediction = self.predict(inputs)
Expand All @@ -85,8 +87,8 @@ def fit(self, X, Y, Xvalid, Yvalid, lr=1e-4, mu=0.9, decay=0.9, epochs=15, batch

n_batches = N // batch_sz
costs = []
init = tf.global_variables_initializer()
with tf.Session() as session:
init = tf.compat.v1.global_variables_initializer()
with tf.compat.v1.Session() as session:
session.run(init)
for i in range(epochs):
print("epoch:", i, "n_batches:", n_batches)
Expand Down
4 changes: 2 additions & 2 deletions ann_class2/keras_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# Note: you may need to update your version of future
# sudo pip install -U future

from keras.models import Model
from keras.layers import Dense, Input
from tensorflow.keras.models import Model #type: ignore
from tensorflow.keras.layers import Dense, Input #type: ignore
from util import get_normalized_data, y2indicator

import matplotlib.pyplot as plt
Expand Down
2 changes: 2 additions & 0 deletions ann_class2/pytorch_batchnorm.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
model.add_module("dense1", torch.nn.Linear(D, 500))
model.add_module("bn1", torch.nn.BatchNorm1d(500))
model.add_module("relu1", torch.nn.ReLU())
model.add_module("dropout1", torch.nn.Dropout(p=0.2))
model.add_module("dense2", torch.nn.Linear(500, 300))
model.add_module("bn2", torch.nn.BatchNorm1d(300))
model.add_module("relu2", torch.nn.ReLU())
model.add_module("dropout2", torch.nn.Dropout(p=0.2))
model.add_module("dense3", torch.nn.Linear(300, K))
# Note: no final softmax!
# just like Tensorflow, it's included in cross-entropy function
Expand Down
19 changes: 10 additions & 9 deletions ann_class2/tensorflow2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@

import numpy as np
import tensorflow as tf

import matplotlib.pyplot as plt

from util import get_normalized_data, y2indicator

if tf.__version__.startswith('2'):
tf.compat.v1.disable_eager_execution()


def error_rate(p, t):
return np.mean(p != t)
Expand All @@ -31,7 +32,7 @@ def main():
print_period = 50

lr = 0.00004
reg = 0.01
#reg = 0.01

Ytrain_ind = y2indicator(Ytrain)
Ytest_ind = y2indicator(Ytest)
Expand All @@ -53,8 +54,8 @@ def main():


# define variables and expressions
X = tf.placeholder(tf.float32, shape=(None, D), name='X')
T = tf.placeholder(tf.float32, shape=(None, K), name='T')
X = tf.compat.v1.placeholder(tf.float32, shape=(None, D), name='X')
T = tf.compat.v1.placeholder(tf.float32, shape=(None, K), name='T')
W1 = tf.Variable(W1_init.astype(np.float32))
b1 = tf.Variable(b1_init.astype(np.float32))
W2 = tf.Variable(W2_init.astype(np.float32))
Expand All @@ -70,19 +71,19 @@ def main():
# softmax_cross_entropy_with_logits take in the "logits"
# if you wanted to know the actual output of the neural net,
# you could pass "Yish" into tf.nn.softmax(logits)
cost = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits_v2(logits=Yish, labels=T))
cost = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits=Yish, labels=T))

# we choose the optimizer but don't implement the algorithm ourselves
# let's go with RMSprop, since we just learned about it.
# it includes momentum!
train_op = tf.train.RMSPropOptimizer(lr, decay=0.99, momentum=0.9).minimize(cost)
train_op = tf.compat.v1.train.RMSPropOptimizer(lr, decay=0.99, momentum=0.9).minimize(cost)

# we'll use this to calculate the error rate
predict_op = tf.argmax(Yish, 1)

costs = []
init = tf.global_variables_initializer()
with tf.Session() as session:
init = tf.compat.v1.global_variables_initializer()
with tf.compat.v1.Session() as session:
session.run(init)

for i in range(max_iter):
Expand Down
14 changes: 7 additions & 7 deletions ann_class2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
#from sklearn.linear_model import LogisticRegression


def get_clouds():
Expand Down Expand Up @@ -70,14 +70,14 @@ def get_spiral():
def get_transformed_data():
print("Reading in and transforming data...")

if not os.path.exists('../large_files/train.csv'):
print('Looking for ../large_files/train.csv')
if not os.path.exists('.\\large_files\\digit-recognizer\\train.csv'):
print('Looking for .\\large_files\\digit-recognizer\\train.csv')
print('You have not downloaded the data and/or not placed the files in the correct location.')
print('Please get the data from: https://www.kaggle.com/c/digit-recognizer')
print('Place train.csv in the folder large_files adjacent to the class folder')
exit()

df = pd.read_csv('../large_files/train.csv')
df = pd.read_csv('.\\large_files\\digit-recognizer\\train.csv')
data = df.values.astype(np.float32)
np.random.shuffle(data)

Expand Down Expand Up @@ -117,14 +117,14 @@ def get_transformed_data():
def get_normalized_data():
print("Reading in and transforming data...")

if not os.path.exists('../large_files/train.csv'):
print('Looking for ../large_files/train.csv')
if not os.path.exists('.\\large_files\\digit-recognizer\\train.csv'):
print('Looking for .\\large_files\\digit-recognizer\\train.csv')
print('You have not downloaded the data and/or not placed the files in the correct location.')
print('Please get the data from: https://www.kaggle.com/c/digit-recognizer')
print('Place train.csv in the folder large_files adjacent to the class folder')
exit()

df = pd.read_csv('../large_files/train.csv')
df = pd.read_csv('.\\large_files\\digit-recognizer\\train.csv')
data = df.values.astype(np.float32)
np.random.shuffle(data)
X = data[:, 1:]
Expand Down
18 changes: 9 additions & 9 deletions cnn_class2/class_activation_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
# Note: you may need to update your version of future
# sudo pip install -U future

from keras.models import Model
from keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
from keras.preprocessing import image
from tensorflow.keras.models import Model #type: ignore
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions #type: ignore
from tensorflow.keras.preprocessing import image #type: ignore

import numpy as np
import scipy as sp
Expand All @@ -19,10 +19,10 @@


# get the image files
# http://www.vision.caltech.edu/Image_Datasets/Caltech101/
# http://www.vision.caltech.edu/Image_Datasets/Caltech256/
image_files = glob('../large_files/256_ObjectCategories/*/*.jp*g')
image_files += glob('../large_files/101_ObjectCategories/*/*.jp*g')
# http://www.vision.caltech.edu/datasets/Caltech101
# http://www.vision.caltech.edu/datasets/Caltech256/
image_files = glob('.\\large_files\\256_ObjectCategories\\*\\*.jp*g')
image_files += glob('.\\large_files\\101_ObjectCategories\\*\\*.jp*g')



Expand All @@ -39,13 +39,13 @@
resnet.summary()

# make a model to get output before flatten
activation_layer = resnet.get_layer('activation_49')
activation_layer = resnet.get_layer('conv5_block3_out')

# create a model object
model = Model(inputs=resnet.input, outputs=activation_layer.output)

# get the feature map weights
final_dense = resnet.get_layer('fc1000')
final_dense = resnet.get_layer('predictions')
W = final_dense.get_weights()[0]


Expand Down
14 changes: 7 additions & 7 deletions cnn_class2/make_limited_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def link(src, dst):
if not os.path.exists(dst):
os.symlink(src, dst, target_is_directory=True)

mkdir('../large_files/fruits-360-small')
mkdir('.\\large_files\\fruits-360-small')


classes = [
Expand All @@ -24,16 +24,16 @@ def link(src, dst):
'Raspberry'
]

train_path_from = os.path.abspath('../large_files/fruits-360/Training')
valid_path_from = os.path.abspath('../large_files/fruits-360/Validation')
train_path_from = os.path.abspath('.\\large_files\\fruits-360\\Training')
valid_path_from = os.path.abspath('\\large_files\\fruits-360\\Validation')

train_path_to = os.path.abspath('../large_files/fruits-360-small/Training')
valid_path_to = os.path.abspath('../large_files/fruits-360-small/Validation')
train_path_to = os.path.abspath('.\\large_files\\fruits-360-small\\Training')
valid_path_to = os.path.abspath('.\\large_files\\fruits-360-small\\Validation')

mkdir(train_path_to)
mkdir(valid_path_to)


for c in classes:
link(train_path_from + '/' + c, train_path_to + '/' + c)
link(valid_path_from + '/' + c, valid_path_to + '/' + c)
link(train_path_from + '\\' + c, train_path_to + '\\' + c)
link(valid_path_from + '\\' + c, valid_path_to + '\\' + c)
Loading