Class: TensorStream::Session
- Inherits:
-
Object
- Object
- TensorStream::Session
- Defined in:
- lib/tensor_stream/session.rb
Overview
TensorStream class that defines a session
Instance Attribute Summary collapse
-
#last_session_context ⇒ Object
readonly
Returns the value of attribute last_session_context.
Class Method Summary collapse
Instance Method Summary collapse
- #dump_internal_ops(tensor) ⇒ Object
- #dump_ops(tensor, selector) ⇒ Object
- #dump_user_ops(tensor) ⇒ Object
-
#initialize(evaluator = :ruby_evaluator, thread_pool_class: Concurrent::ImmediateExecutor) ⇒ Session
constructor
A new instance of Session.
- #run(*args) ⇒ Object
Constructor Details
#initialize(evaluator = :ruby_evaluator, thread_pool_class: Concurrent::ImmediateExecutor) ⇒ Session
Returns a new instance of Session.
5 6 7 8 |
# File 'lib/tensor_stream/session.rb', line 5 def initialize(evaluator = :ruby_evaluator, thread_pool_class: Concurrent::ImmediateExecutor) @evaluator_class = Object.const_get("TensorStream::Evaluator::#{camelize(evaluator.to_s)}") @thread_pool = thread_pool_class.new end |
Instance Attribute Details
#last_session_context ⇒ Object (readonly)
Returns the value of attribute last_session_context.
4 5 6 |
# File 'lib/tensor_stream/session.rb', line 4 def last_session_context @last_session_context end |
Class Method Details
Instance Method Details
#dump_internal_ops(tensor) ⇒ Object
37 38 39 |
# File 'lib/tensor_stream/session.rb', line 37 def dump_internal_ops(tensor) dump_ops(tensor, ->(_k, n) { n.is_a?(Tensor) && n.internal? }) end |
#dump_ops(tensor, selector) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/tensor_stream/session.rb', line 45 def dump_ops(tensor, selector) graph = tensor.graph graph.nodes.select { |k, v| selector.call(k, v) }.collect do |k, node| next unless @last_session_context[node.name] "#{k} #{node.to_math(true, 1)} = #{@last_session_context[node.name]}" end.compact end |
#dump_user_ops(tensor) ⇒ Object
41 42 43 |
# File 'lib/tensor_stream/session.rb', line 41 def dump_user_ops(tensor) dump_ops(tensor, ->(_k, n) { n.is_a?(Tensor) && !n.internal? }) end |
#run(*args) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/tensor_stream/session.rb', line 14 def run(*args) = if args.last.is_a?(Hash) args.pop else {} end context = {} # scan for placeholders and assign value if [:feed_dict] [:feed_dict].keys.each do |k| context[k.name.to_sym] = [:feed_dict][k] if k.is_a?(Placeholder) end end evaluator = @evaluator_class.new(self, context.merge!(retain: [:retain]), thread_pool: @thread_pool) execution_context = {} result = args.collect { |e| evaluator.run(e, execution_context) } @last_session_context = context result.size == 1 ? result.first : result end |