tensor_stream 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +74 -0
- data/README.md +4 -3
- data/lib/tensor_stream.rb +17 -18
- data/lib/tensor_stream/control_flow.rb +7 -4
- data/lib/tensor_stream/evaluator/evaluator.rb +1 -1
- data/lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb +6 -7
- data/lib/tensor_stream/evaluator/ruby_evaluator.rb +80 -91
- data/lib/tensor_stream/graph.rb +51 -18
- data/lib/tensor_stream/graph_keys.rb +2 -2
- data/lib/tensor_stream/helpers/op_helper.rb +31 -27
- data/lib/tensor_stream/math_gradients.rb +20 -23
- data/lib/tensor_stream/nn/nn_ops.rb +2 -2
- data/lib/tensor_stream/operation.rb +28 -45
- data/lib/tensor_stream/ops.rb +103 -103
- data/lib/tensor_stream/placeholder.rb +7 -4
- data/lib/tensor_stream/session.rb +20 -22
- data/lib/tensor_stream/tensor.rb +43 -101
- data/lib/tensor_stream/tensor_shape.rb +4 -3
- data/lib/tensor_stream/train/gradient_descent_optimizer.rb +5 -5
- data/lib/tensor_stream/train/saver.rb +13 -13
- data/lib/tensor_stream/trainer.rb +1 -1
- data/lib/tensor_stream/types.rb +14 -1
- data/lib/tensor_stream/variable.rb +8 -7
- data/lib/tensor_stream/version.rb +1 -1
- data/samples/iris.rb +7 -7
- data/samples/linear_regression.rb +3 -3
- data/samples/raw_neural_net_sample.rb +6 -6
- data/tensor_stream.gemspec +1 -0
- metadata +17 -2
@@ -1,7 +1,8 @@
|
|
1
1
|
module TensorStream
|
2
|
+
# class that defines a shape for TensorFlow compatibility
|
2
3
|
class TensorShape
|
3
4
|
attr_accessor :rank, :shape
|
4
|
-
|
5
|
+
|
5
6
|
def initialize(shape, rank)
|
6
7
|
@shape = shape
|
7
8
|
@rank = rank
|
@@ -17,9 +18,9 @@ module TensorStream
|
|
17
18
|
def [](index)
|
18
19
|
@shape[index]
|
19
20
|
end
|
20
|
-
|
21
|
+
|
21
22
|
def ndims
|
22
23
|
shape.size
|
23
24
|
end
|
24
25
|
end
|
25
|
-
end
|
26
|
+
end
|
@@ -4,14 +4,14 @@ module TensorStream
|
|
4
4
|
class GradientDescentOptimizer
|
5
5
|
attr_accessor :learning_rate
|
6
6
|
|
7
|
-
def initialize(learning_rate,
|
7
|
+
def initialize(learning_rate, _options = {})
|
8
8
|
@learning_rate = learning_rate
|
9
9
|
end
|
10
10
|
|
11
11
|
def minimize(cost)
|
12
|
-
trainable_vars = TensorStream::Graph.get_default_graph
|
13
|
-
|
14
|
-
|
12
|
+
trainable_vars = TensorStream::Graph.get_default_graph
|
13
|
+
.get_collection(GraphKeys::GLOBAL_VARIABLES)
|
14
|
+
.select(&:trainable)
|
15
15
|
|
16
16
|
derivatives = TensorStream.gradients(cost, trainable_vars)
|
17
17
|
trainable_vars.each_with_index.collect do |var, index|
|
@@ -20,4 +20,4 @@ module TensorStream
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
23
|
-
end
|
23
|
+
end
|
@@ -2,16 +2,16 @@ require 'json'
|
|
2
2
|
|
3
3
|
module TensorStream
|
4
4
|
module Train
|
5
|
+
# High level class used for loading and saving variables
|
5
6
|
class Saver
|
6
|
-
def save(session, outputfile,
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
strip_default_attrs: false)
|
7
|
+
def save(session, outputfile, global_step: nil,
|
8
|
+
latest_filename: nil,
|
9
|
+
meta_graph_suffix: 'meta',
|
10
|
+
write_meta_graph: true,
|
11
|
+
write_state: true,
|
12
|
+
strip_default_attrs: false)
|
13
13
|
vars = TensorStream::Graph.get_default_graph.get_collection(GraphKeys::GLOBAL_VARIABLES)
|
14
|
-
|
14
|
+
|
15
15
|
variables = {}
|
16
16
|
graph = {}
|
17
17
|
gs = eval_global_step(session, global_step)
|
@@ -34,12 +34,12 @@ module TensorStream
|
|
34
34
|
path
|
35
35
|
end
|
36
36
|
|
37
|
-
def restore(
|
37
|
+
def restore(_session, inputfile)
|
38
38
|
input_dump = JSON.parse(File.read(inputfile))
|
39
39
|
|
40
40
|
vars = TensorStream::Graph.get_default_graph.get_collection(GraphKeys::GLOBAL_VARIABLES)
|
41
41
|
vars.each do |variable|
|
42
|
-
variable.value = input_dump[
|
42
|
+
variable.value = input_dump['variables'][variable.name]
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -48,9 +48,9 @@ module TensorStream
|
|
48
48
|
def eval_global_step(session, global_step)
|
49
49
|
return nil if global_step.nil?
|
50
50
|
|
51
|
-
if
|
51
|
+
if global_step.is_a?(Tensor)
|
52
52
|
session.last_session_context(global_step.name)
|
53
|
-
elsif
|
53
|
+
elsif global_step.is_a?(String) || global_step.is_a?(Symbol)
|
54
54
|
session.last_session_context(global_step)
|
55
55
|
else
|
56
56
|
global_step.to_i
|
@@ -58,4 +58,4 @@ module TensorStream
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
end
|
61
|
-
end
|
61
|
+
end
|
data/lib/tensor_stream/types.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'ostruct'
|
2
2
|
|
3
3
|
module TensorStream
|
4
|
+
# Convenience class for specifying valid data_types
|
4
5
|
module Types
|
5
6
|
def self.int16
|
6
7
|
:int16
|
@@ -13,5 +14,17 @@ module TensorStream
|
|
13
14
|
def self.int32
|
14
15
|
:int32
|
15
16
|
end
|
17
|
+
|
18
|
+
def self.float64
|
19
|
+
:float64
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.string
|
23
|
+
:string
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.boolean
|
27
|
+
:boolean
|
28
|
+
end
|
16
29
|
end
|
17
|
-
end
|
30
|
+
end
|
@@ -1,17 +1,18 @@
|
|
1
1
|
module TensorStream
|
2
|
+
# Class that defines a TensorStream variable
|
2
3
|
class Variable < Tensor
|
3
4
|
attr_accessor :trainable
|
4
5
|
def initialize(data_type, rank, shape, options = {})
|
6
|
+
@graph = options[:graph] || TensorStream.get_default_graph
|
7
|
+
|
5
8
|
@data_type = data_type
|
6
9
|
@rank = rank
|
7
10
|
@shape = TensorShape.new(shape, rank)
|
8
11
|
@value = nil
|
9
|
-
@source =
|
10
|
-
|
12
|
+
@source = format_source(caller_locations)
|
13
|
+
|
11
14
|
@name = options[:name] || build_name
|
12
|
-
if options[:initializer]
|
13
|
-
@initalizer_tensor = options[:initializer]
|
14
|
-
end
|
15
|
+
@initalizer_tensor = options[:initializer] if options[:initializer]
|
15
16
|
@trainable = options.fetch(:trainable, true)
|
16
17
|
@graph.add_variable(self, options)
|
17
18
|
end
|
@@ -33,7 +34,7 @@ module TensorStream
|
|
33
34
|
Operation.new(:assign_add, self, value)
|
34
35
|
end
|
35
36
|
|
36
|
-
def to_math(
|
37
|
+
def to_math(_tensor, _name_only = false, _max_depth = 99)
|
37
38
|
@name
|
38
39
|
end
|
39
40
|
|
@@ -49,4 +50,4 @@ module TensorStream
|
|
49
50
|
variables_initializer(TensorStream::GraphKeys::GLOBAL_VARIABLES)
|
50
51
|
end
|
51
52
|
end
|
52
|
-
end
|
53
|
+
end
|
data/samples/iris.rb
CHANGED
@@ -68,15 +68,15 @@ y = tf.placeholder("float", shape: [nil, num_classes], name: 'y')
|
|
68
68
|
|
69
69
|
# Store layers weight & bias
|
70
70
|
weights = {
|
71
|
-
h1: tf.
|
72
|
-
h2: tf.
|
73
|
-
out: tf.
|
71
|
+
h1: tf.variable(tf.random_normal([num_input, n_hidden_1]), name: 'h1'),
|
72
|
+
h2: tf.variable(tf.random_normal([n_hidden_1, n_hidden_2]), name: 'h2'),
|
73
|
+
out: tf.variable(tf.random_normal([n_hidden_2, num_classes]), name: 'out')
|
74
74
|
}
|
75
75
|
|
76
76
|
biases = {
|
77
|
-
b1: tf.
|
78
|
-
b2: tf.
|
79
|
-
out: tf.
|
77
|
+
b1: tf.variable(tf.random_normal([n_hidden_1]), name: 'b1'),
|
78
|
+
b2: tf.variable(tf.random_normal([n_hidden_2]), name: 'b2'),
|
79
|
+
out: tf.variable(tf.random_normal([num_classes]), name: 'b_out')
|
80
80
|
}
|
81
81
|
|
82
82
|
|
@@ -100,7 +100,7 @@ optimizer = TensorStream::Train::GradientDescentOptimizer.new(learning_rate).min
|
|
100
100
|
# Initialize the variables (i.e. assign their default value)
|
101
101
|
init = TensorStream.global_variables_initializer()
|
102
102
|
|
103
|
-
TensorStream.
|
103
|
+
TensorStream.session do |sess|
|
104
104
|
puts "init vars"
|
105
105
|
sess.run(init)
|
106
106
|
puts "Testing the untrained network..."
|
@@ -19,8 +19,8 @@ X = tf.placeholder("float")
|
|
19
19
|
Y = tf.placeholder("float")
|
20
20
|
|
21
21
|
# Set model weights
|
22
|
-
W = tf.
|
23
|
-
b = tf.
|
22
|
+
W = tf.variable(rand, name: "weight")
|
23
|
+
b = tf.variable(rand, name: "bias")
|
24
24
|
|
25
25
|
# Construct a linear model
|
26
26
|
pred = X * W + b
|
@@ -33,7 +33,7 @@ optimizer = TensorStream::Train::GradientDescentOptimizer.new(learning_rate).min
|
|
33
33
|
# Initialize the variables (i.e. assign their default value)
|
34
34
|
init = tf.global_variables_initializer()
|
35
35
|
|
36
|
-
tf.
|
36
|
+
tf.session do |sess|
|
37
37
|
start_time = Time.now
|
38
38
|
sess.run(init)
|
39
39
|
(0..training_epochs).each do |epoch|
|
@@ -21,15 +21,15 @@ Y = tf.placeholder("float", shape: [nil, num_classes])
|
|
21
21
|
|
22
22
|
# Store layers weight & bias
|
23
23
|
@weights = {
|
24
|
-
h1: tf.
|
25
|
-
h2: tf.
|
26
|
-
out: tf.
|
24
|
+
h1: tf.variable(tf.random_normal([num_input, n_hidden_1])),
|
25
|
+
h2: tf.variable(tf.random_normal([n_hidden_1, n_hidden_2])),
|
26
|
+
out: tf.variable(tf.random_normal([n_hidden_2, num_classes]))
|
27
27
|
}
|
28
28
|
|
29
29
|
@biases = {
|
30
|
-
b1: tf.
|
31
|
-
b2: tf.
|
32
|
-
out: tf.
|
30
|
+
b1: tf.variable(tf.random_normal([n_hidden_1])),
|
31
|
+
b2: tf.variable(tf.random_normal([n_hidden_2])),
|
32
|
+
out: tf.variable(tf.random_normal([num_classes]))
|
33
33
|
}
|
34
34
|
|
35
35
|
|
data/tensor_stream.gemspec
CHANGED
@@ -35,6 +35,7 @@ Gem::Specification.new do |spec|
|
|
35
35
|
spec.add_development_dependency "rspec", "~> 3.0"
|
36
36
|
spec.add_development_dependency "awesome_print"
|
37
37
|
spec.add_development_dependency "rubocop"
|
38
|
+
spec.add_development_dependency "pry-byebug"
|
38
39
|
spec.add_dependency "deep_merge"
|
39
40
|
spec.add_dependency "concurrent-ruby"
|
40
41
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tensor_stream
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joseph Emmanuel Dayo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry-byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: deep_merge
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -121,6 +135,7 @@ files:
|
|
121
135
|
- ".gitignore"
|
122
136
|
- ".rake_tasks~"
|
123
137
|
- ".rspec"
|
138
|
+
- ".rubocop.yml"
|
124
139
|
- ".travis.yml"
|
125
140
|
- CODE_OF_CONDUCT.md
|
126
141
|
- Gemfile
|