Sha256: a16446a68f8c5cd9a83bdfd5b53b50749e8b31a36d225f653bd36afd0060d1ed

Contents?: true

Size: 1.51 KB

Versions: 9

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

9 entries across 9 versions & 1 rubygems

Version Path
tensor_stream-1.0.9 samples/others/nearest_neighbor.rb
tensor_stream-1.0.8 samples/others/nearest_neighbor.rb
tensor_stream-1.0.7 samples/others/nearest_neighbor.rb
tensor_stream-1.0.6 samples/others/nearest_neighbor.rb
tensor_stream-1.0.5 samples/others/nearest_neighbor.rb
tensor_stream-1.0.4 samples/others/nearest_neighbor.rb
tensor_stream-1.0.3 samples/others/nearest_neighbor.rb
tensor_stream-1.0.2 samples/others/nearest_neighbor.rb
tensor_stream-1.0.1 samples/others/nearest_neighbor.rb