tensor_stream 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6d647cef8f32fa7b3c10460365adfc55ccdd9872e71d453df090986349b615f5
4
- data.tar.gz: baa7be71775bc5d39396343b6d4c32943cf3b79d5a2e591c885bd6fc9314883e
3
+ metadata.gz: f4f9e6d7a1640d4b24b92d7706bcab88ab5bb294551f38dc6ad403eb1b2a761b
4
+ data.tar.gz: 71c245e394be382e3976f4b2b9989c2b89538f22cc40984a5a84e07fb0bca597
5
5
  SHA512:
6
- metadata.gz: d3207ef919464e696d03fe7bbd264ba606565bf09d66796d461b687f047e8b2b969259bcffaf7524677d6b22398ba32025ca8f94c33756cda3a3bb37f535a902
7
- data.tar.gz: 735dbd55e54237619bb9c6653818dade6df257fb71a58cc4abd540a3053d51d318d734ba149c3889bd6403a5219166c291534a66528b83e481ba1240e2696e49
6
+ metadata.gz: a04ca2c61064dc4fb67f2653acbde962f8f8b5f56a5e5f27760f44f6032b81c61307e3b207bbb4ec42bd1713d06315ba760a710a712fb9f124efa6c7f06a7246
7
+ data.tar.gz: ae8e29f412c1b63b854604696df4858ce4d3fb078c982e53a0b85fd8ff6b546c2b9502ecb4d253cb6d0f9255aa1e16b1aaa445da769a0b06bb85ed55a0aebd55
@@ -532,7 +532,6 @@ module TensorStream
532
532
  matrix_a, matrix_b = inputs
533
533
  rank_a = get_rank(matrix_a)
534
534
  rank_b = get_rank(matrix_b)
535
-
536
535
  raise "#{tensor.inputs[0].name} rank must be greater than 1" if rank_a < 2
537
536
  raise "#{tensor.inputs[1].name} rank must be greater than 1" if rank_b < 2
538
537
 
@@ -11,11 +11,11 @@ module TensorStream
11
11
  end
12
12
 
13
13
  def self.sigmoid(input, name: nil)
14
- TensorStream.sigmoid(input, name)
14
+ TensorStream.sigmoid(input, name: name)
15
15
  end
16
16
 
17
17
  def self.softmax_cross_entropy_with_logits(labels: nil, logits: nil, name: nil)
18
- softmax_cross_entropy_with_logits_v2(labels, logits, name)
18
+ softmax_cross_entropy_with_logits_v2(labels: labels, logits: logits, name: name)
19
19
  end
20
20
 
21
21
  def self.softmax_cross_entropy_with_logits_v2(labels: nil, logits: nil, name: nil)
@@ -1,5 +1,5 @@
1
1
  module TensorStream
2
- VERSION = '0.6.0'.freeze
2
+ VERSION = '0.6.1'.freeze
3
3
 
4
4
  def self.version
5
5
  VERSION
@@ -1,5 +1,6 @@
1
1
  require "bundler/setup"
2
2
  require 'tensor_stream'
3
+ require 'tensor_stream/evaluator/opencl/opencl_evaluator'
3
4
 
4
5
  # This neural network will predict the species of an iris based on sepal and petal size
5
6
  # Dataset: http://en.wikipedia.org/wiki/Iris_flower_data_set
@@ -47,65 +48,54 @@ x_test.each_with_index do |x, index|
47
48
  validation_cases << [x, y_test[index] ]
48
49
  end
49
50
 
50
- learning_rate = 0.1
51
- num_steps = 500
52
- batch_size = 128
53
- display_step = 100
54
51
 
55
- # Network Parameters
56
- n_hidden_1 = 4 # 1st layer number of neurons
57
- num_classes = 3 # MNIST total classes (0-9 digits)
58
- num_input = 4
59
- training_epochs = 100
60
52
 
61
- tf = TensorStream
62
-
63
- # tf Graph input
64
- x = tf.placeholder("float", shape: [nil, num_input], name: 'x')
65
- y = tf.placeholder("float", shape: [nil, num_classes], name: 'y')
66
-
67
- # Store layers weight & bias
68
- weights = {
69
- h1: tf.variable(tf.random_normal([num_input, n_hidden_1]), name: 'h1'),
70
- out: tf.variable(tf.random_normal([num_classes, num_classes]), name: 'out')
71
- }
72
-
73
- biases = {
74
- b1: tf.variable(tf.random_normal([n_hidden_1]), name: 'b1'),
75
- out: tf.variable(tf.random_normal([num_classes]), name: 'b_out')
76
- }
53
+ def init_weights(shape)
54
+ # Weight initialization
55
+ weights = TensorStream.random_normal(shape, stddev: 0.1)
56
+ TensorStream.variable(weights)
57
+ end
77
58
 
78
- # Create model
79
- def neural_net(x, weights, biases)
80
- layer_1 = TensorStream.tanh(TensorStream.add(TensorStream.matmul(x, weights[:h1]), biases[:b1], name: 'layer1_add'))
81
- # Output fully connected layer with a neuron for each class
82
- TensorStream.sigmoid(TensorStream.matmul(layer_1, weights[:out]) + biases[:out])
59
+ def forwardprop(x, w_1, w_2)
60
+ # Forward-propagation.
61
+ # IMPORTANT: yhat is not softmax since TensorFlow's softmax_cross_entropy_with_logits() does that internally.
62
+ h = TensorStream.nn.sigmoid(TensorStream.matmul(x, w_1)) # The \sigma function
63
+ TensorStream.matmul(h, w_2) # The \varphi function
83
64
  end
84
65
 
85
- # Construct model
86
- logits = neural_net(x, weights, biases)
87
-
88
- # Mean squared error
89
- cost = TensorStream.reduce_sum(TensorStream.pow(logits - y, 2)) / ( 2 * y_train.size)
90
- optimizer = TensorStream::Train::GradientDescentOptimizer.new(learning_rate).minimize(cost)
91
-
92
- # Initialize the variables (i.e. assign their default value)
93
- init = TensorStream.global_variables_initializer()
94
-
95
- TensorStream.session do |sess|
96
- puts "init vars"
97
- sess.run(init)
98
- puts "Testing the untrained network..."
99
- loss = sess.run(cost, feed_dict: { x => x_train, y => y_train })
100
- puts loss
101
- puts "loss before training"
102
- (0..training_epochs).each do |epoch|
103
- x_train.zip(y_train).each do |t_x, t_y|
104
- sess.run(optimizer, feed_dict: { x => [t_x], y => [t_y] })
105
- loss = sess.run(cost, feed_dict: { x => [t_x], y => [t_y] })
106
- end
107
- puts "loss #{loss}"
66
+ x_size = x_train[0].size
67
+ y_size = y_train[0].size
68
+ h_size = 256
69
+ X = tf.placeholder(:float, shape: [nil, x_size])
70
+ y = tf.placeholder(:float, shape: [nil, y_size])
71
+
72
+ # Weight initializations
73
+ w_1 = init_weights([x_size, h_size])
74
+ w_2 = init_weights([h_size, y_size])
75
+
76
+ # Forward propagation
77
+ yhat = forwardprop(X, w_1, w_2)
78
+ predict = tf.argmax(yhat, axis=1)
79
+
80
+ # Backward propagation
81
+ cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels: y, logits: yhat))
82
+ updates = TensorStream::Train::GradientDescentOptimizer.new(0.01).minimize(cost)
83
+
84
+ # Run SGD
85
+ sess = tf.session
86
+ init = tf.global_variables_initializer
87
+ sess.run(init)
88
+
89
+ loss = sess.run(cost, feed_dict: { X => x_train, y => y_train })
90
+ puts "Testing the untrained network..."
91
+ puts loss
92
+ (0..100).each do |epoch|
93
+ x_train.size.times do |i|
94
+ sess.run(updates, feed_dict: {X => [x_train[i]], y => [y_train[i]]})
95
+ loss = sess.run(cost, feed_dict: { X => [x_train[i]], y => [y_train[i]] })
108
96
  end
109
- loss = sess.run(cost, feed_dict: { x => x_train, y => y_train })
110
- puts "loss after training #{loss}"
111
- end
97
+ puts "epoch: #{epoch}, loss #{loss}"
98
+ end
99
+
100
+ loss = sess.run(cost, feed_dict: { X => x_train, y => y_train })
101
+ puts "loss after training #{loss}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tensor_stream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Emmanuel Dayo