[tf]完成一个简单的模型

2018-12-13  本文已影响0人  VanJordan
x = tf.placeholder(tf.float32, [None, 28, 28, 1])
t = tf.placeholder(tf.float32, [None, 10])
w = tf.Variable(tf.zeros([784, 10]))
w1 = tf.Variable(tf.truncated_normal([L, M], stddev=0.1))
b = tf.Variable(tf.zeros([10]))
init_op = tf.global_variables_initializer()
is_correct = tf.equal(tf.argmax(y,1), tf.argmax(t,1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.003)
train_step = optimizer.minimize(cross_entropy)
with tf.Session() as sess:
    for step in range(1000):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        sess.run(train_step, feed_dict={x: batch_xs, t: batch_ys})
        if step % 100 == 0:
            acc, loss = sess.run([accuracy, cross_entropy],
            feed_dict={x: batch_xs, t: batch_ys})
            acc, loss = sess.run([accuracy, cross_entropy],
            feed_dict={x: mnist.test.images, t: mnist.test.labels})
lr = tf.placeholder(tf.float32)
train_step = tf.train.AdamOptimizer(lr).minimize(cross_entropy)

def lr(step):
    max_lr, min_lr, decay_speed = 0.003, 0.0001, 2000.0
    return min_lr + (max_lr - min_lr) * math.exp(-step/decay_speed)

with tf.Session() as sess:
    for step in range(10000):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        sess.run(train_step,
        feed_dict={x: batch_xs, t: batch_ys, pkeep: 0.75, lr: lr(step)})
pkeep = tf.placeholder(tf.float32)
y1 = tf.nn.relu(tf.matmul(x, w1) + b1)
y1d = tf.nn.dropout(y1, pkeep)

with tf.Session() as sess:
    for step in range(10000):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        sess.run(train_step,
            feed_dict={x: batch_xs, t: batch_ys, pkeep: 0.75, lr: lr(step)})
    if step % 100 == 0:
        acc, loss = sess.run([accuracy, cross_entropy],
            feed_dict={x: batch_xs, t: batch_ys, pkeep: 1})
        acc, loss = sess.run([accuracy, cross_entropy],
            feed_dict={x: mnist.test.images, t: mnist.test.labels, pkeep: 1})
上一篇 下一篇

猜你喜欢

热点阅读