optimum_theta <- optim(par = theta_initial, fn = cost ,method = "BFGS",
control=list(maxit=2))
I use the above code and add control argument, but this code will run for a long time and more than two iterations. I do not know why and how to solve this problem.
The following is all of the code that is relevant to this optimization.
cost <- function(theta){
record <- NULL
record_2 <- NULL
num <<- num + 1
for(i in 1:124){
loss <- log(1 + exp(-data_train$CellType[i] * (t(theta) %*%
as.numeric(data_train[i,-1228]))))
record <- c(record,loss)
loss_2 <- log(1 + exp(-data_test$CellType[i] * (t(theta) %*%
as.numeric(data_test[i,-1228]))))
record_2 <- c(record_2,loss_2)
}
training_cost[num] <<- mean(record)
test_cost[num] <<- mean(record_2)
result <- mean(record)
return(result)
}
num <- 0
training_cost <- NULL
test_cost <- NULL
theta_initial <- rep(0, 1227)
optimum_theta <- optim(par = theta_initial, fn = cost, method = "BFGS",
control=list(maxit=2))
#######the following is the update for my question#######
code used is like this:
colnames(data_plot)[3] <- "CellType"
n <- nrow(data_plot)
set.seed(12345)
id <- sample(1:n,floor(n * 0.5))
data_train <- data_plot[id,]
data_test <- data_plot[-id,]
data_train$CellType <- ifelse(data_train$CellType == "T-cell",1,-1)
data_test$CellType <- ifelse(data_test$CellType == "T-cell",1,-1)
data_train_p <- as.matrix(data_train[,-3])
data_test_p <- as.matrix(data_train[,-3])
lossfun <- function(theta,X, Y){
result <- mean(log(1 + exp(-Y * (X %*%theta))))
return(result)
}
cost <- function(theta){
loss_train <- lossfun(theta,X = data_train_p,Y = data_train$CellType)
loss_test <- lossfun(theta,X= data_test_p,Y = data_test$CellType)
num <<- num + 1
training_cost[num] <<- loss_train
test_cost[num] <<- loss_test
return(loss_train)
}
num <- 0
training_cost <- NULL
test_cost <- NULL
theta_initial <- rep(0,2)
optimum_theta <- optim(par = theta_initial,fn = cost,method = "BFGS",control=list(maxit=20))
iteration <- 1:num
data_plot <- data.frame(iteration,training_cost,test_cost)
data_plot <- reshape2::melt(data_plot,id.var = "iteration")
library(ggplot2)
ggplot(data_plot,aes(x=iteration,y= value,color= variable)) + geom_line()