00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 # include <qstring.h>
00014 # include <qlineedit.h>
00015 # include <qslider.h>
00016 # include <qfile.h>
00017 # include <qtextstream.h>
00018 # include <qcanvas.h>
00019 # include <qfiledialog.h>
00020 # include <qstatusbar.h>
00021 # include <qtextedit.h>
00022 # include <qtimer.h>
00023 # include <qdatetime.h>
00024 # include <qwt_plot.h>
00025
00026 # include <inn.h>
00027 # include <learningThread.h>
00028 # include <network.h>
00029
00030 extern Inn * inn ;
00031 extern Network * network;
00032
00033 QwtPlot *sigPlot, *errPlot;
00034 long sig, sigDer;
00035 double sigX[120];
00036 double sigY[120];
00037 double sigDerY[120];
00038
00039 QTimer *timer;
00040 QTime *elapsed;
00041 LearningThread lt;
00042
00043 void LearningDialog::init() {
00044 network->setEPS(4);
00045 network->setExampleOrder(SEQUENTIAL);
00046
00047 sigPlot = new QwtPlot("Sigmoid");
00048 sigPlot->setFixedSize(400, 300);
00049 sigPlot->setTitle("Sigmoid and Derived");
00050 sigPlot->enableGridX();
00051 sigPlot->enableGridY();
00052 sigPlot->setAxisTitle(sigPlot->gridXAxis(), "X");
00053 sigPlot->setAxisTitle(sigPlot->gridYAxis(), "Y");
00054 sigPlot->setCanvasBackground(QColor(255,255,255));
00055 sigPlot->setLegendFrameStyle(QFrame::PopupPanel | QFrame::Plain);
00056
00057 errPlot = new QwtPlot("Error Evolution");
00058 errPlot->setFixedSize(400, 300);
00059 errPlot->setTitle("Error Evolution");
00060 errPlot->enableGridX();
00061 errPlot->enableGridY();
00062 errPlot->setAxisTitle(errPlot->gridXAxis(), "Epoch");
00063 errPlot->setAxisTitle(errPlot->gridYAxis(), "Average Error");
00064 errPlot->setCanvasBackground(QColor(255,255,255));
00065 errPlot->setLegendFrameStyle(QFrame::PopupPanel | QFrame::Plain);
00066
00067 lineEdit5->setText(QString::number(network->getK(), 'f', 3)) ;
00068 lineEdit8->setText(QString::number(network->getEPS()));
00069 slider1->setValue((int)(network->getK()*1000));
00070
00071 timer = new QTimer(this);
00072 connect(timer, SIGNAL(timeout()), this , SLOT(timerEvent()));
00073 }
00074
00075
00076 void LearningDialog::slotSigmoidChanged( void ) {
00077 network->setK(((double)slider1->value())/1000) ;
00078 lineEdit5->setText(QString::number(network->getK(), 'f', 3)) ;
00079 slotShowSigmoid();
00080 }
00081
00082
00083 void LearningDialog::slotBrowse( void ) {
00084 network->clearErrorEvolution();
00085
00086 QString filename = QFileDialog::getOpenFileName(
00087 ".",
00088 "CSV file (*.csv)",
00089 this,
00090 "open file a teaching file",
00091 "Choose a file to open" ) ;
00092 lineEdit1->setText(filename);
00093 }
00094
00095
00096 void LearningDialog::slotStartLearning( void ) {
00097 if (!lt.running()) {
00098 if ( (lineEdit1->displayText() == "") || (lineEdit2->displayText() == "")
00099 || (lineEdit3->displayText() == "" ) || (lineEdit4->displayText() == "" ) ) {
00100 QMessageBox::warning(0, "Warning", "Please fill in all the fields");
00101 } else {
00102 inn->console->append("learning started ...") ;
00103 inn->statusBar()->message( "learning started ...", 2000 );
00104 lt.init(lineEdit1->text(), lineEdit3->text().toDouble(), lineEdit2->text().toInt(), lineEdit4->text().toDouble());
00105 lt.start();
00106 lt.setState(PLAY);
00107 elapsed = new QTime();
00108 elapsed->start() ;
00109 timer->start(10);
00110 }
00111 } else if (lt.getState() == PAUSE) {
00112 inn->console->append("learning restarted ...") ;
00113 inn->statusBar()->message( "learning restarted ...", 2000 );
00114 pauseButton->setOn(false);
00115 elapsed = new QTime();
00116 elapsed->start();
00117 lt.setState(PLAY);
00118 }
00119 }
00120
00121
00122 void LearningDialog::slotShowSigmoid( void ) {
00123 double valX = -6.0;
00124
00125 for (int i=0; i<120; i++) {
00126 sigX[i] = valX;
00127 sigY[i] = 1 / (1 + exp(-1 * network->getK() * valX));
00128 sigDerY[i] = sigY[i] * (1 - sigY[i]);
00129 valX += 0.1;
00130 }
00131
00132 sigPlot->clear();
00133 sig = sigPlot->insertCurve("Sigmoid");
00134 sigDer = sigPlot->insertCurve("Sigmoid Derived");
00135 sigPlot->setCurveStyle(sig, QwtCurve::Spline);
00136 sigPlot->setCurveStyle(sigDer, QwtCurve::Spline);
00137 sigPlot->enableLegend(true, sig);
00138 sigPlot->enableLegend(true, sigDer);
00139 sigPlot->setCurvePen(sig, QPen(QColor(0,0,255), 1));
00140 sigPlot->setCurvePen(sigDer, QPen(QColor(0,255,0), 1));
00141 sigPlot->setCurveData(sig, sigX, sigY, 120);
00142 sigPlot->setCurveData(sigDer, sigX, sigDerY, 120);
00143 sigPlot->replot();
00144 sigPlot->show() ;
00145 }
00146
00147
00148 void LearningDialog::slotShowError( void ) {
00149 int err;
00150 vector<double> *error = network->getErrorEvolution();
00151 vector<double>::iterator iter = error->begin();
00152 double *x = new double[error->size()];
00153 double *y = new double[error->size()];
00154 for (unsigned int i=0; i<error->size(); i++) {
00155 x[i] = i;
00156 y[i] = (*iter);
00157 iter++;
00158 }
00159
00160 errPlot->clear();
00161 err = errPlot->insertCurve("Error Evolution");
00162 errPlot->setCurveData(err, x, y, error->size());
00163 errPlot->setCurveStyle(err, QwtCurve::Spline);
00164 errPlot->setCurvePen(err, QPen(QColor(0,0,255), 1));
00165 errPlot->enableLegend(true, err);
00166
00167 errPlot->replot();
00168 errPlot->show();
00169 }
00170
00171
00172 void LearningDialog::timerEvent() {
00173 QString s, s1;
00174 s.setNum(network->getCurrentEpoch() - 1);
00175 lineEdit6->setText(s) ;
00176 s1.setNum(network->getCurrentAverageError());
00177 lineEdit7->setText(s1);
00178
00179 if (elapsed != NULL) {
00180 QTime t ;
00181 t.setHMS(
00182 (elapsed->elapsed()/(60*60*1000))%60,
00183 (elapsed->elapsed()/(60*1000))%60,
00184 (elapsed->elapsed()/(1000))%60,
00185 elapsed->elapsed()%(1000)) ;
00186 lineEdit9->setText(t.toString("hh:mm:ss.zzz")) ;
00187 }
00188
00189 if (!lt.running()) stopSlot();
00190 }
00191
00192
00193 void LearningDialog::LearningDialog_destroyed( QObject * ) {
00194
00195 }
00196
00197
00198 void LearningDialog::slotEpochsPerSecondChanged( void ) {
00199 network->setEPS(slider2->value()) ;
00200 lineEdit8->setText(QString::number(slider2->value())) ;
00201 }
00202
00203
00204 void LearningDialog::pauseSlot() {
00205 if (lt.running()) {
00206 if (pauseButton->isOn()) {
00207 inn->console->append("learning paused ...") ;
00208 inn->statusBar()->message( "learning paused ...", 2000 );
00209 delete elapsed;
00210 elapsed = NULL;
00211 lt.setState(PAUSE);
00212 } else {
00213 inn->console->append("learning restarted ...") ;
00214 inn->statusBar()->message( "learning restarted ...", 2000 );
00215 elapsed = new QTime();
00216 elapsed->start();
00217 lt.setState(PLAY);
00218 }
00219 } else {
00220 pauseButton->setOn(false);
00221 inn->statusBar()->message( "you cannot pause a thing that has not yet been started ...", 2000 );
00222 }
00223 }
00224
00225
00226 void LearningDialog::stopSlot() {
00227 if (lt.running()) {
00228 inn->console->append("learning stopped ...") ;
00229 inn->statusBar()->message( "learning stopped ...", 2000 );
00230 lt.setState(PAUSE);
00231 lt.terminate();
00232 timer->stop() ;
00233 delete elapsed;
00234 elapsed = NULL;
00235 pauseButton->setOn(false);
00236 } else {
00237 timer->stop();
00238 delete elapsed;
00239 elapsed = NULL;
00240 if (network->getCurrentEpoch() < lineEdit2->text().toInt()
00241 && network->getCurrentAverageError() > lineEdit3->text().toDouble())
00242 inn->statusBar()->message( "you cannot stop a thing that has not yet been started ...", 2000 );
00243 }
00244 }
00245
00246
00247 void LearningDialog::stepSlot() {
00248 if (lt.running()) {
00249 if (lt.getState() != PAUSE) {
00250 inn->statusBar()->message( "you must first pause the learning ...", 2000 );
00251 } else {
00252 inn->statusBar()->message( "next step ...", 2000 );
00253 lt.nextStep();
00254 }
00255 } else {
00256 if ( (lineEdit1->displayText() == "") || (lineEdit2->displayText() == "")
00257 || (lineEdit3->displayText() == "" ) || (lineEdit4->displayText() == "" ) ) {
00258 QMessageBox::warning(0, "Warning", "Please fill in all the fields");
00259 } else {
00260 inn->console->append("learning started in step by step mode...") ;
00261 inn->statusBar()->message( "learning started in step by step mode...", 2000 );
00262 pauseButton->setOn(true);
00263 lt.init(lineEdit1->text(), lineEdit3->text().toDouble(), lineEdit2->text().toInt(), lineEdit4->text().toDouble());
00264 lt.setState(PAUSE);
00265 lt.start();
00266 elapsed = NULL;
00267 timer->start(10);
00268 }
00269 }
00270 }
00271
00272
00273 void LearningDialog::sequentialOrder() {
00274 network->setExampleOrder(SEQUENTIAL);
00275 }
00276
00277
00278 void LearningDialog::randomWithOrder() {
00279 network->setExampleOrder(RANDOM_WITH);
00280 }
00281
00282
00283 void LearningDialog::randomWithoutOrder() {
00284 network->setExampleOrder(RANDOM_WITHOUT);
00285 }