library(dslabs)
mnist <- read_mnist()
vectorize<-function(j){
k <- rep(0,10)
k[j] <- 1 # label col = 1 if num == col
k
}
sigmoid <- function(z) 1/(1+exp(-z))
sigmoidprime <-function(z) exp(-z) / ((1+exp(-z))^2)
cost <- function(y,y_hat) 0.5 * sum((y - y_hat)^2)
#factorial design (alter parameters here)
data_size <- 53660
neuron_a <- neuron_b <- 40
Learning_rate <- 0.75
index <- sample(60000,data_size)
x <- mnist$train$image[index,]
x_hat <- mnist$test$image
trainy <- mnist$train$labels[index]
y<- t(apply(matrix(trainy),1,vectorize)) # flag by col
input_layer_size <- 784
output_layer_size <- 10
hidden_layer_size_1 <- neuron_a
hidden_layer_size_2 <- neuron_b
scalar<-Learning_rate
#initial weights
W_1 <- matrix(runif(input_layer_size * hidden_layer_size_1) - 0.5, nrow = input_layer_size, ncol = hidden_layer_size_1)
W_2 <- matrix(runif(hidden_layer_size_1 * hidden_layer_size_2) - 0.5, nrow = hidden_layer_size_1, ncol = hidden_layer_size_2)
W_3 <- matrix(runif(hidden_layer_size_2 * output_layer_size) - 0.5, nrow = hidden_layer_size_2, ncol = output_layer_size)
#Biases
B1 <- matrix(runif(hidden_layer_size_1),ncol=1)
B2 <- matrix(runif(hidden_layer_size_2),ncol=1)
B3 <- matrix(runif(output_layer_size),ncol=1)
n<-dim(y)[1]
batch_size <- 10
j<-sample(1:n)
j_sub <- seq(1,n,by=batch_size)
for(i in j_sub){
rows<-j[i:(i+batch_size-1)]
X <- x[rows,]/255
Y <- y[rows,]
Z_2 <- X %*% W_1
A_2 <- sigmoid(Z_2 + t(B1 %*% rep(1,batch_size)))
Z_3 <- A_2 %*% W_2
A_3 <- sigmoid(Z_3 + t(B2 %*% rep(1,batch_size)))
Z_4 <- A_3 %*% W_3
Y_hat <- sigmoid(Z_4 + t(B3 %*% rep(1,batch_size)))
#Stochastic Gradient
delta_4 <- (-(Y-Y_hat) * sigmoidprime(Z_4 + t(B3 %*% rep(1,batch_size))))
djdb3 <- rep(1,batch_size) %*% delta_4
djdw3 <- t(A_3) %*% delta_4
delta_3 <- delta_4 %*% t(W_3) * sigmoidprime(Z_3 + t(B2 %*% rep(1,batch_size)))
djdb2 <- rep(1,batch_size) %*% delta_3
djdw2 <- t(A_2) %*% delta_3
delta_2 <- delta_3 %*% t(W_2) * sigmoidprime(Z_2 + t(B1 %*% rep(1,batch_size)))
djdb1 <- rep(1,batch_size) %*% delta_2
djdw1 <- t(X) %*% delta_2
#Update
W_1 <- W_1 - scalar * djdw1
B2 <- B2 - scalar * t(djdb2)
W_2 <- W_2 - scalar * djdw2
B1 <- B1 - scalar * t(djdb1)
W_3 <- W_3 - scalar * djdw3
B3 <- B3 - scalar * t(djdb3)
}
Xt<-x_hat/255
batch_size <- dim(Xt)[1]
Z_2 <- Xt %*% W_1
A_2 <- sigmoid(Z_2 + t(B1 %*% rep(1,batch_size)))
Z_3 <- A_2 %*% W_2
A_3 <- sigmoid(Z_3 + t(B2 %*% rep(1,batch_size)))
Z_4 <- A_3 %*% W_3
Y_hat <- sigmoid(Z_4 + t(B3 %*% rep(1,batch_size)))
guess <- rep(0,batch_size)
for(i in 1:batch_size){
guess[i] <- which.max(Y_hat[i,])
}
sum(guess==mnist$test$labels)/10000
table(guess,mnist$test$labels)
mnist <- read_mnist()
vectorize<-function(j){
k <- rep(0,10)
k[j] <- 1 # label col = 1 if num == col
k
}
sigmoid <- function(z) 1/(1+exp(-z))
sigmoidprime <-function(z) exp(-z) / ((1+exp(-z))^2)
cost <- function(y,y_hat) 0.5 * sum((y - y_hat)^2)
#factorial design (alter parameters here)
data_size <- 53660
neuron_a <- neuron_b <- 40
Learning_rate <- 0.75
index <- sample(60000,data_size)
x <- mnist$train$image[index,]
x_hat <- mnist$test$image
trainy <- mnist$train$labels[index]
y<- t(apply(matrix(trainy),1,vectorize)) # flag by col
input_layer_size <- 784
output_layer_size <- 10
hidden_layer_size_1 <- neuron_a
hidden_layer_size_2 <- neuron_b
scalar<-Learning_rate
#initial weights
W_1 <- matrix(runif(input_layer_size * hidden_layer_size_1) - 0.5, nrow = input_layer_size, ncol = hidden_layer_size_1)
W_2 <- matrix(runif(hidden_layer_size_1 * hidden_layer_size_2) - 0.5, nrow = hidden_layer_size_1, ncol = hidden_layer_size_2)
W_3 <- matrix(runif(hidden_layer_size_2 * output_layer_size) - 0.5, nrow = hidden_layer_size_2, ncol = output_layer_size)
#Biases
B1 <- matrix(runif(hidden_layer_size_1),ncol=1)
B2 <- matrix(runif(hidden_layer_size_2),ncol=1)
B3 <- matrix(runif(output_layer_size),ncol=1)
n<-dim(y)[1]
batch_size <- 10
j<-sample(1:n)
j_sub <- seq(1,n,by=batch_size)
for(i in j_sub){
rows<-j[i:(i+batch_size-1)]
X <- x[rows,]/255
Y <- y[rows,]
Z_2 <- X %*% W_1
A_2 <- sigmoid(Z_2 + t(B1 %*% rep(1,batch_size)))
Z_3 <- A_2 %*% W_2
A_3 <- sigmoid(Z_3 + t(B2 %*% rep(1,batch_size)))
Z_4 <- A_3 %*% W_3
Y_hat <- sigmoid(Z_4 + t(B3 %*% rep(1,batch_size)))
#Stochastic Gradient
delta_4 <- (-(Y-Y_hat) * sigmoidprime(Z_4 + t(B3 %*% rep(1,batch_size))))
djdb3 <- rep(1,batch_size) %*% delta_4
djdw3 <- t(A_3) %*% delta_4
delta_3 <- delta_4 %*% t(W_3) * sigmoidprime(Z_3 + t(B2 %*% rep(1,batch_size)))
djdb2 <- rep(1,batch_size) %*% delta_3
djdw2 <- t(A_2) %*% delta_3
delta_2 <- delta_3 %*% t(W_2) * sigmoidprime(Z_2 + t(B1 %*% rep(1,batch_size)))
djdb1 <- rep(1,batch_size) %*% delta_2
djdw1 <- t(X) %*% delta_2
#Update
W_1 <- W_1 - scalar * djdw1
B2 <- B2 - scalar * t(djdb2)
W_2 <- W_2 - scalar * djdw2
B1 <- B1 - scalar * t(djdb1)
W_3 <- W_3 - scalar * djdw3
B3 <- B3 - scalar * t(djdb3)
}
Xt<-x_hat/255
batch_size <- dim(Xt)[1]
Z_2 <- Xt %*% W_1
A_2 <- sigmoid(Z_2 + t(B1 %*% rep(1,batch_size)))
Z_3 <- A_2 %*% W_2
A_3 <- sigmoid(Z_3 + t(B2 %*% rep(1,batch_size)))
Z_4 <- A_3 %*% W_3
Y_hat <- sigmoid(Z_4 + t(B3 %*% rep(1,batch_size)))
guess <- rep(0,batch_size)
for(i in 1:batch_size){
guess[i] <- which.max(Y_hat[i,])
}
sum(guess==mnist$test$labels)/10000
table(guess,mnist$test$labels)
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Your browser does not support viewing this document. Click here to download the document.
Tic Tac Toe
triples <- list(c(1,2,3),c(4,5,6),c(7,8,9),c(1,4,7),c(2,5,8),c(3,6,9),c(1,5,9),c(3,5,7))
winner<-FALSE
state<-as.character(1:9)
display<-function(state){
cat(" ",state[1],"|",state[2],"|",state[3],"\n")
cat(" ---+---+---","\n")
cat(" ",state[4],"|",state[5],"|",state[6],"\n")
cat(" ---+---+---","\n")
cat(" ",state[7],"|",state[8],"|",state[9],"\n")
}
update<-function(state, who, pos){
if(who==1){state[pos]<-"x"}
else if(who==2){state[pos]<-"o"}
return(state)
}
check_winner<-function(state){
for(i in 1:length(triples)){
if(sum(triples[[i]] %in% which(state=="x"))>=3){cat("x wins, type play() to play again","\n");winner=TRUE;break}
}
for(i in 1:length(triples)){
if(sum(triples[[i]] %in% which(state=="o"))>=3){cat("o wins, type play() to play again","\n");winner=TRUE;break}
}
if(sum(state==as.character(1:9))==0 && winner==FALSE){cat("game draw,type play() to play again.");winner=TRUE}
return(winner)
}
check_illegal<-function(state){
illegal<-TRUE
while(illegal){
pos<-as.double(readline("which position to play(type 1 to 9):"))
if(pos!=1&&pos!=2&&pos!=3&&pos!=4&&pos!=5&&pos!=6&&pos!=7&&pos!=8&&pos!=9){
cat("please type a number from 1 to 9","\n")
}else{
if(state[pos] != "x" && state[pos] != "o"){
state<-update(state,pos)
illegal=FALSE
}else if(state[pos] == "x" || state[pos] == "o"){
cat("you already played there, try again","\n")
}
}
}
return(pos)
}
computer_turn<-function(state){
if(sum(state=="x") > sum(state=="o")){#human goes first playing x and computer second playing o
before<-state
for(i in 1:length(triples)){
if(sum(triples[[i]] %in% which(state=="o"))==2 &&
any(state[triples[[i]][which((triples[[i]] %in% which(state=="o"))==FALSE)]] %in% as.character(1:9)) && any(state[triples[[i]][which((triples[[i]] %in% which(state=="x"))==FALSE)]] %in% as.character(1:9))){#winning
state[triples[[i]][which((triples[[i]] %in% which(state=="o"))==FALSE)]]<-"o"
}
if(identical(state,before)==FALSE)break
}
if(identical(state,before)==TRUE){
for(i in 1:length(triples)){
if(sum(triples[[i]] %in% which(state=="x"))==2 &&
any(state[triples[[i]][which((triples[[i]] %in% which(state=="x"))==FALSE)]] %in% as.character(1:9))&& any(state[triples[[i]][which((triples[[i]] %in% which(state=="o"))==FALSE)]] %in% as.character(1:9))){#blocking
state[triples[[i]][which((triples[[i]] %in% which(state=="x"))==FALSE)]]<-"o"
}
if(identical(state,before)==FALSE)break
}
}
if(identical(state,before)==TRUE){state[sample(which(state == as.character(1:9)),1)]<-"o"}
}else if(sum(state=="o") >= sum(state=="x") || sum(state=="o")==0){#computer goes first playing x and human second playing o
before<-state
for(i in 1:length(triples)){
if(sum(triples[[i]] %in% which(state=="x"))==2 &&
any(state[triples[[i]][which((triples[[i]] %in% which(state=="x"))==FALSE)]] %in% as.character(1:9))&& any(state[triples[[i]][which((triples[[i]] %in% which(state=="o"))==FALSE)]] %in% as.character(1:9))){#winning
state[triples[[i]][which((triples[[i]] %in% which(state=="x"))==FALSE)]]<-"x"
}
if(identical(state,before)==FALSE)break
}
if(identical(state,before)==TRUE){
for(i in 1:length(triples)){
if(sum(triples[[i]] %in% which(state=="o"))==2 &&
any(state[triples[[i]][which((triples[[i]] %in% which(state=="o"))==FALSE)]] %in% as.character(1:9))&& any(state[triples[[i]][which((triples[[i]] %in% which(state=="x"))==FALSE)]] %in% as.character(1:9))){#blocking
state[triples[[i]][which((triples[[i]] %in% which(state=="o"))==FALSE)]]<-"x"
}
if(identical(state,before)==FALSE)break
}
}
if(identical(state,before)==TRUE){state[sample(which(state == as.character(1:9)),1)]<-"x"}
}
return(state)
}
human_verse_computer<-function(state,who){
while(winner == FALSE){
if(who==1){
pos<-check_illegal(state)
state<-update(state, who, pos)
display(state)
winner<-check_winner(state)
if(winner) break
state<-computer_turn(state)
cat("computer plays:","\n")
display(state)
winner<-check_winner(state)
}
else if(who==2){
state<-computer_turn(state)
cat("computer plays:","\n")
display(state)
winner<-check_winner(state)
if(winner) break
pos<-check_illegal(state)
state<-update(state, who, pos)
display(state)
winner<-check_winner(state)
}
}
}
human_verse_human<-function(state,who){
while(winner == FALSE){
pos<-check_illegal(state)
if(who==1){# x's turn
state<-update(state, who, pos)
display(state)
winner<-check_winner(state)
who<-2 # will this 2 lead to "else if" loop?
}
else if(who==2){# o's turn
state<-update(state, who, pos)
display(state)
winner<-check_winner(state)
who<-1
}
}
}
play <- function(){
computer_or_not<-readline("how many players?(type 1 to play against computer):")
if(computer_or_not==1){
who<-readline("you want to go first(type 1) or second(type 2):");cat("x goes first","\n")
display(state)
human_verse_computer(state,who)
}
else if(computer_or_not==2){
who<-1;cat("x goes first","\n")
display(state)
human_verse_human(state,who)
}
}
winner<-FALSE
state<-as.character(1:9)
display<-function(state){
cat(" ",state[1],"|",state[2],"|",state[3],"\n")
cat(" ---+---+---","\n")
cat(" ",state[4],"|",state[5],"|",state[6],"\n")
cat(" ---+---+---","\n")
cat(" ",state[7],"|",state[8],"|",state[9],"\n")
}
update<-function(state, who, pos){
if(who==1){state[pos]<-"x"}
else if(who==2){state[pos]<-"o"}
return(state)
}
check_winner<-function(state){
for(i in 1:length(triples)){
if(sum(triples[[i]] %in% which(state=="x"))>=3){cat("x wins, type play() to play again","\n");winner=TRUE;break}
}
for(i in 1:length(triples)){
if(sum(triples[[i]] %in% which(state=="o"))>=3){cat("o wins, type play() to play again","\n");winner=TRUE;break}
}
if(sum(state==as.character(1:9))==0 && winner==FALSE){cat("game draw,type play() to play again.");winner=TRUE}
return(winner)
}
check_illegal<-function(state){
illegal<-TRUE
while(illegal){
pos<-as.double(readline("which position to play(type 1 to 9):"))
if(pos!=1&&pos!=2&&pos!=3&&pos!=4&&pos!=5&&pos!=6&&pos!=7&&pos!=8&&pos!=9){
cat("please type a number from 1 to 9","\n")
}else{
if(state[pos] != "x" && state[pos] != "o"){
state<-update(state,pos)
illegal=FALSE
}else if(state[pos] == "x" || state[pos] == "o"){
cat("you already played there, try again","\n")
}
}
}
return(pos)
}
computer_turn<-function(state){
if(sum(state=="x") > sum(state=="o")){#human goes first playing x and computer second playing o
before<-state
for(i in 1:length(triples)){
if(sum(triples[[i]] %in% which(state=="o"))==2 &&
any(state[triples[[i]][which((triples[[i]] %in% which(state=="o"))==FALSE)]] %in% as.character(1:9)) && any(state[triples[[i]][which((triples[[i]] %in% which(state=="x"))==FALSE)]] %in% as.character(1:9))){#winning
state[triples[[i]][which((triples[[i]] %in% which(state=="o"))==FALSE)]]<-"o"
}
if(identical(state,before)==FALSE)break
}
if(identical(state,before)==TRUE){
for(i in 1:length(triples)){
if(sum(triples[[i]] %in% which(state=="x"))==2 &&
any(state[triples[[i]][which((triples[[i]] %in% which(state=="x"))==FALSE)]] %in% as.character(1:9))&& any(state[triples[[i]][which((triples[[i]] %in% which(state=="o"))==FALSE)]] %in% as.character(1:9))){#blocking
state[triples[[i]][which((triples[[i]] %in% which(state=="x"))==FALSE)]]<-"o"
}
if(identical(state,before)==FALSE)break
}
}
if(identical(state,before)==TRUE){state[sample(which(state == as.character(1:9)),1)]<-"o"}
}else if(sum(state=="o") >= sum(state=="x") || sum(state=="o")==0){#computer goes first playing x and human second playing o
before<-state
for(i in 1:length(triples)){
if(sum(triples[[i]] %in% which(state=="x"))==2 &&
any(state[triples[[i]][which((triples[[i]] %in% which(state=="x"))==FALSE)]] %in% as.character(1:9))&& any(state[triples[[i]][which((triples[[i]] %in% which(state=="o"))==FALSE)]] %in% as.character(1:9))){#winning
state[triples[[i]][which((triples[[i]] %in% which(state=="x"))==FALSE)]]<-"x"
}
if(identical(state,before)==FALSE)break
}
if(identical(state,before)==TRUE){
for(i in 1:length(triples)){
if(sum(triples[[i]] %in% which(state=="o"))==2 &&
any(state[triples[[i]][which((triples[[i]] %in% which(state=="o"))==FALSE)]] %in% as.character(1:9))&& any(state[triples[[i]][which((triples[[i]] %in% which(state=="x"))==FALSE)]] %in% as.character(1:9))){#blocking
state[triples[[i]][which((triples[[i]] %in% which(state=="o"))==FALSE)]]<-"x"
}
if(identical(state,before)==FALSE)break
}
}
if(identical(state,before)==TRUE){state[sample(which(state == as.character(1:9)),1)]<-"x"}
}
return(state)
}
human_verse_computer<-function(state,who){
while(winner == FALSE){
if(who==1){
pos<-check_illegal(state)
state<-update(state, who, pos)
display(state)
winner<-check_winner(state)
if(winner) break
state<-computer_turn(state)
cat("computer plays:","\n")
display(state)
winner<-check_winner(state)
}
else if(who==2){
state<-computer_turn(state)
cat("computer plays:","\n")
display(state)
winner<-check_winner(state)
if(winner) break
pos<-check_illegal(state)
state<-update(state, who, pos)
display(state)
winner<-check_winner(state)
}
}
}
human_verse_human<-function(state,who){
while(winner == FALSE){
pos<-check_illegal(state)
if(who==1){# x's turn
state<-update(state, who, pos)
display(state)
winner<-check_winner(state)
who<-2 # will this 2 lead to "else if" loop?
}
else if(who==2){# o's turn
state<-update(state, who, pos)
display(state)
winner<-check_winner(state)
who<-1
}
}
}
play <- function(){
computer_or_not<-readline("how many players?(type 1 to play against computer):")
if(computer_or_not==1){
who<-readline("you want to go first(type 1) or second(type 2):");cat("x goes first","\n")
display(state)
human_verse_computer(state,who)
}
else if(computer_or_not==2){
who<-1;cat("x goes first","\n")
display(state)
human_verse_human(state,who)
}
}