Skip to content

Instantly share code, notes, and snippets.

View khanhnamle1994's full-sized avatar
🎯
Focusing

James Le khanhnamle1994

🎯
Focusing
View GitHub Profile
@khanhnamle1994
khanhnamle1994 / run.py
Last active April 24, 2018 03:21
FCN - Run a train a model and save output images resulting from the test image fed on the trained model
def run():
# Download pretrained vgg model
helper.maybe_download_pretrained_vgg(data_dir)
# A function to get batches
get_batches_fn = helper.gen_batch_function(training_dir, image_shape)
with tf.Session() as session:
@khanhnamle1994
khanhnamle1994 / main.py
Last active January 26, 2024 22:14
FCN - Full Code
#--------------------------
# USER-SPECIFIED DATA
#--------------------------
# Tune these parameters
num_classes = 2
image_shape = (160, 576)
EPOCHS = 40
BATCH_SIZE = 16
def get_best_squad_n(formation, nationality, measurement = 'Overall'):
FIFA18_copy = FIFA18.copy()
FIFA18_copy = FIFA18_copy[FIFA18_copy['Nationality'] == nationality]
store = []
for i in formation:
store.append([
FIFA18_copy.loc[[FIFA18_copy[FIFA18_copy['Position'].str.contains(i)][measurement].idxmax()]]['Position'].to_string(index = False),
FIFA18_copy.loc[[FIFA18_copy[FIFA18_copy['Position'].str.contains(i)][measurement].idxmax()]]['Name'].to_string(index = False),
FIFA18_copy[FIFA18_copy['Position'].str.contains(i)][measurement].max(),
def get_summary_n(squad_list, squad_name, nationality_list):
summary = []
for i in nationality_list:
count = 0
for j in squad_list:
# for overall rating
O_temp_rating, _ = get_best_squad_n(formation = j, nationality = i, measurement = 'Overall')
@khanhnamle1994
khanhnamle1994 / operations.py
Last active May 22, 2018 02:41
Add and Multiply Operations for RNN's Forward and Backward Passes
class MultiplyGate:
def forward(self,W, x):
return np.dot(W, x)
def backward(self, W, x, dz):
dW = np.asarray(np.dot(np.transpose(np.asmatrix(dz)), np.asmatrix(x)))
dx = np.dot(np.transpose(W), dz)
return dW, dx
class AddGate:
def forward(self, x1, x2):
@khanhnamle1994
khanhnamle1994 / tanh.py
Last active May 22, 2018 02:36
Tanh Activation Function
class Tanh:
def forward(self, x):
return np.tanh(x)
def backward(self, x, top_diff):
output = self.forward(x)
return (1.0 - np.square(output)) * top_diff
@khanhnamle1994
khanhnamle1994 / RNN_layer.py
Created May 21, 2018 03:29
Implementation of RNN Layers
mulGate = MultiplyGate()
addGate = AddGate()
activation = Tanh()
class RNNLayer:
def forward(self, x, prev_s, U, W, V):
self.mulu = mulGate.forward(U, x)
self.mulw = mulGate.forward(W, prev_s)
self.add = addGate.forward(self.mulw, self.mulu)
self.s = activation.forward(self.add)
@khanhnamle1994
khanhnamle1994 / model_init.py
Created May 21, 2018 03:34
Initializing RNN Model with random weights
class Model:
def __init__(self, word_dim, hidden_dim=100, bptt_truncate=4):
self.word_dim = word_dim
self.hidden_dim = hidden_dim
self.bptt_truncate = bptt_truncate
self.U = np.random.uniform(-np.sqrt(1. / word_dim), np.sqrt(1. / word_dim), (hidden_dim, word_dim))
self.W = np.random.uniform(-np.sqrt(1. / hidden_dim), np.sqrt(1. / hidden_dim), (hidden_dim, hidden_dim))
self.V = np.random.uniform(-np.sqrt(1. / hidden_dim), np.sqrt(1. / hidden_dim), (word_dim, hidden_dim))
@khanhnamle1994
khanhnamle1994 / forward_prop.py
Created May 21, 2018 03:54
The forward propagation to predict word probabilities
def forward_propagation(self, x):
# The total number of time steps
T = len(x)
layers = []
prev_s = np.zeros(self.hidden_dim)
# For each time step...
for t in range(T):
layer = RNNLayer()
input = np.zeros(self.word_dim)
input[x[t]] = 1
@khanhnamle1994
khanhnamle1994 / softmax.py
Created May 21, 2018 03:56
Softmax Output
class Softmax:
def predict(self, x):
exp_scores = np.exp(x)
return exp_scores / np.sum(exp_scores)
def loss(self, x, y):
probs = self.predict(x)
return -np.log(probs[y])
def diff(self, x, y):
probs = self.predict(x)
probs[y] -= 1.0