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 +4 -4
- data/lib/tensor_stream/evaluator/ruby_evaluator.rb +0 -1
- data/lib/tensor_stream/nn/nn_ops.rb +2 -2
- data/lib/tensor_stream/version.rb +1 -1
- data/samples/iris.rb +46 -56
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4f9e6d7a1640d4b24b92d7706bcab88ab5bb294551f38dc6ad403eb1b2a761b
|
4
|
+
data.tar.gz: 71c245e394be382e3976f4b2b9989c2b89538f22cc40984a5a84e07fb0bca597
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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)
|
data/samples/iris.rb
CHANGED
@@ -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
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
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
|
-
|
110
|
-
|
111
|
-
|
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}"
|