options(warn=-1) # turns off warnings, to turn on: "options(warn=0)" library(plot3D) library(pracma) # Importing the libraries for (f in list.files(path="nt_toolbox/toolbox_general/", pattern="*.R")) { source(paste("nt_toolbox/toolbox_general/", f, sep="")) } for (f in list.files(path="nt_toolbox/toolbox_signal/", pattern="*.R")) { source(paste("nt_toolbox/toolbox_signal/", f, sep="")) } fs = 10 Xm = function(X){as.matrix(X - rep(colMeans(X), rep.int(nrow(X), ncol(X))))} Cov = function(X){data.matrix(1. / (n - 1) * t(Xm(X)) %*% Xm(X))} f = function(x,i) { if (i==1) { return(0.5 * (x - 1)**2) } else { return(0.5 * (x + 1)**2) } } df = function(x,i) { if (i==1) { return(x - 1) } else { return(x + 1) } } F = function(x){f(x,1) + f(x,2)} options(repr.plot.width=6, repr.plot.height=6) t = seq(-3,3, length=201) plot(t, f(t,1), col="blue", type='l', ylab="", xlab="") lines(t, f(t,2), col="orange", type='l') lines(t, F(t), col="green", type='l') legend("top", legend=c("f1", "f2", "F"), col=c("blue", "orange", "green"), pch="-") source("nt_solutions/ml_4_sgd/exo0.R") ## Insert your code here. file_name = 'nt_toolbox/data/quantum.csv' A = read.table(file_name, sep=",", head=FALSE) #A = A[sample(dim(A)[1]),] X = A[,1:(dim(A)[2] - 1)] y = A[,dim(A)[2]] y = (2 * y - 1) X[,1] = as.numeric(X[,1]) I = colMeans(abs(as.matrix(X))) > 1e-1 X = X[,I] X = scale(X) n = dim(X)[1] p = dim(X)[2] svd_decomp = svd(Xm(X)) U = svd_decomp$u s = svd_decomp$d V = svd_decomp$v Xr = X %*% t(V) options(repr.plot.width=4, repr.plot.height=4) plot(s, type="o", col=4, ylab="", xlab="", pch=16) options(repr.plot.width=5, repr.plot.height=5) I = sample(n) I = I[1:200] plot_multiclasses(X[I,],y[I], 2) plot_multiclasses(X[I,],y[I], 3) L = function(s,y){1/n * sum( log(1 + exp(-s * y)))} E = function(w,X,y){L(X %*% w, y)} theta = function(v){1 / (1 + exp(-v))} nablaL = function(s, r){ - 1/n * y * theta(-s * y)} nablaE = function(w,X,y){t(X) %*% nablaL(X %*% w,y)} source("nt_solutions/ml_4_sgd/exo1.R") ## Insert your code here. nablaEi = function(w,i){as.numeric(-y[i] * t(X[i,]) * c(theta(-y[i] * (X[i,] %*% w))))} l0 = 100 tau0 = 0.05 source("nt_solutions/ml_4_sgd/exo2.R") ## Insert your code here. source("nt_solutions/ml_4_sgd/exo3.R") ## Insert your code here. source("nt_solutions/ml_4_sgd/exo4.R") ## Insert your code here.