Last active
July 16, 2019 17:10
-
-
Save arafatkatze/7cec701536f33d3bf8899b123b143eb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'tensorflow' | |
| graph = Tensorflow::Graph.new | |
| tensor_1 = Tensorflow::Tensor.new([[2, 2.3], [ 10, 6.2]]) | |
| tensor_2 = Tensorflow::Tensor.new([[4, 3.2], [ 47, 1.2]]) | |
| placeholder_1 = graph.placeholder('tensor1', tensor_1.type_num) | |
| placeholder_2 = graph.placeholder('tensor2', tensor_2.type_num) | |
| opspec = Tensorflow::OpSpec.new('Addition_of_tensors', 'Add', nil, [placeholder_1, placeholder_2]) | |
| op = graph.AddOperation(opspec) | |
| session_op = Tensorflow::Session_options.new | |
| session = Tensorflow::Session.new(graph, session_op) | |
| hash = {} | |
| hash[placeholder_1] = tensor_1 | |
| hash[placeholder_2] = tensor_2 | |
| result = session.run(hash, [op.output(0)], []) | |
| print result[0], "\n" |
Yeah, definitely not the empty () parenthesis if not parameters.
Regarding using symbols instead of strings, we need to be careful initially, because TensorFlow complains if we pass it a Symbol as the key. So at some point we should convert any placeholder names/keys to String before passing them on to TensorFlow and maybe use HashWithIndifferentAccess when a tensor is fetched from TensorFlow.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just a set of small "more ruby-ish" code changes for this example:
Also, note that sometimes you use namespace (
Tensorflow::GraphDef), and sometimes not (Session.new). Also, I supposeTensorFlowis more correct as a namespace name.(It is pretty small "first glance" changes, just for the showcase sake)