def updateResult(self): inputColumn0 = self.tableWidget.cellWidget(0, 4).value() # number of legs inputColumn1 = self.tableWidget.cellWidget(1, 4).value() # lives in water inputColumn2 = self.tableWidget.cellWidget(2, 4).value() # can fly inputColumn3 = self.tableWidget.cellWidget(3, 4).value() # has feathers inputColumn4 = self.tableWidget.cellWidget(4, 4).value() # egg-laying outputVector = self.linearNetwork.response([inputColumn0, inputColumn1, inputColumn2, inputColumn3, inputColumn4]) if self.checkBoxWinner.checkState() == Qt.Checked: winner = LinearNetwork.winner(outputVector, self.spinBoxThreshold.value()) for i in xrange(len(outputVector)): self.tableWidget.setCellWidget(5, i + 1, QLabel()) if winner != -1: cellWinner = self.tableWidget.cellWidget(5, winner + 1) cellWinner.setStyleSheet('background-color: red;') self.labelNeuron.setText('Neuron ' + str(winner + 1)) self.labelType.setText('This is a ' + self.classNames[winner]) else: self.labelNeuron.setText('(There is no winner.)') self.labelType.setText('Thiss is something strange!') for i in xrange(len(outputVector)): self.tableWidget.cellWidget(5, i + 1).setText(str(outputVector[i]))
def __init__(self, parent=None): super(Widget, self).__init__(parent) buttonHelp1 = QPushButton('help') buttonHelp1.setEnabled(False) buttonHelp1.setToolTip('The table below, with the exception of the last row and column, describe the network\n' + 'in terms of the weight coefficient values. The value of a cell in the n-th row and m-th\n' + 'column is the weight of the n-th feature for the m-th animal class.\n\n' + 'As you can see, each animal class has its own neuron that recognizes it, and each of\n' + 'these neurons has inputs that receive individual feature values.\n\n' + 'The last column represents the input vector, and the last row contains the network\n' + 'output values.') buttonHelp2 = QPushButton('help') buttonHelp2.setEnabled(False) buttonHelp2.setToolTip('If you show the winner, you will see the neuron whose response was the strongest.\n' + 'This neuron indicates the animal class the examined object will most probably fall into.\n' + 'The table below, with the exception of the last row and column, describe the network\n' + 'in terms of the weight coefficient values. The value of a cell in the n-th row and m-th\n' + 'column is the weight of the n-th feature for the m-th animal class.\n\n' + 'As you can see, each animal class has its own neuron that recognizes it, and each of\n' + 'these neurons has inputs that receive individual feature values.\n\n' + 'The last column represents the input vector, and the last row contains the network\n' + 'output values.') hBoxLayout1 = QHBoxLayout() hBoxLayout1.addWidget(QLabel('The neural network in this example uses five features to recognize\n' + 'three classes of animals. Its weight coefficients are predefined and shown in the table below.\n\n' + 'To test the network behavior, enter the input signals in the rightmost column and read the output\n' + 'values from the bottom row.')) hBoxLayout1.addWidget(buttonHelp1) self.checkBoxWinner = QCheckBox() self.checkBoxWinner.setText('Show the winner') hBoxLayout2 = QHBoxLayout() hBoxLayout2.addWidget(self.checkBoxWinner) hBoxLayout2.addWidget(buttonHelp2) self.tableWidget = QTableWidget() self.labelNeuron = QLabel('(There is no winner.)') self.labelType = QLabel('This is something strange!') self.spinBoxThreshold = QDoubleSpinBox() self.spinBoxThreshold.setValue(5.0) self.groupBox = QGroupBox('Show the winner') gridLayout = QGridLayout() gridLayout.addWidget(QLabel('And the winner is...'), 0, 0) gridLayout.addWidget(self.labelNeuron, 0, 1) gridLayout.addWidget(QLabel('Because of this, the network claims:'), 1, 0) gridLayout.addWidget(self.labelType, 1, 1) gridLayout.addWidget(QLabel('Threshold:'), 2, 0) gridLayout.addWidget(self.spinBoxThreshold, 2, 1) self.groupBox.setLayout(gridLayout) self.groupBox.setVisible(False) vBoxLayout = QVBoxLayout() vBoxLayout.addLayout(hBoxLayout1) vBoxLayout.addWidget(self.tableWidget) vBoxLayout.addLayout(hBoxLayout2) vBoxLayout.addWidget(self.groupBox) self.setLayout(vBoxLayout) self.setWindowTitle('Simple linear neural network (example2)') self.classNames = ['mammal', 'bird', 'fish'] self.tableWidget.setColumnCount(5) self.tableWidget.setRowCount(6) self.tableWidget.verticalHeader().hide() self.tableWidget.setHorizontalHeaderLabels(['Feature'] + self.classNames + ['Input vector']) self.tableWidget.setCellWidget(0, 0, QLabel('number of legs')) self.tableWidget.setCellWidget(1, 0, QLabel('lives in water')) self.tableWidget.setCellWidget(2, 0, QLabel('can fly')) self.tableWidget.setCellWidget(3, 0, QLabel('has feathers')) self.tableWidget.setCellWidget(4, 0, QLabel('egg-laying')) self.tableWidget.setCellWidget(5, 0, QLabel('Output')) weights = [ [ 4, 0.01, 0.01, -1, -1.5 ], [ 2, -1, 2, 2.5, 2 ], [ -1, 3.5, 0.01, -2, 1.5 ] ] for i in xrange(len(weights)): for j in xrange(len(weights[i])): self.tableWidget.setCellWidget(j, i + 1, QLabel(' ' + str(weights[i][j]))) for i in xrange(len(weights)): self.tableWidget.setCellWidget(5, i + 1, QLabel('')) for i in xrange(len(weights[0])): doubleSpinBox = QDoubleSpinBox() doubleSpinBox.setValue(0.0) doubleSpinBox.setRange(-15.0, 15.0) self.tableWidget.setCellWidget(i, 4, doubleSpinBox) self.tableWidget.setCellWidget(5, 4, QPushButton('Calculate')) self.linearNetwork = LinearNetwork(initialWeights=weights) self.connect(self.checkBoxWinner, SIGNAL('stateChanged(int)'), self.visibleGrid) self.connect(self.tableWidget.cellWidget(5, 4), SIGNAL('clicked()'), self.updateResult) self.resize(600, 400)