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 / loss.py
Created May 21, 2018 03:57
Using cross entropy to calculate loss function
def calculate_loss(self, x, y):
assert len(x) == len(y)
output = Softmax()
layers = self.forward_propagation(x)
loss = 0.0
for i, layer in enumerate(layers):
loss += output.loss(layer.mulv, y[i])
return loss / float(len(y))
def calculate_total_loss(self, X, Y):
@khanhnamle1994
khanhnamle1994 / backpropagation.py
Created May 21, 2018 03:58
Backpropagation Through Time
def bptt(self, x, y):
assert len(x) == len(y)
output = Softmax()
layers = self.forward_propagation(x)
dU = np.zeros(self.U.shape)
dV = np.zeros(self.V.shape)
dW = np.zeros(self.W.shape)
T = len(layers)
prev_s_t = np.zeros(self.hidden_dim)
@khanhnamle1994
khanhnamle1994 / sgd.py
Created May 21, 2018 04:01
Stochastic Gradient Descent
def sgd(self, x, y, learning_rate):
dU, dW, dV = self.bptt(x, y)
self.U -= learning_rate * dU
self.V -= learning_rate * dV
self.W -= learning_rate * dW
TaylorSwift_vc <- paste(as.character(PlaylistSongs$id[1:38]), sep="", collapse=",")
ArianaGrande_vc <- paste(as.character(PlaylistSongs$id[39:86]), sep="", collapse=",")
KendrickLamar_vc <- paste(as.character(PlaylistSongs$id[87:124]), sep="", collapse=",")
ShawnMendes_vc <- paste(as.character(PlaylistSongs$id[125:177]), sep="", collapse=",")
Maroon5_vc <- paste(as.character(PlaylistSongs$id[178:226]), sep="", collapse=",")
PostMalone_vc <- paste(as.character(PlaylistSongs$id[227:261]), sep="", collapse=",")
Kygo_vc <- paste(as.character(PlaylistSongs$id[262:299]), sep="", collapse=",")
TheChainsmokers_vc <- paste(as.character(PlaylistSongs$id[300:333]), sep="", collapse=",")
Adele_vc <- paste(as.character(PlaylistSongs$id[334:358]), sep="", collapse=",")
Drake_vc <- paste(as.character(PlaylistSongs$id[359:408]), sep="", collapse=",")
# Obtain the audio features for each musician
TaylorSwift <- get_features(TaylorSwift_vc)
ArianaGrande <- get_features(ArianaGrande_vc)
KendrickLamar <- get_features(KendrickLamar_vc)
ShawnMendes <- get_features(ShawnMendes_vc)
Maroon5 <- get_features(Maroon5_vc)
PostMalone <- get_features(PostMalone_vc)
Kygo <- get_features(Kygo_vc)
TheChainsmokers <- get_features(TheChainsmokers_vc)
Adele <- get_features(Adele_vc)
library(radarchart)
library(tidyr)
sample1 <- mean_features[mean_features$Musician %in% c("TaylorSwift", "ArianaGrande", "KendrickLamar", "ShawnMendes", "Maroon5", "PostMalone", "Kygo", "TheChainsmokers", "Adele", "Drake"),]
mean_features_norm_1 <- cbind(sample1[1], apply(sample1[-1],2, function(x){(x-min(x)) / diff(range(x))}))
radarDF_1 <- gather(mean_features_norm_1, key=Attribute, value=Score, -Musician) %>%
spread(key=Musician, value=Score)
chartJSRadar(scores = radarDF_1, scaleStartValue = -1, maxScale =1, showToolTipLabel = TRUE)
# K-Means Clustering
library(ggfortify)
library(ggthemes)
set.seed(5000)
k_means <- kmeans(scaled.features, 6)
kmeans_plot <- autoplot(k_means,
main = "K-means Clustering",
data = scaled.features,
# Cluster Radar Chart
mean_features_norm_50 <- cbind(mean_features[1], apply(mean_features[-1],2,scale))
cluster_centers <- as.data.frame(k_means$centers)
cluster <- c("Cluster 1", "Cluster 2", "Cluster 3", "Cluster 4", "Cluster 5", "Cluster 6")
cluster_centers <- cbind(cluster, cluster_centers)
radarDF_6 <- gather(cluster_centers, key=Attribute, value=Score, -cluster) %>%
spread(key=cluster, value=Score)
# Change the colours according to clusters
# Danceability feature
library(ggplot2)
danceability_subset <- mean_features_norm_50[,c("Musician","danceability", "cluster")]
danceability_subset <- danceability_subset[order(danceability_subset$danceability, decreasing = TRUE), ]
danceability_plot <- ggplot(danceability_subset,
aes(x = reorder(Musician, danceability),
y = danceability, label = danceability)) +
xlab("Musician") + ylab("Danceability") +
geom_bar(stat = 'identity', aes(fill = cluster), width = .5) +
# Import libraries
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
# Load training and test data into dataframes
data_train = pd.read_csv('data/fashion-mnist_train.csv')
data_test = pd.read_csv('data/fashion-mnist_test.csv')
# X forms the training images, and y forms the training labels
X = np.array(data_train.iloc[:, 1:])