示例#1
0
文件: main.py 项目: fela98/DigitNet
def main():
    parser = argparse.ArgumentParser(
        description='Train and evaluate DigitNet to predict handwritten digits'
    )
    parser.add_argument('--epochs',
                        default=5,
                        type=int,
                        help='The number of epochs to run the model')
    parser.add_argument(
        '--model',
        default='multilayer',
        help=
        'Specifies which model to run, available models are: simple, sigmoid, multilayer, convolutional'
    )

    args = parser.parse_args()

    if args.model == 'simple':
        model, data_transformer = simple()
    elif args.model == 'sigmoid':
        model, data_transformer = sigmoid()
    elif args.model == 'multilayer':
        model, data_transformer = multilayer()
    elif args.model == 'convolutional':
        model, data_transformer = convolutional()

    print('Summary of model:')
    print(model.summary())

    mnist = mnistdata.read_data_sets("data", one_hot=True, reshape=False)

    print("\n|---- TRAINING ----|")

    batch_size = 100

    training_images = data_transformer(mnist.train.images)
    testing_images = data_transformer(mnist.test.images)

    model.compile(optimizer='rmsprop',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    model.fit(training_images,
              mnist.train.labels,
              epochs=args.epochs,
              batch_size=batch_size)

    print("\n|---- EVALUATING ----|")

    score = model.evaluate(testing_images,
                           mnist.test.labels,
                           batch_size=batch_size)

    print("Accuracy: ", "%.2f" % (score[1] * 100), "%")

    return
示例#2
0
# \x/x\x/x\x/x\x/x\x/    -- fully connected layer (softmax)      W [784, 10]     b[10]
#   · · · · · · · ·                                              Y [batch, 10]

# The model is:
#
# Y = softmax( X * W + b)
#              X: matrix for 100 grayscale images of 28x28 pixels, flattened (there are 100 images in a mini-batch)
#              W: weight matrix with 784 lines and 10 columns
#              b: bias vector with 10 dimensions
#              +: add with broadcasting: adds the vector to each line of the matrix (numpy)
#              softmax(matrix) applies softmax on each line
#              softmax(line) applies an exp to each value then divides by the norm of the resulting line
#              Y: output matrix with 100 lines and 10 columns

# Download images and labels into mnist.test (10K images+labels) and mnist.train (60K images+labels)
mnist = mnistdata.read_data_sets("data", one_hot=True, reshape=False)

# input X: 28x28 grayscale images, the first dimension (None) will index the images in the mini-batch
X = tf.placeholder(tf.float32, [None, 28, 28, 1])
# correct answers will go here
Y_ = tf.placeholder(tf.float32, [None, 10])
# weights W[784, 10]   784=28*28
W = tf.Variable(tf.zeros([784, 10]))
# biases b[10]
b = tf.Variable(tf.zeros([10]))

# flatten the images into a single line of pixels
# -1 in the shape definition means "the only possible dimension that will preserve the number of elements"
XX = tf.reshape(X, [-1, 784])

# The model
# neural network with 5 layers
#
# · · · · · · · · · ·          (input data, flattened pixels)       X [batch, 784]   # 784 = 28*28
# \x/x\x/x\x/x\x/x\x/       -- fully connected layer (relu+BN)      W1 [784, 200]      B1[200]
#  · · · · · · · · ·                                                Y1 [batch, 200]
#   \x/x\x/x\x/x\x/         -- fully connected layer (relu+BN)      W2 [200, 100]      B2[100]
#    · · · · · · ·                                                  Y2 [batch, 100]
#     \x/x\x/x\x/           -- fully connected layer (relu+BN)      W3 [100, 60]       B3[60]
#      · · · · ·                                                    Y3 [batch, 60]
#       \x/x\x/             -- fully connected layer (relu+BN)      W4 [60, 30]        B4[30]
#        · · ·                                                      Y4 [batch, 30]
#         \x/               -- fully connected layer (softmax)      W5 [30, 10]        B5[10]
#          ·                                                        Y5 [batch, 10]

# Download images and labels into mnist.test (10K images+labels) and mnist.train (60K images+labels)
mnist = mnistdata.read_data_sets("data", one_hot=True, reshape=False)

# input X: 28x28 grayscale images, the first dimension (None) will index the images in the mini-batch
X = tf.placeholder(tf.float32, [None, 28, 28, 1])
# correct answers will go here
Y_ = tf.placeholder(tf.float32, [None, 10])
# variable learning rate
lr = tf.placeholder(tf.float32)
# train/test selector for batch normalisation
tst = tf.placeholder(tf.bool)
# training iteration
iter = tf.placeholder(tf.int32)

# five layers and their number of neurons (tha last layer has 10 softmax neurons)
L = 200
M = 100
示例#4
0
def main():
    print("Tensorflow version " + tf.__version__)
    tf.set_random_seed(0)

    mnist = mnistdata.read_data_sets("data", one_hot=True, reshape=False)

    height = 28
    width = 28
    channels = 1
    output = 10
    minibatch_ph = None  # Will be determined during runtime
    minibatch = 100

    X = tf.placeholder(tf.float32, [minibatch_ph, height, width, channels])
    # NOTE: Y_ are correct answers
    Y_ = tf.placeholder(tf.float32, [minibatch_ph, output])
    W = tf.Variable(tf.zeros([height * width, output]))
    b = tf.Variable(tf.zeros([output]))

    # NOTE: Flatten
    # -1 means: Use the dimension which fits
    # In this case it will be minibatch*width
    XX = tf.reshape(X, [-1, height * width])
    Y = tf.nn.softmax(tf.matmul(XX, W) + b)

    # cross-entropy = - sum( Y_i * log(Yi) )
    # NOTE: We use mean to make things independent of batch size
    # NOTE: This is value is connected to batch size and learning rate
    #       In the original solution this was multiplied with 1000
    #       (100 from the batch size, 10 from number of outputs),
    #       and the learning rate was reduced by 1000
    cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y))
    train_step = \
        tf.train.GradientDescentOptimizer(5).minimize(cross_entropy)

    correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # init
    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)

    for i in range(2000 + 1):
        # Get the batches
        batch_X, batch_Y = mnist.train.next_batch(minibatch)

        # Training
        if i % 50 == 0:
            a, c = sess.run([accuracy, cross_entropy],
                            feed_dict={
                                X: batch_X,
                                Y_: batch_Y
                            })

            print(str(i) + ": accuracy:" + str(a) + " loss: " + str(c))

        # Test values
        if i % 10 == 0:
            a, c = sess.run([accuracy, cross_entropy],
                            feed_dict={
                                X: mnist.test.images,
                                Y_: mnist.test.labels
                            })

            print(
                str(i) + ": ********* epoch " +
                str(i * minibatch // mnist.train.images.shape[0] + 1) +
                " ********* test accuracy:" + str(a) + " test loss: " + str(c))

        # Backprop
        sess.run(train_step, feed_dict={X: batch_X, Y_: batch_Y})