Page 55 of 56

Re: Random

Posted: Wed Jul 15, 2020 8:16 am
by Idunno
Dinosawer wrote:
Wed Jul 15, 2020 7:03 am
Then you did it wrong. What's your entire code now?
Do you really want to know? :ghost:

Re: Random

Posted: Wed Jul 15, 2020 9:12 am
by Dinosawer
No, I asked but actually I can debug it by looking at the position of the stars :roll:

Re: Random

Posted: Wed Jul 15, 2020 9:13 am
by Idunno
Dinosawer wrote:
Wed Jul 15, 2020 9:12 am
No, I asked but actually I can debug it by looking at the position of the stars :roll:
Really? :shock:

Right, I am sufficiently intimidated, have at it: :ghost:

Code: Select all

#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
double lr=1;
double sigmoid(double x){
	double e=2.71828;
	double k=pow(e, x)/(pow(e, x)+1);
	return k;
}
double dsigmoid(double x){
	double e=2.71828;
	double k=0;
	double g=700;
	if (x<g){
		double j=pow(e, x);
		double l=j+1;
		double h=pow(l,2);
		k=j/h;
	}
	return k;
}
int feedforward(double **weightinlayer, double *input, double *layer1, int in, int layer, double b){
	double placeholder[in][layer];
	double placeholder2[layer][in];
	double placeholder1[layer];
	double placeholder3[layer];
	double r;
	for (int i=0;i<in;i++){
		for (int j=0;j<layer;j++){
			placeholder[i][j]=0;
		}
	}
	for (int i=0;i<in;i++){
		for (int j=0;j<layer;j++){
			placeholder2[j][i]=0;
		}
	}
	for (int i=0;i<layer;i++){
		placeholder1[i]=0;
	}
	for (int i=0;i<layer;i++){
		placeholder3[i]=0;
	}
	for (int i=0;i<in;i++){
		for (int j=0;j<layer;j++){
			placeholder[i][j]=input[i]*weightinlayer[i][j];
		}
	}
	for (int i=0;i<in;i++){
		for(int j=0;j<layer;j++){
			placeholder2[j][i]=placeholder[i][j];
		}
	}
	for (int i=0;i<in;i++){
		for (int j=0;j<layer;j++){
			placeholder3[j]=placeholder2[i][j]+placeholder3[j];
		}
	}
	for (int i=0;i<layer;i++){
		r=placeholder3[i];
		r=r+b;
		r=sigmoid(r);
		layer1[i]=r;
	}
	return 0;
}
int backprop(double **weightlayerout, double **weightinlayer, double *input, double *layer1, double *output, int in, int layer, int out, double b, int answer, int h, double b2){
	double placeholder[in][layer];
	double placeholder2[layer][in];
	double placeholder1[layer];
	double placeholder3[layer];
	double dsig[layer];
	double r;
	for (int j=0;j<layer;j++){
		placeholder3[j]=0;
	}
	for (int i=0;i<layer;i++){
		for (int j=0;j<in;j++){
			placeholder2[i][j]=0;
		}
	}
	for (int i=0;i<in;i++){
		for (int j=0;j<layer;j++){
			placeholder[i][j]=input[i]*weightinlayer[i][j];
		}
	}
	for (int i=0;i<in;i++){
		for(int j=0;j<layer;j++){
			placeholder2[j][i]=placeholder[i][j];
		}
	}
	for (int i=0;i<layer;i++){
		for (int j=0;j<in;j++){
			placeholder3[j]=placeholder2[i][j]+placeholder3[j];
		}
	}
	for (int i=0;i<layer;i++){
		r=placeholder3[i];
		r=r+b;
		r=dsigmoid(r);
		dsig[i]=r;
		//cout<<dsig[i]<<" ";
	}
	ofstream fileplaceholder2;
	fileplaceholder2.open("placeholder2.txt", ios::app);
	fileplaceholder2 << h << ": ";
	for (int i=0;i<layer;i++){	
		for (int j=0;j<in;j++){
			fileplaceholder2 << placeholder2[i][j]<< " ";
		}
		cout << "\n";
	}
	fileplaceholder2<< "\n";
	fileplaceholder2.close();
	ofstream fileplaceholder3;
	fileplaceholder3.open("placeholder3.txt", ios::app);
	fileplaceholder3 << h << ": ";
	for (int j=0;j<layer;j++){
		fileplaceholder3 << placeholder3[j]<< " ";
	}
	fileplaceholder3<< "\n";
	fileplaceholder3.close();
	ofstream filedsig;
	filedsig.open("dsig.txt", ios::app);
	filedsig << h << ": ";
	for (int j=0;j<layer;j++){
		filedsig << dsig[j]<< " ";
	}
	filedsig<< "\n";
	filedsig.close();
	double aplaceholder[layer][out];
	double aplaceholder2[out][layer];
	double aplaceholder1[out];
	double aplaceholder3[out];
	double dsig2[out];
	for (int i=0;i<layer;i++){
		for (int j=0;j<out;j++){
			aplaceholder[i][j]=layer1[i]*weightlayerout[i][j];
		//	cout<< h << ": " << aplaceholder[i][j]<< " ";
		//	cout<< h  << ": " << layer1[i]<<" ";
			//cout << weightlayerout[i][j]<<" ";
		}
		cout<<"\n";
	}
	for(int i=0;i<out;i++){
		for(int j=0;j<layer;j++){
			aplaceholder2[i][j]=0;
		}
	}
	for(int j=0;j<out;j++){
		aplaceholder1[j]=0;
	}
	for(int j=0;j<out;j++){
		aplaceholder3[j]=0;
	}
	for (int i=0;i<layer;i++){
		for(int j=0;j<out;j++){
			aplaceholder1[j]=aplaceholder[i][j];
		}
		for(int j=0;j<out;j++){
			aplaceholder2[j][i]=aplaceholder1[j];
		}
	}
	for (int j=0;j<out;j++){
		aplaceholder3[j]=0;
	}
	for (int i=0;i<layer;i++){
		for (int j=0;j<out;j++){
			aplaceholder3[j]=aplaceholder2[i][j]+aplaceholder3[j];
			//cout << h << ": " << aplaceholder3[j] << "\n";
		}
	}
	for (int i=0;i<out;i++){
		//r=0;
		r=aplaceholder3[i];
		//cout << h << ": "<<r<<" " << "\n";
		r=r+b2;
		dsig2[i]=dsigmoid(r);
		//dsig2[i]=r;
	}
	ofstream fileaplaceholder3;
	fileaplaceholder3.open("aplaceholder3.txt", ios::app);
	fileaplaceholder3 << h << ": ";
	for (int j=0;j<out;j++){
		fileaplaceholder3 << aplaceholder3[j]<< " ";
	}
	fileaplaceholder3<< "\n";
	fileaplaceholder3.close();
	ofstream filedsig2;
	filedsig2.open("dsig2.txt", ios::app);
	filedsig2 << h << ": ";
	for (int j=0;j<out;j++){
		filedsig2 << dsig2[j]<< " ";
	}
	filedsig2<< "\n";
	filedsig2.close();
	double pcpw[layer][out];
	double er[out];
	for (int j=0; j<out;j++){
		if (answer==j){
			er[j]=1-output[j];
		}
		else if (answer!=j){
			er[j]=0-output[j];
		}
	}
	for (int i=0;i<out;i++){
		//cout<<er[i]<<" ";
	}
	for (int i=0;i<layer;i++){
		for (int j=0;j<out;j++){
			pcpw[i][j]=2*er[j]*dsig2[j]*layer1[i];
		}
	}

	double tplaceholder[out][layer];
	double tplaceholder1[out];
	double tplaceholder2[layer][out];
	for (int i=0;i<out;i++){
		for (int j=0;j<layer;j++){
			tplaceholder[i][j]=0;
		}
	}
	for (int j=0;j<out;j++){
		tplaceholder1[j]=0;
	}
	for (int i=0;i<out;i++){
		for (int j=0;j<layer;j++){
			tplaceholder2[j][i]=0;
		}
	}
	for (int i=0;i<layer;i++){
		for (int j=0;j<out;j++){
			tplaceholder2[i][j]=weightlayerout[i][j];
			//cout << tplaceholder2[i][j] << " ";
		}
		//cout <<"\n";
	}
	for (int i=0;i<layer;i++){
		for(int j=0;j<out;j++){
			tplaceholder[j][i]=tplaceholder2[i][j];
		}
	}
	for (int i=0;i<out;i++){
		for (int j=0;j<layer;j++){
			tplaceholder[i][j]=tplaceholder[i][j]*er[i];
		}
	}
	double erplaceholder[layer];
	for(int i=0; i<layer; i++){
		erplaceholder[i]=0;
	}
	for(int i=0;i<out;i++){
		for (int j=0;j<layer;j++){
			erplaceholder[j]=tplaceholder[i][j]*dsig[i]+erplaceholder[j];
		}
	}
	ofstream filetplaceholder;
	filetplaceholder.open("tplaceholder.txt", ios::app);
	filetplaceholder << h << ": ";
	for (int j=0;j<layer;j++){
		filetplaceholder << tplaceholder[j]<< " ";
	}
	filetplaceholder << "\n";
	filetplaceholder.close();
	double pcpw2[in][layer];
	ofstream fileer;
	fileer.open("er.txt", ios::app);
	fileer << h << ": ";
	for (int j=0;j<out;j++){
		fileer << er[j]<< " ";
	}
	fileer<< "\n";
	fileer.close();
	ofstream fileerplaceholder;
	fileerplaceholder.open("erplaceholder.txt", ios::app);
	fileerplaceholder << h << ": ";
	for (int j=0;j<layer;j++){
		fileerplaceholder << erplaceholder[j]<< " ";
	}
	fileerplaceholder << "\n";
	fileerplaceholder.close();
	for(int i=0;i<in;i++){
		for (int j=0;j<layer;j++){
			pcpw2[i][j]=erplaceholder[j]*input[i];
		}
	}
	for (int i=0;i<layer;i++){
		for (int j=0;j<out;j++){
			weightlayerout[i][j]=weightlayerout[i][j]-(lr*pcpw[i][j]);
		}
	}
	double k;
	for (int i=0;i<in;i++){
		for (int j=0;j<layer;j++){
			k=lr*pcpw2[i][j];
			weightinlayer[i][j]=weightinlayer[i][j]-k;
		}
	}
	for (int i=0; i<out;i++){
		b=b-2*er[i]*dsig2[i];
	}
	ofstream fileb;
	fileb.open("b.txt", ios::app);
	fileb << h << ": ";
	fileb << b << " ";
	fileb << "\n";
	fileb.close();
	cout << b << " ";
	for (int i=0; i<out;i++){
		b2=b2-2*erplaceholder[i]*dsig[i];
	}
	ofstream fileb2;
	fileb2.open("b2.txt", ios::app);
	fileb2 << h << ": ";
	fileb2 << b2 << " ";
	fileb2 << "\n";
	fileb2.close();
	ofstream filepcpw2;
	filepcpw2.open("pcpw2.txt", ios::app);
	for (int i=0;i<in;i++){
		filepcpw2 << h << ": ";
		for (int j=0;j<layer;j++){
			filepcpw2 << pcpw2[i][j]<< " ";
		}
		filepcpw2 << "\n";
	}
	filepcpw2.close();
	ofstream filepcpw;
	filepcpw.open("pcpw.txt", ios::app);
	for (int i=0;i<layer;i++){
		filepcpw << h << ": ";
		for (int j=0;j<out;j++){
			filepcpw << pcpw[i][j]<< " ";
		}
		filepcpw << "\n";
	}
	filepcpw.close();
	return 0;
}
int checkanswer(double *output, int out){
	double answertrue=0;
	double h, l;
	for (int i=0; i<out; i++){
		if (h<output[i]){
			h=output[i];
			l=i;
		}
	}
	answertrue=l;
	return answertrue;
}
int main(){
	int set=4;
	int in=2*set;
	int out=set*set;
	int layer=(in+out)/2;
	double input[in];
	double layer1[layer];
	double output[out];
	double b=rand()%10;
	double b2=rand()%10;
	double **weightinlayer=new double *[in];
	for (int i=0;i<in;i++){
		weightinlayer[i]=new double [layer];
	}	
	double **weightlayerout=new double *[layer];
	for (int i=0;i<layer;i++){
		weightlayerout[i]=new double [out];
	}
	srand(0);
	for (int i=0;i<in;i++){
		for(int j=0;j<layer;j++){
			weightinlayer[i][j]=rand()%10;
		}
	}
	for (int i=0;i<in;i++){
		for(int j=0;j<layer;j++){
			weightlayerout[i][j]=rand()%10;
		}
	}
	remove("b2.txt");
	remove("b.txt");
	remove("placeholder2.txt");
	remove("tplaceholder.txt");
	remove("placeholder3.txt");
	remove("dsig.txt");
	remove("output.txt");
	remove("er.txt");
	remove("input.txt");
	remove("erplaceholder.txt");
	remove("pcpw2.txt");
	remove("pcpw.txt");
	remove("weightlo.txt");
	remove("weightil.txt");
	ofstream fileweightil;
	ofstream fileweightlo;
	double batch=100;
	double correct;
	for (int i=0; i<batch;i++){
		//cout<< i << ": ";
		int one=rand()%4;
		int two=rand()%4;
		for ( int i=0; i<in; i++){
			input[i]=0;
		}
		input[one]=1;
		input[two+set]=1;
		double answer=one+two;
		feedforward(weightinlayer, input, layer1, in, layer, b2);
		/*for (int k=0;k<layer; k++){
			cout << layer1[i] << " ";
		}
		cout << "\n";*/
		feedforward(weightlayerout, layer1, output, layer, out, b);
		/*for (int i=0;i<layer;i++){
			cout << layer1[i]<<" ";
		}
		cout << "\n";
		/*for (int i=0; i<out; i++){
			cout << output[i] << " ";
		}
		cout<< i <<":";
		for (int k=0;k<in;k++){
			for (int j=0;j<layer;j++){
				cout << "  "<< weightinlayer[k][j]<< " ";
			}
			cout << "\n";
		}*//*
		for (int k=0;k<layer;k++){
			for (int j=0;j<out;j++){
				cout << "  "<< weightlayerout[k][j]<< " ";
			}
			cout << "\n";
		}/*
		for (int i=0;i<out;i++){
			cout << output[i] << " ";
		}*/
		fileweightil.open("weightil.txt", ios::app);
		for (int k=0; k<in;k++){
			fileweightil<< i << ": ";
			for (int j=0;j<layer;j++){
				fileweightil << weightinlayer[k][j]  << " ";
			}
			fileweightil << "\n";
		}
		fileweightil.close();
		fileweightlo.open("weightlo.txt", ios::app);
		for (int k=0; k<layer;k++){
			fileweightlo<< i << ": ";
			for (int j=0;j<out;j++){
				fileweightlo << weightlayerout[k][j]  << " ";
			}
			fileweightlo << "\n";
		}
		fileweightlo.close();
		backprop(weightlayerout, weightinlayer, input, layer1, output, in, layer, out, b, answer, i, b2);
		/*for (int k=0;k<in;k++){
			for (int j=0;j<layer; j++){
				if (weightinlayer[k][j]!=weightinlayer[k][j]){
					weightinlayer[k][j]=rand()%10;
				}
			}
		}
		for (int k=0;k<layer;k++){
			for (int j=0;j<out; j++){
				if (weightlayerout[k][j]!=weightlayerout[k][j]){
					weightlayerout[k][j]=rand()%10;
				}
			}
		}*/
		int answertrue=checkanswer(output, out);
		ofstream fileinput;
		fileinput.open("input.txt", ios::app);
		fileinput << i << ": ";
		for (int j=0;j<in;j++){
			fileinput << input[j]<< " ";
		}
		fileinput << "\n";
		fileinput.close();
		ofstream fileout;
		fileout.open("output.txt", ios::app);
		fileout << i << ": ";
		for (int j=0;j<out;j++){
			fileout << output[j]<< " ";
		}
		fileout<< "\n";
		fileout.close();
		if (answertrue==answer){
			correct++;
		}

		//cout << "\n";
		//cout<< answertrue<< "  "<< answer << "\n";
	}
	double percent=correct/batch*100;
	cout << correct << " "<< percent << "%\n";
}
Also, hi Dinosawer. :wave:

Re: Random

Posted: Wed Jul 15, 2020 11:01 am
by Dinosawer
Hi :wave:

Well, actually putting the code I gave you in the function at all would probably help :ghost:

Re: Random

Posted: Wed Jul 15, 2020 11:13 am
by Idunno
Dinosawer wrote:
Wed Jul 15, 2020 11:01 am
Hi :wave:

Well, actually putting the code I gave you in the function at all would probably help :ghost:
I did. That was the first thing I tried. It was completely ignored by the compiler.:ghost:

Re: Random

Posted: Wed Jul 15, 2020 11:18 am
by Dinosawer
Then you did it wrong. Show me how you put it in.

Re: Random

Posted: Wed Jul 15, 2020 11:23 am
by Idunno
Dinosawer wrote:
Wed Jul 15, 2020 11:18 am
Then you did it wrong. Show me how you put it in.

Code: Select all

double dsigmoid(double x){
	double e=2.71828;
	double k=0;
	if (x>700){
		return 0;
	}
	double j=pow(e, x);
	double l=j+1;
	double h=pow(l,2);
	k=j/h;
	return k;
}
Here you go. :ghost:

Re: Random

Posted: Wed Jul 15, 2020 12:31 pm
by Dinosawer
That should work, so if it doesn't you weren't running the right code

Re: Random

Posted: Wed Jul 15, 2020 12:40 pm
by Idunno
Dinosawer wrote:
Wed Jul 15, 2020 12:31 pm
That should work, so if it doesn't you weren't running the right code
I just tried it again, and it still isn't working. At this point I'm starting to blame my text editor. :ghost:

Or a curse. :think:

Re: Random

Posted: Wed Jul 15, 2020 7:10 pm
by 0111narwhalz
Idunno wrote:
Wed Jul 15, 2020 12:40 pm

I just tried it again, and it still isn't working. At this point I'm starting to blame my text editor. :ghost:

Or a curse. :think:
Make sure you're saving where you think you're saving, compiling the file(s) you think you're compiling, and running the executable you think you're running.

Re: Random

Posted: Thu Jul 16, 2020 7:52 am
by Idunno
0111narwhalz wrote:
Wed Jul 15, 2020 7:10 pm
Idunno wrote:
Wed Jul 15, 2020 12:40 pm

I just tried it again, and it still isn't working. At this point I'm starting to blame my text editor. :ghost:

Or a curse. :think:
Make sure you're saving where you think you're saving, compiling the file(s) you think you're compiling, and running the executable you think you're running.
I'm using sublime text for speed. ST has a single button for saving, compiling, and running. 'Course, that's irrelevant now, because it's working for some reason. All I needed to do was hammer the compile button 12 times in a row without stopping to actually let it compile. :ghost:

Thanks for the help guys. :thumbup:

Re: Random

Posted: Thu Jul 16, 2020 1:23 pm
by Scytale
Today I learned emacs really is an operating system unto itself

Re: Random

Posted: Thu Jul 16, 2020 7:57 pm
by Idunno
Scytale wrote:
Thu Jul 16, 2020 1:23 pm
Today I learned emacs really is an operating system unto itself
I have heard rumours, but have been unwilling to test them. :shifty:

Re: Random

Posted: Fri Jul 17, 2020 6:54 am
by Scytale
Idunno wrote:
Thu Jul 16, 2020 7:57 pm
Scytale wrote:
Thu Jul 16, 2020 1:23 pm
Today I learned emacs really is an operating system unto itself
I have heard rumours, but have been unwilling to test them. :shifty:
I avoided using any of this capability assiduously for years, but in a matter of months I have been converted.

It started when I realized that I was getting really sick of (a) not having any programs that really worked well with emacs-type keybindings, and (b) also not being able to work efficiently with Python scripts (in particular) which I edited within emacs. In the meantime, because of the need to work remotely, I let someone show me how "cool" Jupyter is, for flexible Python scripting and prototyping, but I couldn't stand it for various reasons, all of which boiled down to it Not Being Emacs.

So I bit the bullet and looked for a way to integrate Jupyter Lab functionality into emacs. I found it and my remote productivity shot up. Not only could I use my beloved keybindings, but the integration also solved some crucial (and really idiotic) flaws that were otherwise present in Jupyter anyway, like not being able to copy-paste from a script to a notebook.

From there my fate was sealed.

I have just integrated LaTeX into emacs and can cycle my formatting like a madman. It's basically WYSIWYG at this point! (Don't talk to me about Overleaf.) It's only a matter of time before I look for C-prototyping, and SWIG C-integration with Python is a no-brainer. I wonder if I can run MacOS from within emacs...

Re: Random

Posted: Fri Jul 17, 2020 7:14 am
by Idunno
Scytale wrote:
Fri Jul 17, 2020 6:54 am
Idunno wrote:
Thu Jul 16, 2020 7:57 pm
Scytale wrote:
Thu Jul 16, 2020 1:23 pm
Today I learned emacs really is an operating system unto itself
I have heard rumours, but have been unwilling to test them. :shifty:
I avoided using any of this capability assiduously for years, but in a matter of months I have been converted.

It started when I realized that I was getting really sick of (a) not having any programs that really worked well with emacs-type keybindings, and (b) also not being able to work efficiently with Python scripts (in particular) which I edited within emacs. In the meantime, because of the need to work remotely, I let someone show me how "cool" Jupyter is, for flexible Python scripting and prototyping, but I couldn't stand it for various reasons, all of which boiled down to it Not Being Emacs.

So I bit the bullet and looked for a way to integrate Jupyter Lab functionality into emacs. I found it and my remote productivity shot up. Not only could I use my beloved keybindings, but the integration also solved some crucial (and really idiotic) flaws that were otherwise present in Jupyter anyway, like not being able to copy-paste from a script to a notebook.

From there my fate was sealed.

I have just integrated LaTeX into emacs and can cycle my formatting like a madman. It's basically WYSIWYG at this point! (Don't talk to me about Overleaf.) It's only a matter of time before I look for C-prototyping, and SWIG C-integration with Python is a no-brainer. I wonder if I can run MacOS from within emacs...
Can you run emacs within emacs? :think: