tensorflow-ruby 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +18 -0
- data/LICENSE.txt +22 -0
- data/README.md +104 -0
- data/lib/datasets/download_manager.rb +49 -0
- data/lib/datasets/images/mnist.rb +54 -0
- data/lib/datasets/resource.rb +19 -0
- data/lib/tensorflow-ruby.rb +182 -0
- data/lib/tensorflow.rb +1 -0
- data/lib/tensorflow/batchable_type_spec.rb +4 -0
- data/lib/tensorflow/core/framework/allocation_description_pb.rb +21 -0
- data/lib/tensorflow/core/framework/api_def_pb.rb +59 -0
- data/lib/tensorflow/core/framework/attr_value_pb.rb +46 -0
- data/lib/tensorflow/core/framework/cost_graph_pb.rb +49 -0
- data/lib/tensorflow/core/framework/device_attributes_pb.rb +37 -0
- data/lib/tensorflow/core/framework/function_pb.rb +38 -0
- data/lib/tensorflow/core/framework/graph_pb.rb +22 -0
- data/lib/tensorflow/core/framework/graph_transfer_info_pb.rb +73 -0
- data/lib/tensorflow/core/framework/kernel_def_pb.rb +31 -0
- data/lib/tensorflow/core/framework/log_memory_pb.rb +53 -0
- data/lib/tensorflow/core/framework/node_def_pb.rb +27 -0
- data/lib/tensorflow/core/framework/op_def_pb.rb +58 -0
- data/lib/tensorflow/core/framework/reader_base_pb.rb +19 -0
- data/lib/tensorflow/core/framework/remote_fused_graph_execute_info_pb.rb +30 -0
- data/lib/tensorflow/core/framework/resource_handle_pb.rb +28 -0
- data/lib/tensorflow/core/framework/step_stats_pb.rb +72 -0
- data/lib/tensorflow/core/framework/summary_pb.rb +71 -0
- data/lib/tensorflow/core/framework/tensor_description_pb.rb +21 -0
- data/lib/tensorflow/core/framework/tensor_pb.rb +41 -0
- data/lib/tensorflow/core/framework/tensor_shape_pb.rb +22 -0
- data/lib/tensorflow/core/framework/tensor_slice_pb.rb +23 -0
- data/lib/tensorflow/core/framework/types_pb.rb +62 -0
- data/lib/tensorflow/core/framework/variable_pb.rb +45 -0
- data/lib/tensorflow/core/framework/versions_pb.rb +18 -0
- data/lib/tensorflow/core/lib/core/error_codes_pb.rb +35 -0
- data/lib/tensorflow/core/protobuf/cluster_pb.rb +21 -0
- data/lib/tensorflow/core/protobuf/config_pb.rb +180 -0
- data/lib/tensorflow/core/protobuf/control_flow_pb.rb +48 -0
- data/lib/tensorflow/core/protobuf/conv_autotuning_pb.rb +29 -0
- data/lib/tensorflow/core/protobuf/critical_section_pb.rb +21 -0
- data/lib/tensorflow/core/protobuf/debug_pb.rb +38 -0
- data/lib/tensorflow/core/protobuf/device_properties_pb.rb +33 -0
- data/lib/tensorflow/core/protobuf/eager_service_pb.rb +112 -0
- data/lib/tensorflow/core/protobuf/graph_debug_info_pb.rb +29 -0
- data/lib/tensorflow/core/protobuf/master_pb.rb +123 -0
- data/lib/tensorflow/core/protobuf/master_service_pb.rb +15 -0
- data/lib/tensorflow/core/protobuf/meta_graph_pb.rb +95 -0
- data/lib/tensorflow/core/protobuf/named_tensor_pb.rb +18 -0
- data/lib/tensorflow/core/protobuf/queue_runner_pb.rb +21 -0
- data/lib/tensorflow/core/protobuf/replay_log_pb.rb +48 -0
- data/lib/tensorflow/core/protobuf/rewriter_config_pb.rb +81 -0
- data/lib/tensorflow/core/protobuf/saved_model_pb.rb +18 -0
- data/lib/tensorflow/core/protobuf/saved_object_graph_pb.rb +87 -0
- data/lib/tensorflow/core/protobuf/saver_pb.rb +28 -0
- data/lib/tensorflow/core/protobuf/struct_pb.rb +81 -0
- data/lib/tensorflow/core/protobuf/tensor_bundle_pb.rb +37 -0
- data/lib/tensorflow/core/protobuf/tensorflow_server_pb.rb +22 -0
- data/lib/tensorflow/core/protobuf/trace_events_pb.rb +39 -0
- data/lib/tensorflow/core/protobuf/trackable_object_graph_pb.rb +40 -0
- data/lib/tensorflow/core/protobuf/transport_options_pb.rb +16 -0
- data/lib/tensorflow/core/protobuf/verifier_config_pb.rb +23 -0
- data/lib/tensorflow/core/protobuf/worker_pb.rb +246 -0
- data/lib/tensorflow/core/protobuf/worker_service_pb.rb +15 -0
- data/lib/tensorflow/core/util/event_pb.rb +93 -0
- data/lib/tensorflow/core/util/memmapped_file_system_pb.rb +22 -0
- data/lib/tensorflow/core/util/saved_tensor_slice_pb.rb +40 -0
- data/lib/tensorflow/data/batch_dataset.rb +18 -0
- data/lib/tensorflow/data/dataset.rb +106 -0
- data/lib/tensorflow/data/fixed_length_record_dataset.rb +27 -0
- data/lib/tensorflow/data/iterator.rb +76 -0
- data/lib/tensorflow/data/map_dataset.rb +17 -0
- data/lib/tensorflow/data/repeat_dataset.rb +16 -0
- data/lib/tensorflow/data/shuffle_dataset.rb +23 -0
- data/lib/tensorflow/data/tensor_dataset.rb +19 -0
- data/lib/tensorflow/data/tensor_slice_dataset.rb +15 -0
- data/lib/tensorflow/data/tf_record_dataset.rb +18 -0
- data/lib/tensorflow/data/zip_dataset.rb +24 -0
- data/lib/tensorflow/decorators.rb +53 -0
- data/lib/tensorflow/eager/context.rb +120 -0
- data/lib/tensorflow/eager/operation.rb +219 -0
- data/lib/tensorflow/eager/tensor_handle.rb +87 -0
- data/lib/tensorflow/error.rb +54 -0
- data/lib/tensorflow/execution_context.rb +62 -0
- data/lib/tensorflow/extensions/arg_def.rb +58 -0
- data/lib/tensorflow/extensions/array.rb +17 -0
- data/lib/tensorflow/extensions/boolean.rb +25 -0
- data/lib/tensorflow/extensions/narray.rb +7 -0
- data/lib/tensorflow/ffi.rb +291 -0
- data/lib/tensorflow/graph/function.rb +33 -0
- data/lib/tensorflow/graph/function_def.rb +62 -0
- data/lib/tensorflow/graph/gradients.rb +120 -0
- data/lib/tensorflow/graph/graph.rb +252 -0
- data/lib/tensorflow/graph/graph_def_options.rb +24 -0
- data/lib/tensorflow/graph/graph_keys.rb +50 -0
- data/lib/tensorflow/graph/operation.rb +176 -0
- data/lib/tensorflow/graph/operation_attr.rb +153 -0
- data/lib/tensorflow/graph/operation_description.rb +255 -0
- data/lib/tensorflow/graph/operation_output.rb +49 -0
- data/lib/tensorflow/graph/session.rb +156 -0
- data/lib/tensorflow/keras/datasets/boston_housing.rb +32 -0
- data/lib/tensorflow/keras/datasets/cifar10.rb +11 -0
- data/lib/tensorflow/keras/datasets/cifar100.rb +11 -0
- data/lib/tensorflow/keras/datasets/fashion_mnist.rb +44 -0
- data/lib/tensorflow/keras/datasets/imdb.rb +30 -0
- data/lib/tensorflow/keras/datasets/mnist.rb +18 -0
- data/lib/tensorflow/keras/datasets/reuters.rb +28 -0
- data/lib/tensorflow/keras/layers/conv.rb +14 -0
- data/lib/tensorflow/keras/layers/conv2d.rb +11 -0
- data/lib/tensorflow/keras/layers/dense.rb +68 -0
- data/lib/tensorflow/keras/layers/dropout.rb +27 -0
- data/lib/tensorflow/keras/layers/flatten.rb +25 -0
- data/lib/tensorflow/keras/losses/sparse_categorical_crossentropy.rb +14 -0
- data/lib/tensorflow/keras/metrics/mean.rb +30 -0
- data/lib/tensorflow/keras/metrics/sparse_categorical_accuracy.rb +17 -0
- data/lib/tensorflow/keras/model.rb +6 -0
- data/lib/tensorflow/keras/models/sequential.rb +56 -0
- data/lib/tensorflow/keras/optimizers/adam.rb +8 -0
- data/lib/tensorflow/keras/preprocessing/image.rb +22 -0
- data/lib/tensorflow/keras/utils.rb +83 -0
- data/lib/tensorflow/name_scope.rb +57 -0
- data/lib/tensorflow/op_def_builder.rb +49 -0
- data/lib/tensorflow/ops/audio.rb +13 -0
- data/lib/tensorflow/ops/bitwise.rb +29 -0
- data/lib/tensorflow/ops/control.rb +13 -0
- data/lib/tensorflow/ops/gradients.rb +21 -0
- data/lib/tensorflow/ops/image.rb +218 -0
- data/lib/tensorflow/ops/io.rb +123 -0
- data/lib/tensorflow/ops/linalg.rb +131 -0
- data/lib/tensorflow/ops/math.rb +493 -0
- data/lib/tensorflow/ops/nn.rb +286 -0
- data/lib/tensorflow/ops/operators.rb +31 -0
- data/lib/tensorflow/ops/ops.rb +102 -0
- data/lib/tensorflow/ops/random.rb +18 -0
- data/lib/tensorflow/ops/raw_ops.rb +5179 -0
- data/lib/tensorflow/ops/raw_ops.rb.erb +38 -0
- data/lib/tensorflow/printers/graph.erb +80 -0
- data/lib/tensorflow/printers/graph.rb +26 -0
- data/lib/tensorflow/printers/graph_def.erb +109 -0
- data/lib/tensorflow/printers/graph_def.rb +26 -0
- data/lib/tensorflow/python_compatiblity.rb +55 -0
- data/lib/tensorflow/resource_summary_writer.rb +78 -0
- data/lib/tensorflow/status.rb +49 -0
- data/lib/tensorflow/stream_executor/dnn_pb.rb +90 -0
- data/lib/tensorflow/strings.rb +100 -0
- data/lib/tensorflow/summary.rb +13 -0
- data/lib/tensorflow/tensor.rb +133 -0
- data/lib/tensorflow/tensor_data.rb +310 -0
- data/lib/tensorflow/tensor_mixin.rb +32 -0
- data/lib/tensorflow/tensor_spec.rb +10 -0
- data/lib/tensorflow/tensorflow/core/util/event_pb.rb +93 -0
- data/lib/tensorflow/train/gradient_descent_optimizer.rb +33 -0
- data/lib/tensorflow/train/optimizer.rb +158 -0
- data/lib/tensorflow/type_spec.rb +4 -0
- data/lib/tensorflow/variable.rb +127 -0
- data/lib/tensorflow/version.rb +3 -0
- metadata +308 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 29ce3a9ced9660aa6b78013f05d631397a41fd060dc22485c170f1f3501e0ce8
|
4
|
+
data.tar.gz: 92130ad0bd4628b2604409c7f4c215fc645233f27b36bea4fc1d00efedd06959
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3649ed6ff6b7e1f5ab24387ea864392d0956bb0991549b7ced5c76ca224bd9fd82b8b82e4e34aa129e09a04ad3eb775f9db61a983be202cd65f09afe311e0b3c
|
7
|
+
data.tar.gz: 165b341e62aec2ce1c82ba0bb1105e8061bf5d138f8ccb757f04ae5584841fef1d9056d48bb4e398ae965e11fa905db4376ba1909214ae7031ea68d8f33ec05e
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
0.2.0 (2019-12-02)
|
2
|
+
====================
|
3
|
+
* Implementation of gradient descent
|
4
|
+
* Support for training models
|
5
|
+
* Linear regression example
|
6
|
+
* MNIST example
|
7
|
+
* Tensorboard support
|
8
|
+
|
9
|
+
0.1.0 (2019-11-04)
|
10
|
+
====================
|
11
|
+
* Ability to call all TensorFlow operations
|
12
|
+
* Eager execution mode
|
13
|
+
* Graph execution mode
|
14
|
+
* Importing and exporting computation graphs
|
15
|
+
* Creating and editing computation graphs
|
16
|
+
* Creating custom operators
|
17
|
+
* Gradients
|
18
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2019 Andrew Kane, Charlie Savage
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
# Tensorflow
|
2
|
+
|
3
|
+
Ruby bindings for [Tensorflow](https://github.com/tensorflow/tensorflow).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
First you'll need to install Tensorflow 2. You can either do a full [installation](https://www.tensorflow.org/install) or just install [Tensorflow for C](https://www.tensorflow.org/install/lang_c). In both cases, you'll need to make
|
8
|
+
sure the tensorflow library is on the system PATH so the Ruby bindings can load it.
|
9
|
+
|
10
|
+
Next install the gem:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem install 'tensorflow-ruby'
|
14
|
+
```
|
15
|
+
|
16
|
+
## Overview
|
17
|
+
|
18
|
+
The Ruby bindings are on built on top of TensorFlow's [C API] (https://github.com/tensorflow/docs/blob/master/site/en/r1/guide/extend/bindings.md)
|
19
|
+
which provides access to some, but not all, of TensorFlow's functionality. The Ruby bindings
|
20
|
+
are designed to mimic the [Python API](https://www.tensorflow.org/versions/r2.0/api_docs/python/tf). Just like the
|
21
|
+
Python bindings, Ruby defaults to [eager](https://www.tensorflow.org/guide/eager) execution mode.
|
22
|
+
|
23
|
+
## Current Status
|
24
|
+
|
25
|
+
Feature | Status
|
26
|
+
:--------------------------------------------- | :---------------------------------------------------------- |
|
27
|
+
Run a predefined Graph | Implemented |
|
28
|
+
Graph construction with generated op functions | Implemented |
|
29
|
+
Gradients | Implemented |
|
30
|
+
Functions | Implemented |
|
31
|
+
Trainers | Gradient Descent Implemented |
|
32
|
+
Data API | 80% Implemented |
|
33
|
+
Tensorboard Support | Implemented |
|
34
|
+
Control Flow | Not Implemented |
|
35
|
+
Gradient Tape | Not Implemented |
|
36
|
+
Keras | Not Implemented |
|
37
|
+
|
38
|
+
## Getting Help
|
39
|
+
To learn how to use the library:
|
40
|
+
* Read this file
|
41
|
+
* Take a look at the comprehensive test suite - it will show you examples of using all the binding's functionality
|
42
|
+
* Review the gem documentation
|
43
|
+
* Review the [Python API](https://www.tensorflow.org/versions/r2.0/api_docs/python/tf) documentation
|
44
|
+
* Take a look at the models directory for example implementations (linear regression, mnist neural network, etc.)
|
45
|
+
|
46
|
+
## Using tensorflow-ruby
|
47
|
+
|
48
|
+
### Constants
|
49
|
+
Constants are
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
a = Tf.constant([1, 2, 3])
|
53
|
+
b = Tf.constant([4, 5, 6])
|
54
|
+
a + b
|
55
|
+
```
|
56
|
+
|
57
|
+
### Variables
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
v = Tf::Variable.new(0.0)
|
61
|
+
w = v + 1
|
62
|
+
```
|
63
|
+
|
64
|
+
### Math
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
Tf::Math.abs([-1, -2])
|
68
|
+
Tf::Math.sqrt([1.0, 4.0, 9.0])
|
69
|
+
```
|
70
|
+
|
71
|
+
### Data::Dataset
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
# load
|
75
|
+
train_dataset = Tf::Data::Dataset.from_tensor_slices([train_examples, train_labels])
|
76
|
+
test_dataset = Tf::Data::Dataset.from_tensor_slices([test_examples, test_labels])
|
77
|
+
|
78
|
+
# shuffle and batch
|
79
|
+
train_dataset = train_dataset.shuffle(100).batch(32)
|
80
|
+
test_dataset = test_dataset.batch(32)
|
81
|
+
|
82
|
+
# iterate
|
83
|
+
train_dataset.each do |examples, labels|
|
84
|
+
# ...
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
## Contributing
|
89
|
+
|
90
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
91
|
+
|
92
|
+
- [Report bugs](https://github.com/cfis/tensorflow-ruby/issues)
|
93
|
+
- Fix bugs and [submit pull requests](https://github.com/cfis/tensorflow-ruby/pulls)
|
94
|
+
- Write, clarify, or fix documentation
|
95
|
+
- Suggest or add new features
|
96
|
+
|
97
|
+
To get started with development and testing:
|
98
|
+
|
99
|
+
```sh
|
100
|
+
git clone https://github.com/cfis/tensorflow-ruby.git
|
101
|
+
cd tensorflow-ruby
|
102
|
+
bundle install
|
103
|
+
rake test
|
104
|
+
```
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'tmpdir'
|
3
|
+
require_relative './resource'
|
4
|
+
|
5
|
+
module Tensorflow
|
6
|
+
module Datasets
|
7
|
+
class DownloadManager
|
8
|
+
attr_reader :uri, :dir
|
9
|
+
|
10
|
+
def initialize(dir = Dir.tmpdir)
|
11
|
+
@dir = dir
|
12
|
+
end
|
13
|
+
|
14
|
+
def download(urls)
|
15
|
+
resources = Array(urls).flatten.map {|url| Resource.new(url)}
|
16
|
+
resources.each do |resource|
|
17
|
+
self.download_resource(resource)
|
18
|
+
end
|
19
|
+
resources
|
20
|
+
end
|
21
|
+
|
22
|
+
def download_resource(resource)
|
23
|
+
resource.path = File.join(self.dir, resource.filename)
|
24
|
+
return if File.exist?(resource.path)
|
25
|
+
|
26
|
+
STDOUT << "Downloading #{resource.uri}" << "\n"
|
27
|
+
http = Net::HTTP.new(resource.uri.host, resource.uri.port)
|
28
|
+
http.use_ssl = resource.uri.is_a?(URI::HTTPS)
|
29
|
+
|
30
|
+
request = Net::HTTP::Get.new(resource.uri.request_uri)
|
31
|
+
|
32
|
+
http.start do |http|
|
33
|
+
http.request(request) do |response|
|
34
|
+
file_size = response['content-length'].to_f
|
35
|
+
bytes = 0
|
36
|
+
|
37
|
+
File.open(resource.path, 'wb') do |file|
|
38
|
+
response.read_body do |chunk|
|
39
|
+
file.write(chunk)
|
40
|
+
bytes += chunk.size
|
41
|
+
#STDOUT << "#{(bytes / file_size * 100).to_i}%\r"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../../../lib', __dir__))
|
2
|
+
|
3
|
+
require 'tensorflow'
|
4
|
+
|
5
|
+
require_relative '../download_manager'
|
6
|
+
|
7
|
+
module Tensorflow
|
8
|
+
module Datasets
|
9
|
+
module Images
|
10
|
+
class Mnist
|
11
|
+
extend Decorator
|
12
|
+
|
13
|
+
BASE_URL = 'https://storage.googleapis.com/cvdf-datasets/mnist'
|
14
|
+
|
15
|
+
@tf.function(input_signature=[[:string]])
|
16
|
+
def decode_image(image)
|
17
|
+
image = IO.decode_raw(image, Tf.uint8)
|
18
|
+
image = Tf.cast(image, Tf.float32)
|
19
|
+
image = Tf.reshape(image, [784])
|
20
|
+
# Normalize from [0, 255] to [0.0, 1.0]
|
21
|
+
image / 255.0
|
22
|
+
end
|
23
|
+
|
24
|
+
@tf.function(input_signature=[[:string]])
|
25
|
+
def decode_label(label)
|
26
|
+
# tf.string -> [Tf.uint8]
|
27
|
+
label = Tf::IO.decode_raw(label, Tf.uint8)
|
28
|
+
label = Tf.reshape(label, []) # label is a scalar
|
29
|
+
Tf.cast(label, Tf.int32)
|
30
|
+
end
|
31
|
+
|
32
|
+
def dataset(images_file, labels_file)
|
33
|
+
download_manager = Datasets::DownloadManager.new
|
34
|
+
urls = ["#{BASE_URL}/#{images_file}",
|
35
|
+
"#{BASE_URL}/#{labels_file}"]
|
36
|
+
|
37
|
+
resources = download_manager.download(urls)
|
38
|
+
|
39
|
+
images = Data::FixedLengthRecordDataset.new(resources.first.path, 28 * 28, header_bytes: 16, compression_type: 'GZIP').map_func(self.decode_image)
|
40
|
+
labels = Data::FixedLengthRecordDataset.new(resources.last.path, 1, header_bytes: 8, compression_type: 'GZIP').map_func(self.decode_label)
|
41
|
+
Data::ZipDataset.new(images, labels)
|
42
|
+
end
|
43
|
+
|
44
|
+
def train
|
45
|
+
dataset('train-images-idx3-ubyte.gz', 'train-labels-idx1-ubyte.gz')
|
46
|
+
end
|
47
|
+
|
48
|
+
def test
|
49
|
+
dataset('t10k-images-idx3-ubyte.gz', 't10k-labels-idx1-ubyte.gz')
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module Tensorflow
|
4
|
+
module Datasets
|
5
|
+
class Resource
|
6
|
+
attr_reader :uri
|
7
|
+
attr_accessor :path
|
8
|
+
|
9
|
+
def initialize(url)
|
10
|
+
@uri = url.is_a?(URI) ? url : URI.parse(url)
|
11
|
+
end
|
12
|
+
|
13
|
+
def filename
|
14
|
+
File.basename(self.uri.path)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,182 @@
|
|
1
|
+
# dependencies
|
2
|
+
require "ffi"
|
3
|
+
require "npy"
|
4
|
+
require "numo/narray"
|
5
|
+
|
6
|
+
# stdlib
|
7
|
+
require "digest"
|
8
|
+
require "fileutils"
|
9
|
+
require "forwardable"
|
10
|
+
require "json"
|
11
|
+
require "net/http"
|
12
|
+
require "tempfile"
|
13
|
+
require "zlib"
|
14
|
+
|
15
|
+
# Load protobufs. Is this a good idea to require all ruby files in those directories?
|
16
|
+
Dir[File.join(__dir__, 'tensorflow', 'core', 'lib', 'core', '*.rb')].each { |file| require file }
|
17
|
+
Dir[File.join(__dir__, 'tensorflow', 'core', 'stream_executor', '*.rb')].each { |file| require file }
|
18
|
+
Dir[File.join(__dir__, 'tensorflow', 'core', 'framework', '*.rb')].each { |file| require file }
|
19
|
+
#Dir[File.join(__dir__, 'tensorflow', 'core', 'protobuf', '*.rb')].each { |file| require file }
|
20
|
+
Dir[File.join(__dir__, 'tensorflow', 'core', 'util', '*.rb')].each { |file| require file }
|
21
|
+
|
22
|
+
# Ops
|
23
|
+
require "tensorflow/ops/audio"
|
24
|
+
require "tensorflow/ops/bitwise"
|
25
|
+
require "tensorflow/ops/control"
|
26
|
+
require "tensorflow/ops/image"
|
27
|
+
require "tensorflow/ops/io"
|
28
|
+
require "tensorflow/ops/linalg"
|
29
|
+
require "tensorflow/ops/math"
|
30
|
+
require "tensorflow/ops/nn"
|
31
|
+
require "tensorflow/ops/operators"
|
32
|
+
require "tensorflow/ops/ops"
|
33
|
+
require "tensorflow/ops/random"
|
34
|
+
require "tensorflow/ops/raw_ops"
|
35
|
+
|
36
|
+
# Core
|
37
|
+
require "tensorflow/ffi"
|
38
|
+
require "tensorflow/decorators"
|
39
|
+
require "tensorflow/error"
|
40
|
+
require "tensorflow/execution_context"
|
41
|
+
require "tensorflow/name_scope"
|
42
|
+
require "tensorflow/op_def_builder"
|
43
|
+
require "tensorflow/status"
|
44
|
+
require "tensorflow/strings"
|
45
|
+
require "tensorflow/tensor_mixin"
|
46
|
+
require "tensorflow/tensor_data"
|
47
|
+
require "tensorflow/tensor"
|
48
|
+
require "tensorflow/variable"
|
49
|
+
require "tensorflow/version"
|
50
|
+
|
51
|
+
# Extensions
|
52
|
+
require "tensorflow/extensions/arg_def.rb"
|
53
|
+
require "tensorflow/extensions/boolean.rb"
|
54
|
+
require "tensorflow/extensions/narray.rb"
|
55
|
+
|
56
|
+
# Printers
|
57
|
+
require "tensorflow/printers/graph"
|
58
|
+
|
59
|
+
# Eager
|
60
|
+
require "tensorflow/eager/context"
|
61
|
+
require "tensorflow/eager/operation"
|
62
|
+
require "tensorflow/eager/tensor_handle"
|
63
|
+
|
64
|
+
# graph
|
65
|
+
require "tensorflow/graph/function"
|
66
|
+
require "tensorflow/graph/function_def"
|
67
|
+
require "tensorflow/graph/gradients"
|
68
|
+
require "tensorflow/graph/graph"
|
69
|
+
require "tensorflow/graph/graph_def_options"
|
70
|
+
require "tensorflow/graph/graph_keys"
|
71
|
+
require "tensorflow/graph/operation"
|
72
|
+
require "tensorflow/graph/operation_output"
|
73
|
+
require "tensorflow/graph/operation_attr"
|
74
|
+
require "tensorflow/graph/operation_description"
|
75
|
+
require "tensorflow/graph/session"
|
76
|
+
|
77
|
+
# Ugly - now require the op gradients
|
78
|
+
require "tensorflow/ops/gradients"
|
79
|
+
|
80
|
+
|
81
|
+
# specs
|
82
|
+
require "tensorflow/type_spec"
|
83
|
+
require "tensorflow/batchable_type_spec"
|
84
|
+
require "tensorflow/tensor_spec"
|
85
|
+
|
86
|
+
# Training
|
87
|
+
require "tensorflow/train/optimizer"
|
88
|
+
require "tensorflow/train/gradient_descent_optimizer"
|
89
|
+
|
90
|
+
# Data
|
91
|
+
require "tensorflow/data/dataset"
|
92
|
+
require "tensorflow/data/batch_dataset"
|
93
|
+
require "tensorflow/data/fixed_length_record_dataset"
|
94
|
+
require "tensorflow/data/iterator"
|
95
|
+
require "tensorflow/data/map_dataset"
|
96
|
+
require "tensorflow/data/repeat_dataset"
|
97
|
+
require "tensorflow/data/shuffle_dataset"
|
98
|
+
require "tensorflow/data/tensor_dataset"
|
99
|
+
require "tensorflow/data/tensor_slice_dataset"
|
100
|
+
require "tensorflow/data/tf_record_dataset"
|
101
|
+
require "tensorflow/data/zip_dataset"
|
102
|
+
|
103
|
+
# Tensorboard
|
104
|
+
require "tensorflow/summary"
|
105
|
+
require "tensorflow/resource_summary_writer"
|
106
|
+
|
107
|
+
# keras
|
108
|
+
require "tensorflow/keras/utils"
|
109
|
+
require "tensorflow/keras/datasets/boston_housing"
|
110
|
+
require "tensorflow/keras/datasets/cifar10"
|
111
|
+
require "tensorflow/keras/datasets/cifar100"
|
112
|
+
require "tensorflow/keras/datasets/fashion_mnist"
|
113
|
+
require "tensorflow/keras/datasets/imdb"
|
114
|
+
require "tensorflow/keras/datasets/mnist"
|
115
|
+
require "tensorflow/keras/datasets/reuters"
|
116
|
+
require "tensorflow/keras/layers/conv"
|
117
|
+
require "tensorflow/keras/layers/conv2d"
|
118
|
+
require "tensorflow/keras/layers/dense"
|
119
|
+
require "tensorflow/keras/layers/dropout"
|
120
|
+
require "tensorflow/keras/layers/flatten"
|
121
|
+
require "tensorflow/keras/losses/sparse_categorical_crossentropy"
|
122
|
+
require "tensorflow/keras/metrics/mean"
|
123
|
+
require "tensorflow/keras/metrics/sparse_categorical_accuracy"
|
124
|
+
require "tensorflow/keras/model"
|
125
|
+
require "tensorflow/keras/models/sequential"
|
126
|
+
require "tensorflow/keras/optimizers/adam"
|
127
|
+
require "tensorflow/keras/preprocessing/image"
|
128
|
+
|
129
|
+
require 'tensorflow/python_compatiblity'
|
130
|
+
|
131
|
+
module Tensorflow
|
132
|
+
extend Ops
|
133
|
+
|
134
|
+
GRAPH_MODE = 0
|
135
|
+
EAGER_MODE = 1
|
136
|
+
|
137
|
+
class << self
|
138
|
+
attr_accessor :ffi_lib
|
139
|
+
|
140
|
+
def library_version
|
141
|
+
FFI.TF_Version
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
# friendlier error message
|
146
|
+
autoload :FFI, "tensorflow/ffi"
|
147
|
+
|
148
|
+
def self.op_defs
|
149
|
+
buffer = FFI.TF_GetAllOpList
|
150
|
+
string = buffer[:data].read_string(buffer[:length])
|
151
|
+
ops = OpList.decode(string)
|
152
|
+
ops.op.each_with_object(Hash.new) do |op_def, hash|
|
153
|
+
hash[op_def.name] = op_def
|
154
|
+
end
|
155
|
+
ensure
|
156
|
+
FFI.TF_DeleteBuffer(buffer)
|
157
|
+
end
|
158
|
+
|
159
|
+
def self.op_def(op_name)
|
160
|
+
self.op_defs[op_name]
|
161
|
+
end
|
162
|
+
|
163
|
+
def self.execution_mode
|
164
|
+
@mode ||= Tensorflow::EAGER_MODE
|
165
|
+
end
|
166
|
+
|
167
|
+
def self.execution_mode=(value)
|
168
|
+
@mode = value
|
169
|
+
end
|
170
|
+
|
171
|
+
def self.name_scope(base_name, &block)
|
172
|
+
ExecutionContext.current.name_scope(base_name, &block)
|
173
|
+
end
|
174
|
+
|
175
|
+
extend PythonCompatability
|
176
|
+
class << self
|
177
|
+
extend Forwardable
|
178
|
+
def_delegators Linalg, *Linalg.singleton_methods
|
179
|
+
def_delegators Math, *Math.singleton_methods
|
180
|
+
def_delegators NN, *NN.singleton_methods
|
181
|
+
end
|
182
|
+
end
|