Module: TensorStream
- Extended by:
- OpHelper, Ops
- Defined in:
- lib/tensor_stream.rb,
lib/tensor_stream/ops.rb,
lib/tensor_stream/graph.rb,
lib/tensor_stream/types.rb,
lib/tensor_stream/device.rb,
lib/tensor_stream/tensor.rb,
lib/tensor_stream/session.rb,
lib/tensor_stream/trainer.rb,
lib/tensor_stream/version.rb,
lib/tensor_stream/variable.rb,
lib/tensor_stream/nn/nn_ops.rb,
lib/tensor_stream/operation.rb,
lib/tensor_stream/graph_keys.rb,
lib/tensor_stream/initializer.rb,
lib/tensor_stream/placeholder.rb,
lib/tensor_stream/train/saver.rb,
lib/tensor_stream/control_flow.rb,
lib/tensor_stream/tensor_shape.rb,
lib/tensor_stream/math_gradients.rb,
lib/tensor_stream/evaluator/buffer.rb,
lib/tensor_stream/helpers/op_helper.rb,
lib/tensor_stream/evaluator/evaluator.rb,
lib/tensor_stream/helpers/string_helper.rb,
lib/tensor_stream/evaluator/opencl_buffer.rb,
lib/tensor_stream/evaluator/ruby_evaluator.rb,
lib/tensor_stream/graph_serializers/pbtext.rb,
lib/tensor_stream/graph_serializers/graphml.rb,
lib/tensor_stream/evaluator/opencl_evaluator.rb,
lib/tensor_stream/graph_serializers/serializer.rb,
lib/tensor_stream/train/gradient_descent_optimizer.rb,
lib/tensor_stream/evaluator/operation_helpers/math_helper.rb,
lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb
Overview
module that exposes TensorStream top level functions
Defined Under Namespace
Modules: ArrayOpsHelper, Evaluator, MathHelper, OpHelper, Ops, StringHelper, Train, Trainer, Types
Classes: Buffer, ControlFlow, Device, Graph, GraphKeys, Graphml, Initializer, MathGradients, NN, OpenCLBuffer, Operation, Pbtext, Placeholder, Serializer, Session, Tensor, TensorShape, Variable
Constant Summary
collapse
- VERSION =
'0.3.0'.freeze
Constants included
from Ops
Ops::FLOATING_POINT_TYPES, Ops::INTEGER_TYPES, Ops::NUMERIC_TYPES
Class Method Summary
collapse
-
.__v_scope_name ⇒ Object
-
.check_allowed_types(input, types) ⇒ Object
-
.constant(value, options = {}) ⇒ Object
-
.convert_to_tensor(value, dtype: nil, name: nil, preferred_dtype: nil) ⇒ Object
-
.disable_eager_execution ⇒ Object
-
.enable_eager_execution ⇒ Object
-
.executing_eagerly? ⇒ Boolean
-
.float32 ⇒ Object
-
.get_collection(name, options = {}) ⇒ Object
-
.get_default_graph ⇒ Object
-
.get_variable(name, dtype: nil, shape: nil, initializer: nil, trainable: true, collections: nil) ⇒ Object
-
.get_variable_scope ⇒ Object
-
.global_variables_initializer ⇒ Object
-
.graph ⇒ Object
-
.group(inputs, name: nil) ⇒ Object
-
.layers ⇒ Object
-
.name_scope(name, default: nil, values: nil) ⇒ Object
-
.nn ⇒ Object
tensorflow compatibility.
-
.placeholder(dtype, shape: nil) ⇒ Object
-
.program(&block) ⇒ Object
-
.reset_default_graph ⇒ Object
-
.session(evaluator = :ruby_evaluator, thread_pool_class: Concurrent::ImmediateExecutor) {|session| ... } ⇒ Object
-
.set_random_seed(seed) ⇒ Object
-
.train ⇒ Object
-
.trainable_variables ⇒ Object
-
.variable(value, name: nil, initializer: nil, graph: nil, dtype: nil, trainable: true) ⇒ Object
-
.variable_scope(scope = nil, reuse: nil, initializer: nil) ⇒ Object
-
.version ⇒ Object
Methods included from OpHelper
_op, cons, dtype_eval, format_source, fp_type?, i_cons, i_op, shape_eval, val_to_dtype
Methods included from Ops
abs, add, argmax, argmin, cast, concat, cond, cos, div, equal, exp, eye, glorot_uniform_initializer, gradients, greater, greater_equal, identity, less, less_equal, log, log1p, logical_and, matmul, max, maximum, mul, multiply, negate, negative, not_equal, ones, ones_like, pad, pow, print, random_normal, random_uniform, random_uniform_initializer, rank, reciprocal, reduce_mean, reduce_prod, reduce_sum, reshape, round, shape, sigmoid, sign, sin, slice, sqrt, square, stop_gradient, sub, subtract, tan, tanh, tile, transpose, where, zeros, zeros_initializer, zeros_like
Class Method Details
.__v_scope_name ⇒ Object
117
118
119
|
# File 'lib/tensor_stream.rb', line 117
def self.__v_scope_name
Thread.current[:tensor_stream_variable_scope].map(&:name).compact.reject(&:empty?).join('/')
end
|
.check_allowed_types(input, types) ⇒ Object
202
203
204
205
206
207
|
# File 'lib/tensor_stream.rb', line 202
def self.check_allowed_types(input, types)
return input unless input.is_a?(Tensor)
return input if input.data_type.nil?
raise "#{input.source}: Parameter data type #{input.data_type} passed not in #{types.join(',')}" unless types.include?(input.data_type.to_sym)
end
|
.constant(value, options = {}) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/tensor_stream.rb', line 136
def self.constant(value, options = {})
shared_options = { const: true, value: value, name: options[:name] }
if value.is_a?(Float)
TensorStream::Tensor.new(options[:dtype] || :float32, 0, options[:shape] || [], shared_options)
elsif value.is_a?(Integer)
TensorStream::Tensor.new(options[:dtype] || :int32, 0, options[:shape] || [], shared_options)
elsif value.is_a?(String)
TensorStream::Tensor.new(options[:dtype] || :string, 0, options[:shape] || [], shared_options)
elsif value.is_a?(Array)
dtype = nil
rank = 1
dimensions = []
value_ptr = value
Kernel.loop do
dtype, rank, value_ptr, d = dtype_eval(rank, value_ptr)
dimensions << d
break if dtype != :array
end
TensorStream::Tensor.new(dtype, rank, options[:shape] || dimensions, shared_options)
end
end
|
.convert_to_tensor(value, dtype: nil, name: nil, preferred_dtype: nil) ⇒ Object
192
193
194
195
196
197
198
199
200
|
# File 'lib/tensor_stream.rb', line 192
def self.convert_to_tensor(value, dtype: nil, name: nil, preferred_dtype: nil)
return convert_to_tensor(value.call) if value.is_a?(Proc)
if !value.is_a?(Tensor)
i_cons(value, dtype: dtype || Tensor.detect_type(value), name: name)
else
value
end
end
|
.disable_eager_execution ⇒ Object
.enable_eager_execution ⇒ Object
.executing_eagerly? ⇒ Boolean
.float32 ⇒ Object
36
37
38
|
# File 'lib/tensor_stream.rb', line 36
def self.float32
Types.float32
end
|
.get_collection(name, options = {}) ⇒ Object
168
169
170
|
# File 'lib/tensor_stream.rb', line 168
def self.get_collection(name, options = {})
Graph.get_default_graph.get_collection(name, options)
end
|
.get_default_graph ⇒ Object
.get_variable(name, dtype: nil, shape: nil, initializer: nil, trainable: true, collections: nil) ⇒ Object
164
165
166
|
# File 'lib/tensor_stream.rb', line 164
def self.get_variable(name, dtype: nil, shape: nil, initializer: nil, trainable: true, collections: nil)
TensorStream::Variable.new(dtype || :float32, nil, shape, collections: collections, name: name, initializer: initializer, trainable: trainable)
end
|
.get_variable_scope ⇒ Object
112
113
114
115
|
# File 'lib/tensor_stream.rb', line 112
def self.get_variable_scope
return nil unless Thread.current[:tensor_stream_variable_scope]
__v_scope_name
end
|
.global_variables_initializer ⇒ Object
.group(inputs, name: nil) ⇒ Object
160
161
162
|
# File 'lib/tensor_stream.rb', line 160
def self.group(inputs, name: nil)
TensorStream::ControlFlow.new(:group, inputs, nil, name: name)
end
|
.layers ⇒ Object
132
133
134
|
# File 'lib/tensor_stream.rb', line 132
def self.layers
TensorStream::Layers
end
|
.name_scope(name, default: nil, values: nil) ⇒ Object
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/tensor_stream.rb', line 101
def self.name_scope(name, default: nil, values: nil)
if values
graph_count = values.select { |v| v.is_a?(Tensor) }.map(&:graph).map(&:object_id).uniq.size
raise "values are not on the same graph" if graph_count > 1
end
get_default_graph.name_scope(name || default) do |scope|
yield scope if block_given?
end
end
|
.nn ⇒ Object
31
32
33
|
# File 'lib/tensor_stream/nn/nn_ops.rb', line 31
def self.nn
TensorStream::NN
end
|
.placeholder(dtype, shape: nil) ⇒ Object
172
173
174
|
# File 'lib/tensor_stream.rb', line 172
def self.placeholder(dtype, shape: nil)
TensorStream::Placeholder.new(dtype, nil, shape)
end
|
.program(&block) ⇒ Object
128
129
130
|
# File 'lib/tensor_stream.rb', line 128
def self.program(&block)
block.call(self)
end
|
.reset_default_graph ⇒ Object
.session(evaluator = :ruby_evaluator, thread_pool_class: Concurrent::ImmediateExecutor) {|session| ... } ⇒ Object
121
122
123
124
125
126
|
# File 'lib/tensor_stream.rb', line 121
def self.session(evaluator = :ruby_evaluator, thread_pool_class: Concurrent::ImmediateExecutor)
session = TensorStream::Session.new(evaluator, thread_pool_class: thread_pool_class)
yield session if block_given?
session
end
|
.set_random_seed(seed) ⇒ Object
.trainable_variables ⇒ Object
.variable(value, name: nil, initializer: nil, graph: nil, dtype: nil, trainable: true) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/tensor_stream.rb', line 64
def self.variable(value, name: nil, initializer: nil, graph: nil, dtype: nil, trainable: true)
op = Operation.new(:assign, nil, value)
common_options = {
initializer: initializer || op,
name: name,
graph: graph,
dtype: dtype,
trainable: trainable
}
tensor = if value.is_a?(String)
TensorStream::Variable.new(dtype || :string, 0, [], common_options)
elsif value.is_a?(Integer)
TensorStream::Variable.new(dtype || :int32, 0, [], common_options)
elsif value.is_a?(Float)
TensorStream::Variable.new(dtype || :float32, 0, [], common_options)
else
TensorStream::Variable.new(dtype || :float32, 0, nil, common_options)
end
op.items[0] = tensor
tensor
end
|
.variable_scope(scope = nil, reuse: nil, initializer: nil) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/tensor_stream.rb', line 86
def self.variable_scope(scope = nil, reuse: nil, initializer: nil)
Thread.current[:tensor_stream_variable_scope] ||= []
Thread.current[:tensor_stream_variable_scope] << OpenStruct.new(name: scope, reuse: reuse, initializer: initializer)
scope_name = __v_scope_name
begin
if block_given?
TensorStream.get_default_graph.name_scope(scope) do
yield(scope_name)
end
end
ensure
Thread.current[:tensor_stream_variable_scope].pop
end
end
|
.version ⇒ Object
4
5
6
|
# File 'lib/tensor_stream/version.rb', line 4
def self.version
VERSION
end
|