-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtf_logreg.py
72 lines (47 loc) · 2.03 KB
/
tf_logreg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
LOCAL_FOLDER = "MNIST_data/"
IMAGE_PIXELS = 784
NUM_CLASSES = 10
LEARNING_RATE = 0.5
TRAINING_STEPS = 1000
BATCH_SIZE = 100
# PREPARING DATA
# downloading (on first run) and extracting MNIST data
data = input_data.read_data_sets(LOCAL_FOLDER, one_hot=True, validation_size=0)
# BUILDING COMPUTATIONAL GRAPH
# model inputs: input pixels and targets
input = tf.placeholder(tf.float32, [None, IMAGE_PIXELS])
targets = tf.placeholder(tf.float32, [None, NUM_CLASSES])
# trainable variables: weights and biases
weights = tf.Variable(tf.zeros([IMAGE_PIXELS, NUM_CLASSES]))
biases = tf.Variable(tf.zeros([NUM_CLASSES]))
# model output: logits
output = tf.matmul(input, weights) + biases
# loss function: cross-entropy with built-in
# (stable) computation of softmax from logits
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(
labels=targets, logits=output
)
)
# training algorithm: gradient descent with configurable learning rate
train_step = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(cross_entropy)
# evaluation operation: ratio of correct predictions
correct_prediction = tf.equal(tf.argmax(output, 1), tf.argmax(targets, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# RUNNING COMPUTATIONAL GRAPH
# creating session
sess = tf.InteractiveSession()
# initializing trainable variables
sess.run(tf.global_variables_initializer())
# training loop
for step in range(TRAINING_STEPS):
# fetching next batch of training data
batch_xs, batch_ys = data.train.next_batch(BATCH_SIZE)
# running the training step with the fetched batch
sess.run(train_step, feed_dict={input: batch_xs, targets: batch_ys})
# evaluating model prediction accuracy of the model on the test set
test_accuracy = sess.run(accuracy, feed_dict={input: data.test.images, targets: data.test.labels})
print("-------------------------------------------------")
print("Test set accuracy: {0:.4f}".format(test_accuracy))