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

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_nameObject



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_executionObject



56
57
58
# File 'lib/tensor_stream.rb', line 56

def self.disable_eager_execution
  TensorStream::Graph.get_default_graph.disable_eager_execution
end

.enable_eager_executionObject



52
53
54
# File 'lib/tensor_stream.rb', line 52

def self.enable_eager_execution
  TensorStream::Graph.get_default_graph.enable_eager_execution
end

.executing_eagerly?Boolean



60
61
62
# File 'lib/tensor_stream.rb', line 60

def self.executing_eagerly?
  TensorStream::Graph.get_default_graph.executing_eagerly?
end

.float32Object



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_graphObject



44
45
46
# File 'lib/tensor_stream.rb', line 44

def self.get_default_graph
  TensorStream::Graph.get_default_graph
end

.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_scopeObject



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_initializerObject



176
177
178
# File 'lib/tensor_stream.rb', line 176

def self.global_variables_initializer
  TensorStream::Variable.global_variables_initializer
end

.graphObject



40
41
42
# File 'lib/tensor_stream.rb', line 40

def self.graph
  TensorStream::Graph.new
end

.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

.layersObject



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

.nnObject

tensorflow compatibility



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_graphObject



48
49
50
# File 'lib/tensor_stream.rb', line 48

def self.reset_default_graph
  TensorStream::Graph.get_default_graph.reset
end

.session(evaluator = :ruby_evaluator, thread_pool_class: Concurrent::ImmediateExecutor) {|session| ... } ⇒ Object

Yields:



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



188
189
190
# File 'lib/tensor_stream.rb', line 188

def self.set_random_seed(seed)
  TensorStream.get_default_graph.random_seed = seed
end

.trainObject



180
181
182
# File 'lib/tensor_stream.rb', line 180

def self.train
  TensorStream::Trainer
end

.trainable_variablesObject



184
185
186
# File 'lib/tensor_stream.rb', line 184

def self.trainable_variables
  TensorStream.get_default_graph.get_collection(TensorStream::GraphKeys::TRAINABLE_VARIABLES)
end

.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

.versionObject



4
5
6
# File 'lib/tensor_stream/version.rb', line 4

def self.version
  VERSION
end