void RadialBasisFunction::Train(HostMatrix<float> &Input, HostMatrix<float> &Target){ //std::cout << "Training" << std::endl; // c_width = (float*) malloc(sizeof(float)*network_size); // memset(c_width,0,sizeof(float)*network_size); DeviceMatrix<float> device_X(Input); //std::cout << "KMeans" << std::endl; clock_t initialTime = clock(); KMeans KM; KM.SetSeed(seed); dCenters = KM.Execute(device_X,network_size); cudaThreadSynchronize(); times[0] = (clock() - initialTime); //std::cout << "Adjust Widths" << std::endl; /*Adjust width using mean of distance to neighbours*/ initialTime = clock(); AdjustWidths(number_neighbours); cudaThreadSynchronize(); times[1] = (clock() - initialTime); /*Training weights and scaling factor*/ HostMatrix<float> TargetArr(Target.Rows(),NumClasses); memset(TargetArr.Pointer(),0,sizeof(float)*TargetArr.Elements()); for(int i = 0; i < Target.Rows(); i++){ TargetArr(i,((int)Target(i,0)-1)) = 1; } DeviceMatrix<float> d_Target(TargetArr); //std::cout << "Calculating Weights" << std::endl; initialTime = clock(); DeviceMatrix<float> device_activ_matrix(device_X.Rows(),dCenters.Rows(),ColumnMajor); KernelActivationMatrix(device_activ_matrix.Pointer(),device_X.Pointer(),dCenters.Pointer(),device_X.Columns(),dCenters.Columns(),device_activ_matrix.Columns(),device_activ_matrix.Rows(),scaling_factor,device_c_width.Pointer()); DeviceMatrix<float> d_Aplus = UTILS::pseudoinverse(device_activ_matrix); dWeights = DeviceMatrix<float>(d_Aplus.Rows(),d_Target.Columns()); d_Aplus.Multiply(d_Aplus,d_Target,dWeights); /*Return Weights and Centers*/ cudaThreadSynchronize(); times[2] = (clock() - initialTime); // cudaMemcpy(c_width,device_c_width.Pointer(),sizeof(float)*device_c_width.Length(),cudaMemcpyDeviceToHost); // this->Weights = HostMatrix<float>(dWeights); // this->Centers = HostMatrix<float>(dCenters); }
/*Calculate the root mean square error of outputs against targets*/ double rmse_error(HostMatrix<float> &Target,HostMatrix<float> &Output){ //check global error float sum = 0; int i; for(i = 0; i < Target.Rows(); i++){ sum = sum + pow(Target(i,0) - Output(i,0),2); } return sqrt(sum/Target.Rows()); }
void writeHeader(HostMatrix<float> &Input, int number_classes, int seed){ cout << "\n=== Run information ===\n\n"; cout << std::left << setw(50) << "Random Seed" << std::left << setw(20) << seed << endl; cout << std::left << setw(50) << "Number of Basis Functions" << std::left << setw(20) << NETWORK_SIZE << endl; cout << std::left << setw(50) << "Number of Neighbours for Width Estimation" << std::left << setw(20) << RNEIGHBOURS << endl; cout << std::left << setw(50) << "Number of Folds" << std::left << setw(20) << KFOLDS << endl; cout << std::left << setw(50) << "Number of Attributes" << std::left << setw(20) << Input.Columns() << endl; cout << std::left << setw(50) << "Number of Classes" << std::left << setw(20) << number_classes << endl; cout << std::left << setw(50) << "Number of Instances" << std::left << setw(20) << Input.Rows() << endl; }
/*Count number of miscalculated outputs against targets*/ int error_calc(HostMatrix<float> &Target,HostMatrix<float> &Output){ //check global error int i; int error = 0; for(i = 0; i < Target.Rows(); i++){ if(Target(i,0) - Output(i,0) != 0){ error += 1; } } return error; }