forked from aymericdamien/TensorFlow-Examples
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |