Skip to content

Commit 2a3ac14

Browse files
committed
Added tensor flow, wip
1 parent 40cef92 commit 2a3ac14

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

MachineLearning/TensorFlow/test.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import tensorflow as tf
2+
from tensorflow.examples.tutorials import mnist
3+
4+
X = tf.placeholder(tf.float32, [None, 28, 28, 1])
5+
W = tf.Variable(tf.zeros([784, 10]))
6+
b = tf.Variable(tf.zeros([10]))
7+
8+
init = tf.initialize_all_variables()
9+
10+
# model
11+
Y = tf.nn.softmax(tf.matmul(tf.reshape(X, [-1, 784]), W) + b)
12+
Y_ = tf.placeholder(tf.float32, [None, 10])
13+
14+
# loss function
15+
cross_entropy = -tf.reduce_sum(Y_ * tf.log*Y)
16+
17+
### relu
18+
# Yf = tf.nn.relu(tf.matmul(X, W) + b)
19+
# pkeep = tf.placeholder(tf.float32)
20+
21+
### drop out
22+
# Y = tf.nn.dropout(Yf, pkeep)
23+
24+
# percentage of correct answers found
25+
is_correct = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1))
26+
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
27+
28+
# train
29+
optimizer = tf.train.GradientDescentOptimizer(0.003) # learning rate
30+
train_step = optimizer.minimiza(cross_entropy) # loss function
31+
32+
33+
sess = tf.Session()
34+
sess.run(init)
35+
36+
for i in range(1000):
37+
# load the batch of images and correct answers
38+
batch_X, batch_Y = mnist.train.next_batch(100)
39+
train_data = {X: batch_X, Y_: batch_Y}
40+
41+
# train
42+
sess.run(train_step, feed_dict=train_data)
43+
# success
44+
a, c = sess.run([accuracy, cross_entropy], feed_dict=train_data)
45+
# success on testing data? (similar to cross validation)
46+
test_data = {X: mnist.test.images, Y_: mnist.test.labels}
47+
a, c = sess.run([accuracy, cross_entropy], feed=test_data)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import tensorflow as tf
2+

0 commit comments

Comments
 (0)