Beispiel #1
0
    n_features = 28 * 28  # 与手写图片尺寸相对应
    n_hidden1 = 300  # 定义包含输出层在内的三个层
    n_hidden2 = 100
    n_classes = 10  # 数字分类问题,最后一层是 10个输出
    layers = []
    relu = NN_activators.ReLUActivator()
    layers.append(NN_Layer.Layer(n_features, n_hidden1, activator=relu))
    layers.append(NN_Layer.Layer(n_hidden1, n_hidden2, activator=relu))
    layers.append(NN_Layer.Layer(n_hidden2, n_classes))
    return layers


# end

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
X_train, Y_train = mnist.train.images, mnist.train.labels
X_test, Y_test = mnist.test.images, mnist.test.labels

layers = create_layers()
loss = NN_Loss.LogarithmicLoss()

model = NeuralNetwork(layers, loss)
model.fit(X_train, Y_train, 50000, 0.01)
V = model.predict(X_test)  # 注意:输出的是与 OneHot向量同维度的一个矩阵(一个测试点对应一个 OneHot向量)

PROBA = NN_Loss.softmax(V)  # 得到 softmax矩阵
y_pred = np.argmax(PROBA, axis=1)

accuracy = accuracy_score(np.argmax(Y_test, axis=1), y_pred)
print("accuracy: {}.".format(accuracy))
Beispiel #2
0
    layers = []
    relu = NN_activators.ReLUActivator()
    layers.append(NN_Layer.Layer(n_inputs, n_hidden1, activator=relu))
    layers.append(NN_Layer.Layer(n_hidden1, n_hidden2, activator=relu))
    layers.append(NN_Layer.Layer(n_hidden2, n_outputs))
    return layers


# end

housing = fetch_california_housing()
X = housing.data
y = housing.target.reshape(-1, 1)

X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.5,
                                                    random_state=0)

X_train = process_features(X_train)
X_test = process_features(X_test)

layers = create_layers()
loss = NN_Loss.SquaredLoss()  # 回归问题,损失函数定义为平方损失函数

model = NeuralNetwork(layers, loss)
model.fit(X_train, y_train, 100000, 0.01)
y_pred = model.predict(X_test)

print("r2_score: {}.".format(r2_score(y_test, y_pred)))
Beispiel #3
0
epochs = 1000  # number of training epochs

#check the timing of model-1
t0 = time.time()  #start time

#define model-1
model1 = NN(input_layer_size=m,
            output_layer_size=n_class,
            hidden_layer_size=hidden_layers)
model1.train(X_train, Y_train, l_rate=l_rate, n_epochs=epochs)
#end time
t1 = time.time()
total_time = t1 - t0

#prediction of our model on test and training data
Y_train_predict = model1.predict(X_train)
Y_test_predict = model1.predict(X_test)
accuracy_train = 100 * np.sum(Y_train == Y_train_predict) / len(Y_train)
accuracy_test = 100 * np.sum(Y_test == Y_test_predict) / len(Y_test)
print("total time for training model-1:", total_time)
print("accuracy of model-1 on training data", accuracy_train)
print("accuracy of model-1 on test data", accuracy_test)

#define neural network model-2 parameters
hidden_layers = [
    100
]  # hidden layer size is passed as a list ith position value determines number of neuron in ith layer
l_rate = 0.3  # learning rate
epochs = 800  # number of training epochs

#check the timing of model-2
Beispiel #4
0
class MainWidget(QWidget):
    def __init__(self, Parent=None):
        super().__init__(Parent)
        self.__result = -1
        self.__initData()
        self.__initView()

    def __initData(self):
        self.__paintBoard = PaintBoard(self)
        self.__model = NeuralNetwork()

    def __initView(self):
        self.setFixedSize(600, 400)
        self.setWindowTitle('Application')

        main_layout = QHBoxLayout(self)
        main_layout.setSpacing(10)
        main_layout.addWidget(self.__paintBoard)

        sub_layout = QVBoxLayout()
        sub_layout.setContentsMargins(10, 10, 10, 10)
        sub_layout.setSpacing(30)

        self.__btn_Clear = QPushButton('clear')
        self.__btn_Clear.setParent(self)
        self.__btn_Clear.clicked.connect(self.__paintBoard.clear)
        sub_layout.addWidget(self.__btn_Clear)

        self.__btn_Predict = QPushButton('predict')
        self.__btn_Predict.setParent(self)
        self.__btn_Predict.clicked.connect(self.predict)
        sub_layout.addWidget(self.__btn_Predict)

        self.__btn_Quit = QPushButton('quit')
        self.__btn_Quit.setParent(self)
        self.__btn_Quit.clicked.connect(self.quit)
        sub_layout.addWidget(self.__btn_Quit)

        self.__lb_Result_Tip = QLabel()
        font = QFont()
        font.setPointSize(24)
        self.__lb_Result_Tip.setFont(font)

        self.__lb_Result_Tip.setText('result')
        self.__lb_Result_Tip.setParent(self)
        sub_layout.addWidget(self.__lb_Result_Tip)

        self.__lb_Result = QLabel()
        font = QFont()
        font.setPointSize(30)
        self.__lb_Result.setFont(font)
        self.__lb_Result.setParent(self)
        self.__lb_Result.setAlignment(Qt.AlignHCenter)
        sub_layout.addWidget(self.__lb_Result)

        main_layout.addLayout(sub_layout)

    def quit(self):
        self.close()

    def predict(self):
        image = self.__paintBoard.getImage()
        pil_img = ImageQt.fromqimage(image)
        pil_img = pil_img.resize((28, 28), Image.ANTIALIAS)
        # pil_img.save('./images/test66.png')
        # pil_img.show()

        img_array = np.array(pil_img.convert('L')).reshape(1, 784)
        # img_array = np.hstack([img_array, [1.0]]).reshape((1, 785))

        # display image
        plt.imshow(img_array.reshape(28, 28), cmap="binary")
        # plt.imshow(pil_img, cmap="binary")
        plt.show()
        # fig = plt.figure(figsize=(6, 6))
        # fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
        # # 绘制数字:每张图像8*8像素点
        # for i in range(64):
        #     ax = fig.add_subplot(8, 8, i + 1, xticks=[], yticks=[])
        #     ax.imshow(self.xtest[i].reshape(28, 28), cmap=plt.cm.binary, interpolation='nearest')
        #     # 用目标值标记图像
        #     ax.text(0, 7, str(self.ytest[i]))
        # plt.show()

        self.__result = self.__model.predict(img_array)
        print("result: %d" % self.__result)
        self.__lb_Result.setText("%d" % self.__result)