Sha256: 1640157fe8dfe9cef32124084e930fab4876e0420145e1aed2a4f72c31383cf7

Contents?: true

Size: 1.51 KB

Versions: 13

Compression:

Stored size: 1.51 KB

Contents

'''
A nearest neighbor learning algorithm example using TensorFlow library.
This example is using the MNIST database of handwritten digits
(http://yann.lecun.com/exdb/mnist/)

Author: Aymeric Damien
Project: https://github.com/aymericdamien/TensorFlow-Examples/

Make sure to install the mnist-learn gem !!
'''
require "bundler/setup"
require 'tensor_stream'
require 'mnist-learn'

tf = TensorStream

# Import MNIST data
mnist = Mnist.read_data_sets('/tmp/data', one_hot: true)

# In this example, we limit mnist data
Xtr, Ytr = mnist.train.next_batch(5000) #5000 for training (nn candidates)
Xte, Yte = mnist.test.next_batch(200) #200 for testing

# tf Graph Input
xtr = tf.placeholder(:float, shape: [nil, 784])
xte = tf.placeholder(:float, shape: [784])

# Nearest Neighbor calculation using L1 Distance
# Calculate L1 Distance
distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.negative(xte))), 1)
# Prediction: Get min distance index (Nearest neighbor)
pred = tf.argmin(distance, 0)

accuracy = 0.0

# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()

# Start training
tf.session do |sess|
  # Run the initializer
  sess.run(init)
  Xte.size.times do |i|
    # Get nearest neighbor
    nn_index = sess.run(pred, feed_dict: {xtr => Xtr, xte => Xte[i]})
    print("Test ", i, "Prediction: ",Ytr[nn_index].max, \
            "True Class: ", Yte[i].max, "\n")
    if Ytr[nn_index].max == Yte[i].max
      accuracy += 1.0/ Xte.size
    end
  end

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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
tensor_stream-1.0.0 samples/nearest_neighbor.rb
tensor_stream-1.0.0.pre.rc1 samples/nearest_neighbor.rb
tensor_stream-0.9.10 samples/nearest_neighbor.rb
tensor_stream-0.9.9 samples/nearest_neighbor.rb
tensor_stream-0.9.8 samples/nearest_neighbor.rb
tensor_stream-0.9.7 samples/nearest_neighbor.rb
tensor_stream-0.9.6 samples/nearest_neighbor.rb
tensor_stream-0.9.5 samples/nearest_neighbor.rb
tensor_stream-0.9.2 samples/nearest_neighbor.rb
tensor_stream-0.9.1 samples/nearest_neighbor.rb
tensor_stream-0.9.0 samples/nearest_neighbor.rb
tensor_stream-0.8.6 samples/nearest_neighbor.rb
tensor_stream-0.8.5 samples/nearest_neighbor.rb