First ML Code on Gradient descent!

Hii there,

Hmm… my first program exceeding 20 lines.

Basically, it is a gradient descent problem (don’t know much about it ­čśŤ ). As I am taking Machine Learning course on Cousera I wanna solve some problems on ML. I found some AI problems (don’t know much about this too) on HakerRank site and started solving this one. This guy is an output of my 5 hours of work. ­čśÇ

import java.util.Scanner;


public class houseCosts {
	public static void main(String[] args){
		System.out.println("Enter");
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int m = in.nextInt();
		//System.out.println(n+""+m);
		float[][] x = new float[n+2][m];
		float[] t = new float[n+1];                       
		float[] temp = new float[n+1];
		float alpha = (float) 0.3;                      //alpha
		//inputs
		for(int j = 0; j<m; j++){
			for(int i=1; i<n+2; i++){
				x[i][j] = in.nextFloat();
				//System.out.println("x "+i+" "+j+"= "+x[i][j]);
			}
		}
		
		int num = in.nextInt();
		//System.out.println(num);
		float[][] out = new float[n+1][num];
		for(int j = 0; j<num; j++){
			for(int i=1; i<n+1; i++){
				out[i][j] = in.nextFloat();
				//System.out.println("out "+i+" "+j+"= "+out[i][j]);
			}
		}
		for(int i=0; i<num; i++){
			out[0][i] = 1;
			//System.out.println("x "+0+" "+i+"= "+x[0][i]);
		}
		
		//Initializations 
		for(int i=0; i<m; i++){
			x[0][i] = 1;
			
			//System.out.println("x "+0+" "+i+"= "+x[0][i]);
		}
		for(int j=0; j<n+1; j++){
			t[j] = 0;                                  //theta value initializing
		}
		
		for(int p = 0; p<500; p++){                   //no. of times
			//body
			for(int k=0; k<n+1; k++){
				float dum = 0;
				for(int j=0; j<m; j++){
					float ans = 0;
					for(int i=0; i<n+1; i++){
						ans+= t[i] * x[i][j];
						//System.out.println(ans);
					}
					ans-= x[n+1][j];
					//System.out.println(ans);
					ans*= x[k][j];
					dum+=ans;
					//System.out.println("x "+k+" "+j+" ="+x[k][j]);
					//System.out.println(ans);
				}
				//System.out.println(dum);
				temp[k] = (float) (t[k]-(alpha * dum * (1.0/m)));
				//System.out.println(temp[k]);
			}
			for(int k=0; k<=n; k++){
				t[k]=temp[k];
				//System.out.print(t[k]+" ");
			}
			//System.out.println(" ");
		}
		
		for(int i = 0; i<num; i++){
			float foo = 0;
			for(int j=0; j<n+1; j++){
				foo+=out[j][i] * t[j];
			}
			System.out.println(foo);
		}
		
	}
}

Don’t judge me by this code coz, I don’t know much about algorithms and ML. And there are so many stdouts because I dunno how to debug in eclipse or any IDE for that mater. BTW, I got ten on ten for this problem.

Something Productive- CHECK!

Don’t forget to travel in time.

Cheerios

Surya

 

Advertisements

I am happy. I am sad.

Namaste,

Now, I am happy after all these boring holidays, did something productive(posting this) besides learning. You might think it’s insane, learning during holidays. But, thank god I am not studying. I am following some Java tutorials on YouTube and trying to understand my first technical book Head First Java. A hell of a book that. Basically I don’t like reading books, fact is that I completed reading How I Braved Anu Aunty and Co-Founded A Million Dollar Company┬áin two days in my second sem and that is my first book. The main reason I don’t like books is the way they appear, those paras, those lines with same font and size, which eventually makes ┬áme scare and to throw it away.

Head First JAVA

Coming to this book,┬áHead First Java, I didn’t throw it away because it’s a soft copy on my laptop. I didn’t delete it because I
love┬áthat book. A ‘Brain-Friendly Guide’,┬áthere’s this phrase on cover page and after reading the book (not whole book, though) I realised that the author┬áreally mean it. I completed reading 200 pages of 700 in two days(surprisingly, I understood) and realised that it’s really Brain-Friendly. All Happies. Hope my Java journey takes me further. Now, sad part is, there is something big to do in my mind but facing difficulties to practically apply it(Yeah, I know something┬ábig always comes with a lot of difficulties). I got selected for two roadtests┬á(reviewing new electronic products, mostly development boards, home automation systems) in which I am about to receive A raspberry-pi with home automation modules(fun part is these modules are self-powered) and A launch pad from TI, MSP430 with bluetooth modules. The real sad part living in India(no hard feelings), it takes a long time to ship these kits to India. All of those who got selected in roadtests have received their kits(not from India). There’s a nice thing to do with those self-powered modules(sensors). But before knowing what can we do with Continue reading