Ir para conteúdo
Fórum Script Brasil

cezarvaz

Membros
  • Total de itens

    5
  • Registro em

  • Última visita

Sobre cezarvaz

cezarvaz's Achievements

0

Reputação

  1. Tenta isso!!! #include <stdio.h> #include <iostream> #include <cstdlib> using namespace std; int main (){ int v_matriz[0][9]; int v_soma=0; float num; for(int i=0;i<10;i++){ cout<<("Digite um numero:"); cin >> num; v_matriz[0]=num; v_soma = v_soma+(v_matriz[0]); } cout<<"\n"; cout<<"Resultado: "<<v_soma; }
  2. Olá pessoal, estou aqui novamente para destar outro nó. è o seguinte, em java para gerar um numero randomico necessito dos seguintes comandos em meu trabalho alguém saberia como faço em C++ as linhas em negrito? Desde já agradeço, pois não sei o equivalente deste código em C++. Abração a todos!!! public static void randomize(){ //Cria uma instância de Random Random random = new Random(getSemente()); for (int j=0; j<getNumHidden(); j++) { setBias1(j,-1+random.nextInt(8192)/8192); setAntigodeltabs1(j, 0.0d); setDeltabs1(j, 0.0d); for (int i=0; i<getNumInputs(); i++) { setPesos1(j, i, random.nextInt(8192)/8192-0.5d); setAntigodeltapesos1(j, i, 0.0d); setDeltapesos1(j, i, 0.0d); } } for (int j=0; j<getNumOutputs(); j++) { setBias2(j,-0.1d + random.nextInt(8192)/8192); setAntigodeltabs2(j, 0.0d); for (int i=0;i<getNumHidden(); i++) { setPesos2(j, i, 0.1d * random.nextInt(8192)/8192- 0.05); setAntigodeltapesos2(j, i, 0.0d); setDeltapesos2(j, i, 0.0d); } } }
  3. Baita explicação!!!!! eu não tinha este entendimento muito concreto, valeu pela maneira simples e direta de explicar! Neste meu trabalho, estou traduzindo um código de Java para C++ e estou engatinhando em POO Fiz alterações no código e agora me resta passar as matrizes. O conhecimento para ler txt com os dados necessários como matrizes e substituir o comando abaixo eu sei, basta eu saber como passar a matriz para a classe preceptron, pois os cálculos estão prontos, com isso eu aprendo POO e IA também. Quando eu conseguir este passo em POO poderei fazer outras coisas também, se puderes me aturar mais um pouco agradeço a ajuda!!! double entradasIniciais [4][2] = {{0,0},{0,1},{1,0},{1,1}}; entradas = entradasIniciais; p1.set_Entradas(4,2); double saidasIniciais [4][1] = {{0},{0},{0},{1}}; p1.set_Saidas(4,1); saidas = saidasIniciais; Segue o novo código ;-) #include <iostream> #include <iomanip> #include <stdlib.h> #include <time.h> using namespace std; class IArt{ // Classe base - Inteligencia Artificial. private: long semente; double beta, alfa; int numTraining, ciclos,cicloatual,numOutputs, numInputs,linha,coluna; double *bias1; double *net1; double *fnet1; double *deltabs1; double **pesos1; double **deltapesos1; double **erro1; double **erro2; public: double **entradas; double **saidas; void set_Semente(long num) { semente = num; } long get_Semente() { return semente; } void set_Beta(double num) { beta = num; } double get_Beta() { return beta; } void set_Alfa(double num) { alfa = num; } double get_Alfa() { return alfa; } void set_NumTraining(int num) { numTraining = num; } int get_NumTraining() { return numTraining; } void set_Ciclos(int num) { ciclos = num; } int get_Ciclos() { return ciclos; } void set_CicloAtual(int num) { cicloatual = num; } int get_CicloAtual() { return cicloatual; } void set_NumOutputs(int num) { numOutputs = num; bias1 = new double[numOutputs]; net1 = new double[numOutputs]; fnet1 = new double[numOutputs]; deltabs1 = new double[numOutputs]; } int get_NumOutputs() { return numOutputs; } void set_NumInputs(int num) { numInputs = num; } int get_NumInputs() { return numInputs; } void set_Bias1(int index, double value) { bias1[index]=value; } int get_Bias1(int index) { return bias1[index]; } void set_Net1(int index, double value) { net1[index]=value; } int get_Net1(int index) { return net1[index]; } void set_Fnet1(int index, double value) { fnet1[index]=value; } int get_Fnet1(int index) { return fnet1[index]; } void set_Deltabs1(int index, double value) { deltabs1[index]=value; } int get_Deltabs1(int index) { return deltabs1[index]; } void set_Pesos1(int row, int col, double value) { pesos1[row][col] = value; } double get_Pesos1(int row, int col) { return pesos1[row][col]; } void init_Pesos1(int row, int col) { pesos1 = new double*[row]; for(int i = 0; i < row; i++) pesos1 = new double[col]; } void set_DeltaPesos1(int row, int col, double value) { deltapesos1[row][col] = value; } double get_DeltaPesos1(int row, int col) { return deltapesos1[row][col]; } void init_DeltaPesos1(int row, int col) { deltapesos1 = new double*[row]; for(int i = 0; i < row; i++) deltapesos1 = new double[col]; } void set_Erro1(int row, int col, double value) { erro1[row][col] = value; } double get_Erro1(int row, int col) { return erro1[row][col]; } void init_Erro1(int row, int col) { erro1 = new double*[row]; for(int i = 0; i < row; i++) erro1 = new double[col]; } void set_Erro2(int row, int col, double value) { erro2[row][col] = value; } double get_Erro2(int row, int col) { return erro2[row][col]; } void init_Erro2(int row, int col) { erro2 = new double*[row]; for(int i = 0; i < row; i++) erro2 = new double[col]; } void set_Entradas(int row, int col) { entradas = new double*[row]; for(int i = 0; i < row; i++){ entradas = new double[col]; } } double get_Entradas(int row, int col) { return entradas[row][col]; } void set_Saidas(int row, int col) { saidas = new double*[row]; for(int i = 0; i < row; i++) saidas = new double[col]; } double get_Saidas(int row, int col) { return saidas[row][col]; } }; class Preceptron : public IArt { // Classe Preceptron. public: void mostrar(); void randomize(); void Forward(); }; void Preceptron::randomize(){ srand(time(NULL)); int Random = rand() % get_Semente(); cout<<Random<<"\n"; for (int j=0; j<get_NumOutputs(); j++){ set_Bias1(j,0.0); set_Deltabs1(j, 0.0d); for (int i=0; i<get_NumInputs(); i++){ set_Pesos1(j,i,3.0); set_DeltaPesos1(j,i,0.0d); } } } void Preceptron::Forward(){ for(int kl=0; kl< get_Ciclos(); kl++){ set_CicloAtual(get_CicloAtual()+ 1); for(int itr=0; itr<get_NumTraining(); itr++) { double ea,eb; for (int j=0;j<get_NumOutputs();j++) { set_Net1(j, get_Bias1(j)); for(int i=0;i<get_NumInputs();i++){ set_Net1(j,get_Net1(j)+ (get_Pesos1(j, i)* get_Entradas(itr,i))); } if (get_Net1(j)>= 0.) ea=1.; else ea=0.; set_Fnet1(j,(double) (ea)); } //Ajuste de pesos BackWard for(int j=0;j<get_NumOutputs();j++) { set_Erro2(itr,j, (get_Saidas(itr,j) - get_Fnet1(j))); cout<<"Ciclo:"<<"\t"<<get_CicloAtual()<<"\t"<<"Exemplo:"<<"\t"<<itr+1<<"\n"; cout<<"Saída desejada:"<<"\t"<<get_Saidas(itr,j)<<"\t"<<"Saída calculada:"<<"\t"<<get_Fnet1(j)<<"\n"; cout<<"Erro:"<<"\t"<<get_Erro2(itr,j)<<"\n"; } for(int j=0;j<get_NumOutputs();j++) { set_Deltabs1(j,( get_Alfa() * get_Erro2(itr,j))); for(int ii=0;ii<get_NumInputs();ii++) { set_DeltaPesos1(j,ii, (get_Alfa() * get_Erro2(itr,j))*(get_Entradas(itr,ii))); } } for(int j=0;j<get_NumOutputs();j++) { set_Bias1(j, get_Deltabs1(j) + get_Bias1(j)); cout<<"bias:"<<"\t"<< j<<"\t"<< get_Bias1(j)<<"\n"; for(int ii=0;ii<get_NumInputs();ii++){ set_Pesos1(j,ii, get_Pesos1(j,ii)+(get_DeltaPesos1(j,ii))); cout<<"Peso:"<<"\t"<<j<<"\t"<<ii<<"\t"<<get_Pesos1(j,ii)<<"\n"; } } } } } void Preceptron::mostrar(){ cout << "semente: " << get_Semente() << "\n"; cout << "beta: " << get_Beta() << "\n"; cout << "alfa: " << get_Alfa() << "\n"; cout << "numTraining: " << get_NumTraining() << "\n"; cout << "ciclos: " << get_Ciclos() << "\n"; cout << "ciclo atual : " << get_CicloAtual() << "\n"; cout << "numOutputs : " << get_NumOutputs() << "\n"; cout << "numInputs : " << get_NumInputs() << "\n"; } int main() { Preceptron p1, p2; p1.set_Semente(13); p1.set_Beta(0.0); p1.set_Alfa(1.0); p1.set_NumTraining(4); p1.set_Ciclos(2); p1.set_CicloAtual(0); p1.set_NumOutputs(1); p1.set_NumInputs(2); p1.init_Pesos1(p1.get_NumOutputs(),p1.get_NumInputs()); p1.init_DeltaPesos1(p1.get_NumOutputs(),p1.get_NumInputs()); p1.init_Erro1(p1.get_NumTraining(),p1.get_NumOutputs()); p1.init_Erro2(p1.get_NumTraining(),p1.get_NumOutputs()); p1.randomize(); double entradasIniciais [4][2] = {{0,0},{0,1},{1,0},{1,1}}; entradas = entradasIniciais; p1.set_Entradas(4,2); double saidasIniciais [4][1] = {{0},{0},{0},{1}}; p1.set_Saidas(4,1); saidas = saidasIniciais; p1.mostrar(); cout << "\n"; return 0; }
  4. Valeu pela excelente resposta, mas pra variar ainda tem duas dúvidas na finalização. 1.O código tem agora um problema de passar as matrizes entrada e saida que não consigo resolver, 2.Nunca utilizei o destrutor, talvez porque meus códigos até então eram pequenos, qual seria a localização dele no código? Agradeço a gentileza, vou ajudar os outros integrantes deste forum com alguma coisa que eu saiba responder. Abração a todos... #include <iostream> #include <iomanip> #include <stdlib.h> #include <time.h> using namespace std; class IArt{ // Classe base - Inteligencia Artificial. private: long semente; double beta, alfa; int numTraining, ciclos,cicloatual,numOutputs, numInputs,linha,coluna; double *bias1; double *net1; double *fnet1; double *deltabs1; double **pesos1; double **deltapesos1; double **erro1; double **erro2; double **entradas; double **saidas; public: void set_Semente(long num) { semente = num; } long get_Semente() { return semente; } void set_Beta(double num) { beta = num; } double get_Beta() { return beta; } void set_Alfa(double num) { alfa = num; } double get_Alfa() { return alfa; } void set_NumTraining(int num) { numTraining = num; } int get_NumTraining() { return numTraining; } void set_Ciclos(int num) { ciclos = num; } int get_Ciclos() { return ciclos; } void set_CicloAtual(int num) { cicloatual = num; } int get_CicloAtual() { return cicloatual; } void set_NumOutputs(int num) { numOutputs = num; bias1 = new double[numOutputs]; net1 = new double[numOutputs]; fnet1 = new double[numOutputs]; deltabs1 = new double[numOutputs]; } int get_NumOutputs() { return numOutputs; } void set_NumInputs(int num) { numInputs = num; } int get_NumInputs() { return numInputs; } void set_Bias1(int index, double value) { bias1[index]=value; } int get_Bias1(int index) { return bias1[index]; } void set_Net1(int index, double value) { net1[index]=value; } int get_Net1(int index) { return net1[index]; } void set_Fnet1(int index, double value) { fnet1[index]=value; } int get_Fnet1(int index) { return fnet1[index]; } void set_Deltabs1(int index, double value) { deltabs1[index]=value; } int get_Deltabs1(int index) { return deltabs1[index]; } void set_Pesos1(int row, int col, double value) { pesos1[row][col] = value; } double get_Pesos1(int row, int col) { return pesos1[row][col]; } void init_Pesos1(int row, int col) { pesos1 = new double*[row]; for(int i = 0; i < row; i++) pesos1 = new double[col]; } void set_DeltaPesos1(int row, int col, double value) { deltapesos1[row][col] = value; } double get_DeltaPesos1(int row, int col) { return deltapesos1[row][col]; } void init_DeltaPesos1(int row, int col) { deltapesos1 = new double*[row]; for(int i = 0; i < row; i++) deltapesos1 = new double[col]; } void set_Erro1(int row, int col, double value) { erro1[row][col] = value; } double get_Erro1(int row, int col) { return erro1[row][col]; } void init_Erro1(int row, int col) { erro1 = new double*[row]; for(int i = 0; i < row; i++) erro1 = new double[col]; } void set_Erro2(int row, int col, double value) { erro2[row][col] = value; } double get_Erro2(int row, int col) { return erro2[row][col]; } void init_Erro2(int row, int col) { erro2 = new double*[row]; for(int i = 0; i < row; i++) erro2 = new double[col]; } double get_Entradas(int row, int col) { return entradas[row][col]; } double get_Saidas(int row, int col) { return saidas[row][col]; } }; class Preceptron : public IArt { // Classe Preceptron. public: void mostrar(); void randomize(); void Forward(); }; void Preceptron::randomize(){ srand(time(NULL)); int Random = rand() % get_Semente(); cout<<Random<<"\n"; for (int j=0; j<get_NumOutputs(); j++){ set_Bias1(j,0.0); set_Deltabs1(j, 0.0d); for (int i=0; i<get_NumInputs(); i++){ set_Pesos1(j,i,3.0); set_DeltaPesos1(j,i,0.0d); } } } void Preceptron::Forward(){ for(int kl=0; kl< get_Ciclos(); kl++){ set_CicloAtual(get_CicloAtual()+ 1); for(int itr=0; itr<get_NumTraining(); itr++) { double ea,eb; for (int j=0;j<get_NumOutputs();j++) { set_Net1(j, get_Bias1(j)); for(int i=0;i<get_NumInputs();i++){ set_Net1(j,get_Net1(j)+ (get_Pesos1(j, i)* get_Entradas(itr,i))); } if (get_Net1(j)>= 0.) ea=1.; else ea=0.; set_Fnet1(j,(double) (ea)); } //Ajuste de pesos BackWard for(int j=0;j<get_NumOutputs();j++) { set_Erro2(itr,j, (get_Saidas(itr,j) - get_Fnet1(j))); cout<<"Ciclo:"<<"\t"<<get_CicloAtual()<<"\t"<<"Exemplo:"<<"\t"<<itr+1<<"\n"; cout<<"Saída desejada:"<<"\t"<<get_Saidas(itr,j)<<"\t"<<"Saída calculada:"<<"\t"<<get_Fnet1(j)<<"\n"; cout<<"Erro:"<<"\t"<<get_Erro2(itr,j)<<"\n"; } for(int j=0;j<get_NumOutputs();j++) { set_Deltabs1(j,( get_Alfa() * get_Erro2(itr,j))); for(int ii=0;ii<get_NumInputs();ii++) { set_DeltaPesos1(j,ii, (get_Alfa() * get_Erro2(itr,j))*(get_Entradas(itr,ii))); } } for(int j=0;j<get_NumOutputs();j++) { set_Bias1(j, get_Deltabs1(j) + get_Bias1(j)); cout<<"bias:"<<"\t"<< j<<"\t"<< get_Bias1(j)<<"\n"; for(int ii=0;ii<get_NumInputs();ii++){ set_Pesos1(j,ii, get_Pesos1(j,ii)+(get_DeltaPesos1(j,ii))); cout<<"Peso:"<<"\t"<<j<<"\t"<<ii<<"\t"<<get_Pesos1(j,ii)<<"\n"; } } } } } void Preceptron::mostrar(){ cout << "semente: " << get_Semente() << "\n"; cout << "beta: " << get_Beta() << "\n"; cout << "alfa: " << get_Alfa() << "\n"; cout << "numTraining: " << get_NumTraining() << "\n"; cout << "ciclos: " << get_Ciclos() << "\n"; cout << "ciclo atual : " << get_CicloAtual() << "\n"; cout << "numOutputs : " << get_NumOutputs() << "\n"; cout << "numInputs : " << get_NumInputs() << "\n"; } int main() { Preceptron p1, p2; p1.set_Semente(13); p1.set_Beta(0.0); p1.set_Alfa(1.0); p1.set_NumTraining(4); p1.set_Ciclos(2); p1.set_CicloAtual(0); p1.set_NumOutputs(1); p1.set_NumInputs(2); p1.init_Pesos1(p1.get_NumOutputs(),p1.get_NumInputs()); p1.init_DeltaPesos1(p1.get_NumOutputs(),p1.get_NumInputs()); p1.init_Erro1(p1.get_NumTraining(),p1.get_NumOutputs()); p1.init_Erro2(p1.get_NumTraining(),p1.get_NumOutputs()); p1.randomize(); double entradasIniciais [4][2] = {{0,0},{0,1},{1,0},{1,1}}; entradas = entradasIniciais; p1.get_Entradas(entradas); double saidasIniciais [4][1] = {{0},{0},{0},{1}}; saidas = saidasIniciais; p1.mostrar(); cout << "\n"; return 0; }
  5. Oi pessoal! Boa tarde a todos estou implementando IA com classes de tipos de redes neurais e deu um erro na linha 59 e não sei aonde eu errei. Se alguém puder me ajudar, PLZ!!!! #include <iostream> #include <iomanip> using namespace std; class IArt{ // Classe base - Inteligencia Artificial. private: long semente; double beta, alfa; int numTraining, ciclos,cicloatual,numOutputs, numInputs; double* bias1; double* net1; double* fnet1; double* deltabs1; double** pesos1; double** deltapesos1; public: void set_Semente(long num) { semente = num; } long get_Semente() { return semente; } void set_Beta(double num) { beta = num; } double get_Beta() { return beta; } void set_Alfa(double num) { alfa = num; } double get_Alfa() { return alfa; } void set_NumTraining(int num) { numTraining = num; } int get_NumTraining() { return numTraining; } void set_Ciclos(int num) { ciclos = num; } int get_Ciclos() { return ciclos; } void set_CicloAtual(int num) { cicloatual = num; } int get_CicloAtual() { return cicloatual; } void set_NumOutputs(int num) { numOutputs = num; bias1 = new double[numOutputs]; net1 = new double[numOutputs]; fnet1 = new double[numOutputs]; deltabs1 = new double[numOutputs]; } int get_NumOutputs() { return numOutputs; } void set_NumInputs(int num) { numInputs = num; } int get_NumInputs() { return numInputs; } void set_Bias1(int index, double value) { bias1[index]=value; } int get_Bias1(int index) { return bias1[index]; } void set_Net1(int index, double value) { net1[index]=value; } int get_Net1(int index) { return net1[index]; } void set_Fnet1(int index, double value) { fnet1[index]=value; } int get_Fnet1(int index) { return fnet1[index]; } void set_Deltabs1(int index, double value) { deltabs1[index]=value; } int get_Deltabs1(int index) { return deltabs1[index]; } void set_Pesos1(int row, int col, double value) { pesos1[row][col] = value; } double get_Pesos1(int row, int col) { return pesos1[row][col]; } void Init_Pesos1(int row, int col) { pesos1 = new double[row][col]; } <<<<<<<<<<<<<<<<Aqui! }; class Preceptron : public IArt { // Define classe Preceptron. public: void mostrar(); }; void Preceptron::mostrar(){ cout << "semente: " << get_Semente() << "\n"; cout << "beta: " << get_Beta() << "\n"; cout << "alfa: " << get_Alfa() << "\n"; cout << "numTraining: " << get_NumTraining() << "\n"; cout << "ciclos: " << get_Ciclos() << "\n"; cout << "ciclo atual : " << get_CicloAtual() << "\n"; cout << "numOutputs : " << get_NumOutputs() << "\n"; cout << "numInputs : " << get_NumInputs() << "\n"; } int main() { Preceptron p1, p2; p1.set_Semente(13); p1.set_Beta(0.0); p1.set_Alfa(1.0); p1.set_NumTraining(4); p1.set_Ciclos(2); p1.set_CicloAtual(0); p1.set_NumOutputs(1); p1.set_NumInputs(2); p1.Init_Pesos1(p1.get_NumOutputs(),p1.get_NumInputs()); p1.mostrar(); cout << "\n"; return 0; }
×
×
  • Criar Novo...