LFD Book Forum  

Go Back   LFD Book Forum > Course Discussions > Online LFD course > Homework 1

Thread Tools Display Modes
Old 09-06-2015, 06:09 AM
taffy84 taffy84 is offline
Junior Member
Join Date: Sep 2015
Posts: 1
Default *Answer Q7-10* Convergence in R

I ran into a problem programming this assignment in R (not a particularly good programmer and beginner in R at that). Anyway, wanted to post so others don't waste as much time as I did.

Basically, my code was working except 80% of the time, and failing to converge 20% of the time. It took me a few hours to finally determine the issue.

The function that was causing the issue is sample(input, number of samples).

I had created an R vector by scoring the perceptron called "wrongs" through:
wrongs <-which(scores!=True)
-where scores was a true/false comparison vector with the input scored by the current perceptron versus the input classified by my earlier random line.
Next, to sample the misclassified points I used:

i <- sample(wrongs, 1)
- this should get me the index(i) of one of the misclassified points, and it does UNLESS there is only 1 misclassified point left. Then it reads the 1 index as a sample up to instead of being the whole space. (IE it was giving me indexes of points that were classified correctly). This caused my algorithm to fail to converge most of the time when it got down to 1 misclassified input.
I fixed this with a simple if/else statement as follows:
wrongs <- which(scores!=TRUE)
			if(length(wrongs) > 1){
				i <- sample(wrongs, 1)
			else {
				i <- wrongs
			weight <- updateperceptron(weight, i, input, y)
I have attached the source code below so that you may see the context. Hopefully this saves someone else the hours I spent troubleshooting my algorithm.

getdataset <- function(n){
	X0 <- rep(1, n)
	X1 <- runif(n, -1, 1)
	X2 <- runif(n, -1, 1)
	Input <- matrix(data = c(X0, X1, X2) , ncol=3)

makeline <- function(){
	xy1 <- runif(2, -1, 1)
	xy2 <- runif(2, -1, 1)
	slope <- (xy1[2] - xy2[2])/(xy1[1] - xy2[1])
	b <- xy2[2] - slope * xy2[1]
	sb <- c(slope, b)
	sb <- as.matrix(sb)

classifyinput <- function(sb, input){
	classy <- input[,2] * sb[1] + sb[2]
	y <- ifelse(classy>input[,3], 1, -1)

scoreperceptron <- function(weight, input, y){
	wy <- input %*% weight
	wy <- sign(wy)
	score <- ifelse(wy==y, TRUE, FALSE)

updateperceptron <- function(weight, i, input, y){
	thepoint <- input[i,]
	they <- y[i]
	weight <- weight + (thepoint * they)


calculateerrorprob <- function(weight, k, sb){
	inputs <- getdataset(k)
	ys <- classifyinput(sb, inputs)
	scored <- scoreperceptron(weight, inputs, ys)
	wrongs <- which(scored!=TRUE)
	score <- length(wrongs)/k
runexperiment <- function(n, maxits, k){
	sb <- makeline()
	input <- getdataset(n)
	y <- classifyinput(sb, input)
	counter <- 0
	weight <- c(0,0,0)
	while(counter < maxits){
		weight <- as.matrix(weight)
		scores <- scoreperceptron(weight, input, y)
		else {
			wrongs <- which(scores!=TRUE)
			if(length(wrongs) > 1){
				i <- sample(wrongs, 1)
			else {
				i <- wrongs
			weight <- updateperceptron(weight, i, input, y)
		counter <- counter + 1
	error <- calculateerrorprob(weight, k, sb)
	return(c(counter, error))

totalexp <- function(trials, n, maxits, k){
		counter <- c(0)
		error <- c(0)
		percents <- c(0)
		trial = 1
		n = n
		maxits = maxits
		k = k
		while (trial < trials){
			countere <- runexperiment(n, maxits, k)
			counter[trial] <- countere[1]
			error[trial] <- countere[2]
			trial = trial + 1
Reply With Quote
Old 09-06-2015, 05:10 PM
yaser's Avatar
yaser yaser is offline
Join Date: Aug 2009
Location: Pasadena, California, USA
Posts: 1,478
Default Re: *Answer Q7-10* Convergence in R

Thank you for your input.
Where everyone thinks alike, no one thinks very much
Reply With Quote
Old 12-17-2015, 10:23 AM
Necro Necro is offline
Junior Member
Join Date: Dec 2015
Posts: 1
Default Re: *Answer Q7-10* Convergence in R

Thank you so much for this. I had exactly the same problem.
Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -7. The time now is 02:03 PM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2022, Jelsoft Enterprises Ltd.
The contents of this forum are to be used ONLY by readers of the Learning From Data book by Yaser S. Abu-Mostafa, Malik Magdon-Ismail, and Hsuan-Tien Lin, and participants in the Learning From Data MOOC by Yaser S. Abu-Mostafa. No part of these contents is to be communicated or made accessible to ANY other person or entity.