-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogistic_reg.py
More file actions
39 lines (26 loc) · 1.13 KB
/
logistic_reg.py
File metadata and controls
39 lines (26 loc) · 1.13 KB
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
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
learning_rate = 0.01
training_epochs = 25
batch_size = 100
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
w = tf.Variable(tf.random_normal([784,10]))
b = tf.Variable(tf.zeros([1,10]))
logits = tf.matmul(x,w)+b
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=logits) )
optimizer = tf.train.AdamOptimizer().minimize(cost)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(training_epochs):
epoch_loss = 0
for _ in range(int(mnist.train.num_examples/batch_size)):
batch_x,batch_y = mnist.train.next_batch(batch_size)
_,c = sess.run([optimizer,cost],feed_dict = {x: batch_x, y: batch_y})
epoch_loss+=c
print('Epoch',epoch,'completed out of',training_epochs,'loss:',epoch_loss)
correct = tf.equal(tf.argmax(logits,1),tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct,'float'))
print('Accuracy:',accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))