Skip to content

Commit

Permalink
finish 2_BasicModels
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxin committed Oct 20, 2016
1 parent 9ac85d5 commit 4b8afe3
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/2_BasicModels/rand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import numpy as np
rng = np.random
print rng.randn()
print rng.randn(1)
print rng.randn(5)
print rng.randn(1, 1)
print rng.randn(2, 3)

import tensorflow as tf
# illegal print tf.truncated_normal()
# illegal print tf.truncated_normal(1)
print tf.truncated_normal([ 1 ])
print tf.truncated_normal([ 2, 2 ])

38 changes: 38 additions & 0 deletions examples/2_BasicModels/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import tensorflow as tf

a = tf.placeholder(tf.int32, [None, 4])
b = tf.placeholder(tf.int32, [4])

c = a - b

feed = {
a: [ [ 2, 2, 2, 2 ], [ 3, 3, 3, 3 ], [ 4, 4, 4, 4 ] ],
b: [ 1, 1, 1, 1 ]
}
sess = tf.Session()
print(sess.run(c, feed_dict = feed))

print 'reduce to 1'
e = tf.reduce_sum(c, reduction_indices = 1)
print(sess.run(e, feed_dict = feed))

print 'reduce to 0'
d = tf.reduce_sum(c, reduction_indices = 0)
print(sess.run(d, feed_dict = feed))


print 'reduce to [0, 1]'
d = tf.reduce_sum(c, reduction_indices = [0, 1])
print(sess.run(d, feed_dict = feed))

print 'reduce to [1, 0]'
d = tf.reduce_sum(c, reduction_indices = [1, 0])
print(sess.run(d, feed_dict = feed))

print 'reduce to [0, 0]'
d = tf.reduce_sum(c, reduction_indices = [0, 0])
print(sess.run(d, feed_dict = feed))

print 'reduce to [1, 1]'
d = tf.reduce_sum(c, reduction_indices = [1, 1])
print(sess.run(d, feed_dict = feed))
40 changes: 40 additions & 0 deletions examples/2_BasicModels/zuilinjin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from __future__ import print_function

import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('MNIST_data', one_hot = True)

train_image_set, train_label_set = mnist.train.next_batch(500)
test_image_set, test_label_set = mnist.test.next_batch(10)


# placeholder, Graph Input
train_images = tf.placeholder(tf.float32, [ None, 784 ])
test_image = tf.placeholder(tf.float32, [ 784 ])


# L1 distance
# the - will adapts different dimensions and than will iterates the train_images to minus test_image one by one
distance = tf.reduce_sum(tf.abs(train_images - test_image), reduction_indices = 1)

# find the nearest training example, Graph, not np.argmin
predict_index = tf.arg_min(distance, 0)

init = tf.initialize_all_variables()

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

accuracy = 0
for i in range(len(test_image_set)):
best_neighbour_index = sess.run(predict_index, feed_dict={ train_images: train_image_set, test_image: test_image_set[i] })

#print("test", i, "Prediction:", train_label_set[best_neighbour_index], "True Class:", test_label_set[i])
# vector representation --> index e.g. [ 0, 1, 0, 0] --> 1, [ 0, 0, 0, 1 ] --> 3
print("test", i, "Prediction:", np.argmax(train_label_set[best_neighbour_index]), "True Class:", np.argmax(test_label_set[i]))
if np.argmax(train_label_set[best_neighbour_index]) == np.argmax(test_label_set[i]):
accuracy += 1. / len(test_image_set)

print("Done!", "Accuracy:", accuracy)

0 comments on commit 4b8afe3

Please sign in to comment.