/** * Train Perceptrons */ public function train_perceptrons() { $color = 0; $bgcolors = array('#efefef', '#fefefe'); fms_open_center(); fms_h2("Single-Layer Perceptrons (SLP): Delta Rule"); fms_open_table(0, 'white', $this->width, 2, 2); fms_open_table_row(); fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "N"); for ($j = 0; $j < $this->matrix->columns($this->x); $j++) { fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "-" . $j . "-<br>Actual"); } fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "Desired<br>Outcome"); for ($j = 0; $j < $this->matrix->columns($this->x); $j++) { fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "-" . $j . "-<br>Weight"); } for ($j = 0; $j < $this->matrix->columns($x); $j++) { fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "-" . $j . "-<br>Class"); } fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "Sum"); fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "Forecast"); fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "Residual"); fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "Weighted<br>Diff"); for ($j = 0; $j < $this->matrix->columns($this->x); $j++) { fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "-" . $j . "-<br>Adjusted<br>Weight"); } fms_close_table_row(); fms_open_table_row(); for ($i = 0; $i < $this->matrix->rows($this->x); $i++) { $sum = 0; fms_alternateColor(); fms_open_table_row(); fms_write_table_col($bgcolors[$color], "right", "", "", $fontColor, $fontSize, number_format($i + 1, 0)); for ($j = 0; $j < $this->matrix->columns($this->x); $j++) { fms_write_table_col($bgcolors[$color], "right", 75, "", $fontColor, $fontSize, number_format($this->x[$i][$j], 0)); } fms_write_table_col($bgcolors[$color], "right", 75, "", $fontColor, $fontSize, number_format($this->y[$i], 0)); for ($j = 0; $j < $this->matrix->columns($this->x); $j++) { fms_write_table_col($bgcolors[$color], "right", 75, "", $fontColor, $fontSize, number_format($this->w[$j], 1)); } for ($j = 0; $j < $this->matrix->columns($this->x); $j++) { fms_write_table_col($bgcolors[$color], "right", 75, "", $fontColor, $fontSize, number_format($this->w[$j] * $this->x[$i][$j], 1)); $sum += $this->w[$j] * $this->x[$i][$j]; } fms_write_table_col($bgcolors[$color], "right", 75, "", $fontColor, $fontSize, number_format($sum, 1)); if ($sum > $this->threshold) { $predict = 1; } else { $predict = 0; } fms_write_table_col($bgcolors[$color], "right", 75, "", $fontColor, $fontSize, $predict); $residual = $this->y[$i] - $predict; fms_write_table_col($bgcolors[$color], "right", 75, "", $fontColor, $fontSize, number_format($residual, 0)); $diff = $residual * $this->eta; fms_write_table_col($bgcolors[$color], "right", 75, "", $fontColor, $fontSize, number_format($diff, 1)); for ($j = 0; $j < $this->matrix->columns($this->x); $j++) { $this->w[$j] = $this->w[$j] + $this->x[$i][$j] * $diff; fms_write_table_col($bgcolors[$color], "right", 75, "", $fontColor, $fontSize, number_format($this->w[$j], 1)); } fms_close_table_row(); } fms_close_table(); fms_close_center(); }
/** * Training neurons (calculate weights) */ public function train_neurons() { if ($this->debug) { $color = 0; $bgcolors = array('#efefef', '#fefefe'); fms_open_center(); fms_h2("Training Neurons with the Delta Rule"); /* Column Headings */ fms_open_table(0, 'white', $this->report_width, 2, 2); fms_open_table_row(); for ($j = 0; $j < $this->matrix->columns($this->x); $j++) { fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "PRICE-" . chr(65 + $j)); } fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "MAX"); fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "ETA"); fms_close_table_row(); fms_open_table_row(); for ($j = 0; $j < $this->matrix->columns($this->x); $j++) { fms_write_table_col($bgcolors[$color], "right", "", "", $fontColor, $fontSize, number_format($this->price[$j], 0)); } fms_write_table_col($bgcolors[$color], "right", "", "", $fontColor, $fontSize, number_format($this->max_items, 0)); fms_write_table_col($bgcolors[$color], "right", "", "", $fontColor, $fontSize, number_format($this->eta, 6)); fms_close_table_row(); fms_close_table(); fms_open_table(0, 'white', $this->report_width, 2); fms_open_table_row(); fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "N"); for ($j = 0; $j < $this->matrix->columns($this->x); $j++) { fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "X({$j})"); } fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "TOTAL"); for ($j = 0; $j < $this->matrix->columns($this->x); $j++) { fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "F({$j})"); } fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "TOTAL"); fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "RESIDUAL"); for ($j = 0; $j < $this->matrix->columns($this->x); $j++) { fms_write_table_col("silver", "center valign=bottom", "", "", $fontColor, $fontSize, "D({$j})"); } fms_close_table_row(); fms_break(); } /* Matrix */ for ($i = 0; $i < $this->matrix->rows($this->x); $i++) { if ($this->debug) { fms_alternateColor(); fms_open_table_row(); fms_write_table_col($bgcolors[$color], "right", "", "", $fontColor, $fontSize, number_format($i + 1, 0)); } for ($j = 0; $j < $this->matrix->columns($this->x); $j++) { if ($this->debug) { fms_write_table_col($bgcolors[$color], "right", "", "", $fontColor, $fontSize, number_format($this->x[$i][$j], 0)); } $x_price[$i] += $this->x[$i][$j] * $this->price[$j]; } if ($this->debug) { fms_write_table_col($bgcolors[$color], "right", "", "", $fontColor, $fontSize, number_format($x_price[$i], 0)); } for ($j = 0; $j < $this->matrix->columns($this->x); $j++) { if ($i > 0) { $this->f[$j] += $this->d[$j]; } if ($this->debug) { fms_write_table_col($bgcolors[$color], "right", "", "", $fontColor, $fontSize, number_format($this->f[$j], 0)); } $f_price[$i] += $this->x[$i][$j] * $this->f[$j]; } $this->residual = $x_price[$i] - $f_price[$i]; if ($this->debug) { fms_write_table_col($bgcolors[$color], "right", "", "", $fontColor, $fontSize, number_format($f_price[$i], 0)); fms_write_table_col($bgcolors[$color], "right", "", "", $fontColor, $fontSize, number_format($this->residual, 0)); } for ($j = 0; $j < $this->matrix->columns($this->x); $j++) { $this->d[$j] = $this->x[$i][$j] * $this->residual * $this->eta; if ($this->debug) { fms_write_table_col($bgcolors[$color], "right", "", "", $fontColor, $fontSize, number_format($this->d[$j], 0)); } } if ($this->debug) { fms_close_table_row(); } } if ($this->debug) { fms_close_table(); fms_close_center(); } }