Skip to content

Commit

Permalink
Adding Tensorboard example.
Browse files Browse the repository at this point in the history
  • Loading branch information
arafatkatze committed Mar 5, 2017
1 parent a4f7d19 commit 5b91300
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -114,6 +114,8 @@ This command is to run the tests.
### Install Script
I have also made a make shift install script in tools directory. You are free to use it, but it still needs some work and its best if you follow the installation procedure above or use docker. You are welcome to make improvements to the script.

## Tensorboard
You can run tensorboard on tensorflow.rb too. Just take a look at tensorboard.md file.

## License

Expand Down
2 changes: 1 addition & 1 deletion lib/tensorflow/graph.rb
Expand Up @@ -41,7 +41,7 @@ def import(byte, prefix)
buffer = Tensorflow::TF_NewBuffer()
Tensorflow.buffer_read(buffer, CString(byte))
status = Tensorflow::Status.new
Tensorflow::TF_GraphImportGraphDef(c, buffer, opts, status.c)
Tensorflow::TF_GraphImportGraphDef(self.c, buffer, opts, status.c)
end

# Loads a graph stored in pb file into a graph def. This way you can define the graph
Expand Down
16 changes: 16 additions & 0 deletions spec/tensor_spec.rb
@@ -0,0 +1,16 @@
require 'spec_helper'
describe "Tensorflow::Tensor" do
context 'string tensor' do
it "Should make tensor of string data type." do
input1 = Tensorflow::Tensor.new(["Ruby", "Tensorflow", "is", "cool"], :string)
expect(["Ruby", "Tensorflow", "is", "cool"]).to match_array(Tensorflow::string_reader(input1.tensor))
end

it "Should make tensor of string data type." do
input1 = Tensorflow::Tensor.new("Ruby", :string)
expect(Tensorflow::string_reader(input1.tensor)).to match_array(["Ruby"])
graph = Tensorflow::Graph.new
graph.constant(["Ruby"], dtype: :string)
end
end
end
30 changes: 30 additions & 0 deletions tensorboard.md
@@ -0,0 +1,30 @@
# Tensorboard
Using Tensorboard with Tensorflow.rb is very easy. To make use of tensorboard. First, please make sure that you have installed [tensorflow](https://www.tensorflow.org/install/) completely and tensorflow.rb is working on your system.
I will walk you through a very simple example. Consider the function

```
require 'tensorflow'
graph = Tensorflow::Graph.new
tensor_1 = Tensorflow::Tensor.new([[2, 23, 10, 6]])
tensor_2 = Tensorflow::Tensor.new([[22, 3, 7, 12]])
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
out_tensor = session.run(hash, [op.output(0)], [])
puts out_tensor[0]
graph.write_file("addition.pb")
```
This example is very simple and easy to understand. A graph just adds takes two tensors and adds them.
If you look at the last line that says ``` graph.write_file("addition.pb") ```
Here I am saving the graph defination in [protobuf](https://developers.google.com/protocol-buffers/) format in the file **addition.pb**
Now you can use the [tensorboard.py](https://github.com/Arafatk/tensorflow.rb/blob/master/tensorboard.py) file and convert the **addition.pb** to a format understandable by tensorboard. You can change _directory_ and _filename_ variable as per your convinience.
After running the tensorboard.py file on your addition.py file a new directory will be made as specified in the _directory_ variable and then you can run tensorboard by running the command ```tensorboard --logdir=directory```.
Example if you _directory_ is ```/home/arafat/Desktop/test``` then the command must be run as
```tensorboard --logdir=/home/arafat/Desktop/test```

0 comments on commit 5b91300

Please sign in to comment.