Пример #1
0
def example3():
    def load_data(filename):
        this_dir, _ = os.path.split(__file__)
        data_path = os.path.join(this_dir, 'data', filename)
        return np.load(data_path)

    imgs, labels, shape = load_data('FERET.npy')
    x_train, y_train, x_test, y_test = UF.split_train_test(imgs, labels, 2)
    y_train = UF.OneHot(y_train)
    y_test = UF.OneHot(y_test)
    model = NNM.NeuralNetworkModel(dtype=tf.float64)
    # shape=(5,5,3) means the kernel's height=5 width=5 num of ker=3.
    # first layer should give the input dim, which in this case is (image height, image width, num channel).
    model.build(NNU.ConvolutionUnit(dtype=tf.float64, shape=(4, 4, 2),
                                    transfer_fun=tf.tanh), input_dim=x_train.shape[1:])
    # model.build(NNU.BatchNormalization())
    # model.build(NNU.Dropout(keep_prob=0.8))
    # model.build(NNU.ConvolutionUnit(dtype=tf.float64, shape=(2, 2, 4),
    #                                 transfer_fun=tf.sigmoid))

    # model.build(NNU.AvgPooling(dtype=tf.float64, shape=(1, 4, 4, 1)))
    model.build(NNU.Dropout(keep_prob=0.8))
    model.build(NNU.Flatten())
    model.build(NNU.NeuronLayer(hidden_dim=shape[1], dtype=tf.float64))
    model.build(NNU.BatchNormalization())
    model.build(NNU.SoftMaxLayer())
    model.fit(x_train, y_train, loss_fun=NNL.NeuralNetworkLoss.crossentropy, show_graph=False, num_epochs=501,
              mini_batch=10, learning_rate=0.5)
    acc, result, correctness = model.evaluate(x_test, y_test)
    print(acc, result, correctness)
Пример #2
0
def example2():
    from tensorflow.examples.tutorials.mnist import input_data
    mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)

    x_train = mnist.train.images
    # To reduce computational cost, we let the training size be 500.
    x_train = x_train[:500, :]
    y_train = mnist.train.labels
    y_train = y_train[:500, :]
    x_train = UF.vectors2imgs(x_train, (None, 28, 28, 1))
    model = NNM.NeuralNetworkModel(dtype=tf.float32, img_size=(28, 28))
    # shape=(5,5,3) means the kernel's height=5 width=5 num of ker=3
    model.build(NNU.ConvolutionUnit(dtype=tf.float32, shape=(5, 5, 3), transfer_fun=tf.tanh))
    model.build(NNU.AvgPooling(dtype=tf.float32, shape=(1, 4, 4, 1)))
    model.build(NNU.Dropout(keep_prob=0.5))
    model.build(NNU.Flatten())
    model.build(NNU.NeuronLayer(hidden_dim=10, dtype=tf.float32))
    model.build(NNU.SoftMaxLayer())
    model.fit(x_train, y_train, loss_fun=NNL.NeuralNetworkLoss.crossentropy, show_graph=True, num_epochs=1000)

    x_test = mnist.test.images
    y_test = mnist.test.labels
    x_test = x_test[0:20, :]
    y_test = y_test[0:20, :]
    x_test = UF.vectors2imgs(x_test, (None, 28, 28, 1))
    print(model.Evaluate(x_test, y_test))