tensorflow 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 509b05a4a14e0bbb314c682f4172959b264e6709f92afc0fa0a749afccfe233b
4
- data.tar.gz: daa060a1cb5c2602a652d87b75986a78a0d440c631b5fd7363e822363339f0ca
3
+ metadata.gz: eace7d05756f006a0710499d67eeb878234a5c67fafb877aa5e154f7f58a0b0b
4
+ data.tar.gz: 87f168e411872d952562550b6dfd73ed902c989b5d54df241198732f7a518ae5
5
5
  SHA512:
6
- metadata.gz: '0755950b3b9865fe17a60efa970c23afcbbba33250b78e78bed7ddb907d90ecdfe64ffdd3fc48533ab6a30fbee6a64dbe6fd4d30e8554ca5c3f9535ea46e1daa'
7
- data.tar.gz: 7307f84476cf789486a4f21328056a5abc5485a9dadeff99216dfa12f7a68c2b24f410fdb7caaca1c90a43d3497e03bb9459dbb876543819dce5e210afd02e3a
6
+ metadata.gz: cc67f8f76b77adc109f85c86aba641e71e6a52f60f7de9a0c983af9bd8a1ada5dc989ebfc8ae40b75da2957d24c07716385d6d949d1df7dc5fd88d4e7bb1172c
7
+ data.tar.gz: 545a0fb338134f3cd1088d6467cbaa3ebba2465a845bdb91ada369cbc594f5e63108d20a6d6d5e4884885a42b915483fce8414e0ae1ec27b0833b9861a176fe1
@@ -1,6 +1,13 @@
1
+ ## 0.1.2
2
+
3
+ - Added `Audio`, `Bitwise`, `Image`, `IO`, `Linalg`, `NN`, `Strings`, and `Data::Dataset` modules
4
+ - Added `numo` method to tensors
5
+ - Added Boston housing dataset
6
+ - Fixed string encoding
7
+
1
8
  ## 0.1.1
2
9
 
3
- - Added more operations
10
+ - Added `Math` and `RawOps` modules
4
11
  - Added support for more data types
5
12
  - Added MNIST dataset
6
13
 
data/README.md CHANGED
@@ -39,6 +39,13 @@ v = Tf::Variable.new(0.0)
39
39
  w = v + 1
40
40
  ```
41
41
 
42
+ ## Math
43
+
44
+ ```ruby
45
+ Tf::Math.abs(-2)
46
+ Tf::Math.sqrt(4)
47
+ ```
48
+
42
49
  ## FizzBuzz
43
50
 
44
51
  ```ruby
@@ -60,9 +67,24 @@ end
60
67
  fizzbuzz(15)
61
68
  ```
62
69
 
63
- ## Keras
70
+ ## Data::Dataset
71
+
72
+ ```ruby
73
+ # load
74
+ train_dataset = Tf::Data::Dataset.from_tensor_slices([train_examples, train_labels])
75
+ test_dataset = Tf::Data::Dataset.from_tensor_slices([test_examples, test_labels])
76
+
77
+ # shuffle and batch
78
+ train_dataset = train_dataset.shuffle(100).batch(32)
79
+ test_dataset = test_dataset.batch(32)
64
80
 
65
- Coming soon
81
+ # iterate
82
+ train_dataset.each do |examples, labels|
83
+ # ...
84
+ end
85
+ ```
86
+
87
+ ## Keras [coming soon]
66
88
 
67
89
  ```ruby
68
90
  mnist = Tf::Keras::Datasets::MNIST
@@ -114,3 +136,12 @@ Everyone is encouraged to help improve this project. Here are a few ways you can
114
136
  - Fix bugs and [submit pull requests](https://github.com/ankane/tensorflow/pulls)
115
137
  - Write, clarify, or fix documentation
116
138
  - Suggest or add new features
139
+
140
+ To get started with development and testing:
141
+
142
+ ```sh
143
+ git clone https://github.com/ankane/tensorflow.git
144
+ cd tensorflow
145
+ bundle install
146
+ rake test
147
+ ```
@@ -1,29 +1,54 @@
1
1
  # dependencies
2
2
  require "ffi"
3
3
  require "npy"
4
+ require "numo/narray"
4
5
 
5
6
  # stdlib
7
+ require "digest"
6
8
  require "fileutils"
7
9
  require "forwardable"
10
+ require "json"
8
11
  require "net/http"
9
12
  require "tempfile"
13
+ require "zlib"
10
14
 
11
15
  # modules
12
- require "tensorflow/utils"
16
+ require "tensorflow/audio"
17
+ require "tensorflow/bitwise"
13
18
  require "tensorflow/context"
19
+ require "tensorflow/image"
20
+ require "tensorflow/io"
21
+ require "tensorflow/linalg"
14
22
  require "tensorflow/math"
23
+ require "tensorflow/nn"
15
24
  require "tensorflow/ops"
16
25
  require "tensorflow/raw_ops"
26
+ require "tensorflow/strings"
17
27
  require "tensorflow/tensor"
28
+ require "tensorflow/utils"
18
29
  require "tensorflow/variable"
19
30
  require "tensorflow/version"
20
31
 
32
+ # data
33
+ require "tensorflow/data/dataset"
34
+ require "tensorflow/data/batch_dataset"
35
+ require "tensorflow/data/shuffle_dataset"
36
+ require "tensorflow/data/tensor_slice_dataset"
37
+
21
38
  # keras
39
+ require "tensorflow/keras/datasets/boston_housing"
40
+ require "tensorflow/keras/datasets/cifar10"
41
+ require "tensorflow/keras/datasets/cifar100"
42
+ require "tensorflow/keras/datasets/fashion_mnist"
43
+ require "tensorflow/keras/datasets/imdb"
22
44
  require "tensorflow/keras/datasets/mnist"
45
+ require "tensorflow/keras/datasets/reuters"
23
46
  require "tensorflow/keras/layers/dense"
24
47
  require "tensorflow/keras/layers/dropout"
25
48
  require "tensorflow/keras/layers/flatten"
49
+ require "tensorflow/keras/metrics/mean"
26
50
  require "tensorflow/keras/models/sequential"
51
+ require "tensorflow/keras/utils"
27
52
 
28
53
  module TensorFlow
29
54
  class Error < StandardError; end
@@ -41,7 +66,9 @@ module TensorFlow
41
66
  include Utils
42
67
 
43
68
  extend Forwardable
69
+ def_delegators Linalg, :eye, :matmul
44
70
  def_delegators Math, :abs, :acos, :acosh, :add, :add_n, :argmax, :argmin, :asin, :asinh, :atan, :atan2, :atanh, :cos, :cosh, :cumsum, :divide, :equal, :exp, :floor, :greater, :greater_equal, :less, :less_equal, :logical_and, :logical_not, :logical_or, :maximum, :minimum, :multiply, :negative, :not_equal, :pow, :reduce_all, :reduce_any, :reduce_logsumexp, :reduce_max, :reduce_mean, :reduce_min, :reduce_prod, :reduce_sum, :round, :scalar_mul, :sigmoid, :sign, :sin, :sinh, :sqrt, :square, :subtract, :tan, :tanh, :truediv
71
+ def_delegators NN, :space_to_batch
45
72
 
46
73
  def library_version
47
74
  FFI.TF_Version
@@ -0,0 +1,13 @@
1
+ module TensorFlow
2
+ module Audio
3
+ class << self
4
+ def decode_wav(contents, desired_channels: -1, desired_samples: -1)
5
+ RawOps.decode_wav(contents: contents, desired_channels: desired_channels, desired_samples: desired_samples)
6
+ end
7
+
8
+ def encode_wav(audio, sample_rate)
9
+ RawOps.encode_wav(audio: audio, sample_rate: sample_rate)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ module TensorFlow
2
+ module Bitwise
3
+ class << self
4
+ def bitwise_and(x, y)
5
+ RawOps.bitwise_and(x: x, y: y)
6
+ end
7
+
8
+ def bitwise_or(x, y)
9
+ RawOps.bitwise_or(x: x, y: y)
10
+ end
11
+
12
+ def bitwise_xor(x, y)
13
+ RawOps.bitwise_xor(x: x, y: y)
14
+ end
15
+
16
+ def invert(x)
17
+ RawOps.invert(x: x)
18
+ end
19
+
20
+ def left_shift(x, y)
21
+ RawOps.left_shift(x: x, y: y)
22
+ end
23
+
24
+ def right_shift(x, y)
25
+ RawOps.right_shift(x: x, y: y)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ module TensorFlow
2
+ module Data
3
+ class BatchDataset < Dataset
4
+ def initialize(input_dataset, batch_size, drop_remainder)
5
+ @input_dataset = input_dataset # keep reference for memory
6
+ @output_types = input_dataset.output_types
7
+ @output_shapes = input_dataset.output_shapes.map { |s| [batch_size] + s }
8
+
9
+ variant_tensor = RawOps.batch_dataset_v2(
10
+ input_dataset: input_dataset,
11
+ batch_size: TensorFlow.convert_to_tensor(batch_size, dtype: :int64),
12
+ drop_remainder: drop_remainder,
13
+ output_types: @output_types,
14
+ output_shapes: @output_shapes
15
+ )
16
+ super(variant_tensor)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,46 @@
1
+ module TensorFlow
2
+ module Data
3
+ class Dataset
4
+ include Enumerable
5
+
6
+ # TODO remove
7
+ attr_reader :output_types, :output_shapes
8
+
9
+ def initialize(variant_tensor)
10
+ @variant_tensor = variant_tensor
11
+ end
12
+
13
+ def batch(batch_size, drop_remainder: false)
14
+ BatchDataset.new(self, batch_size, drop_remainder)
15
+ end
16
+
17
+ def shuffle(buffer_size)
18
+ ShuffleDataset.new(self, buffer_size)
19
+ end
20
+
21
+ def self.from_tensor_slices(tensors)
22
+ TensorSliceDataset.new(tensors)
23
+ end
24
+
25
+ def to_ptr
26
+ @variant_tensor.to_ptr
27
+ end
28
+
29
+ def each
30
+ iterator, deleter = RawOps.anonymous_iterator_v2(output_types: @output_types, output_shapes: @output_shapes)
31
+ RawOps.make_iterator(dataset: @variant_tensor, iterator: iterator)
32
+ begin
33
+ loop do
34
+ values = RawOps.iterator_get_next_sync(iterator: iterator, output_types: @output_types, output_shapes: @output_shapes)
35
+ yield values
36
+ end
37
+ rescue Error => e
38
+ # iterate until end of sequence error
39
+ raise e unless e.message == "End of sequence"
40
+ end
41
+ ensure
42
+ RawOps.delete_iterator(handle: iterator, deleter: deleter) if iterator
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,21 @@
1
+ module TensorFlow
2
+ module Data
3
+ class ShuffleDataset < Dataset
4
+ def initialize(input_dataset, buffer_size)
5
+ @input_dataset = input_dataset # keep reference for memory
6
+ @output_types = input_dataset.output_types
7
+ @output_shapes = input_dataset.output_shapes
8
+
9
+ variant_tensor = RawOps.shuffle_dataset(
10
+ input_dataset: input_dataset,
11
+ buffer_size: TensorFlow.convert_to_tensor(buffer_size, dtype: :int64),
12
+ seed: TensorFlow.convert_to_tensor(0, dtype: :int64),
13
+ seed2: TensorFlow.convert_to_tensor(0, dtype: :int64),
14
+ output_types: @output_types,
15
+ output_shapes: @output_shapes
16
+ )
17
+ super(variant_tensor)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ module TensorFlow
2
+ module Data
3
+ class TensorSliceDataset < Dataset
4
+ def initialize(element)
5
+ tensors = Utils.to_tensor_array(element)
6
+ @tensors = tensors # keep reference for memory
7
+ @output_types = tensors.map(&:dtype)
8
+ @output_shapes = tensors.map { |t| t.shape[1..-1] }
9
+
10
+ variant_tensor = RawOps.tensor_slice_dataset(components: tensors, output_shapes: @output_shapes)
11
+ super(variant_tensor)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -35,6 +35,10 @@ module TensorFlow
35
35
  attach_function :TF_NewTensor, %i[int pointer int pointer size_t pointer pointer], :pointer
36
36
  attach_function :TF_DeleteTensor, %i[pointer], :void
37
37
  attach_function :TF_TensorData, %i[pointer], :pointer
38
+ attach_function :TF_TensorByteSize, %i[pointer], :size_t
39
+ attach_function :TF_StringEncode, %i[pointer size_t pointer size_t pointer], :size_t
40
+ attach_function :TF_StringDecode, %i[pointer size_t pointer pointer pointer], :size_t
41
+ attach_function :TF_StringEncodedSize, %i[size_t], :size_t
38
42
 
39
43
  # https://github.com/tensorflow/tensorflow/blob/master/tensorflow/c/eager/c_api.h
40
44
  attach_function :TFE_NewContextOptions, %i[], :pointer
@@ -58,10 +62,18 @@ module TensorFlow
58
62
  attach_function :TFE_OpSetDevice, %i[pointer string pointer], :pointer
59
63
  attach_function :TFE_OpGetDevice, %i[pointer pointer], :string
60
64
  attach_function :TFE_OpAddInput, %i[pointer pointer pointer], :void
65
+ attach_function :TFE_OpAddInputList, %i[pointer pointer int pointer], :void
61
66
  attach_function :TFE_OpGetAttrType, %i[pointer string pointer pointer], :int
62
67
  attach_function :TFE_OpSetAttrString, %i[pointer string pointer size_t], :void
68
+ attach_function :TFE_OpSetAttrInt, %i[pointer string int64_t], :void
69
+ attach_function :TFE_OpSetAttrFloat, %i[pointer string float], :void
70
+ attach_function :TFE_OpSetAttrBool, %i[pointer string uint8], :void
63
71
  attach_function :TFE_OpSetAttrType, %i[pointer string int], :void
64
72
  attach_function :TFE_OpSetAttrShape, %i[pointer string pointer int pointer], :void
73
+ attach_function :TFE_OpSetAttrIntList, %i[pointer string pointer int], :void
74
+ attach_function :TFE_OpSetAttrFloatList, %i[pointer string pointer int], :void
75
+ attach_function :TFE_OpSetAttrTypeList, %i[pointer string pointer int], :void
76
+ attach_function :TFE_OpSetAttrShapeList, %i[pointer string pointer pointer int pointer], :void
65
77
  attach_function :TFE_Execute, %i[pointer pointer pointer pointer], :pointer
66
78
  end
67
79
  end
@@ -0,0 +1,218 @@
1
+ module TensorFlow
2
+ module Image
3
+ class << self
4
+ # def adjust_brightness
5
+ # end
6
+
7
+ def adjust_contrast(images, contrast_factor, min_value, max_value)
8
+ RawOps.adjust_contrast(images: images, contrast_factor: contrast_factor, min_value: min_value, max_value: max_value)
9
+ end
10
+
11
+ # def adjust_gamma
12
+ # end
13
+
14
+ def adjust_hue(images, delta)
15
+ RawOps.adjust_hue(images: images, delta: delta)
16
+ end
17
+
18
+ # def adjust_jpeg_quality
19
+ # end
20
+
21
+ def adjust_saturation(images, scale)
22
+ RawOps.adjust_saturation(images: images, scale: scale)
23
+ end
24
+
25
+ # def central_crop
26
+ # end
27
+
28
+ def combined_non_max_suppression(boxes, scores, max_output_size_per_class, max_total_size, iou_threshold, score_threshold, pad_per_class: nil, clip_boxes: nil)
29
+ RawOps.combined_non_max_suppression(boxes: boxes, scores: scores, max_output_size_per_class: max_output_size_per_class, max_total_size: max_total_size, iou_threshold: iou_threshold, score_threshold: score_threshold, pad_per_class: pad_per_class, clip_boxes: clip_boxes)
30
+ end
31
+
32
+ # def convert_image_dtype
33
+ # end
34
+
35
+ def crop_and_resize(image, boxes, box_ind, crop_size, method: nil, extrapolation_value: nil)
36
+ RawOps.crop_and_resize(image: image, boxes: boxes, box_ind: box_ind, crop_size: crop_size, method: method, extrapolation_value: extrapolation_value)
37
+ end
38
+
39
+ # def crop_to_bounding_box
40
+ # end
41
+
42
+ def decode_and_crop_jpeg(contents, crop_window, channels: nil, ratio: nil, fancy_upscaling: nil, try_recover_truncated: nil, acceptable_fraction: nil, dct_method: nil)
43
+ RawOps.decode_and_crop_jpeg(contents: contents, crop_window: crop_window, channels: channels, ratio: ratio, fancy_upscaling: fancy_upscaling, try_recover_truncated: try_recover_truncated, acceptable_fraction: acceptable_fraction, dct_method: dct_method)
44
+ end
45
+
46
+ def decode_bmp(contents, channels: nil)
47
+ RawOps.decode_bmp(contents: contents, channels: channels)
48
+ end
49
+
50
+ def decode_gif(contents)
51
+ RawOps.decode_gif(contents: contents)
52
+ end
53
+
54
+ # def decode_image
55
+ # end
56
+
57
+ def decode_jpeg(contents, channels: nil, ratio: nil, fancy_upscaling: nil, try_recover_truncated: nil, acceptable_fraction: nil, dct_method: nil)
58
+ RawOps.decode_jpeg(contents: contents, channels: channels, ratio: ratio, fancy_upscaling: fancy_upscaling, try_recover_truncated: try_recover_truncated, acceptable_fraction: acceptable_fraction, dct_method: dct_method)
59
+ end
60
+
61
+ def decode_png(contents, channels: nil, dtype: nil)
62
+ RawOps.decode_png(contents: contents, channels: channels, dtype: dtype)
63
+ end
64
+
65
+ def draw_bounding_boxes(images, boxes)
66
+ RawOps.draw_bounding_boxes(images: images, boxes: boxes)
67
+ end
68
+
69
+ def encode_jpeg(image, format: nil, quality: nil, progressive: nil, optimize_size: nil, chroma_downsampling: nil, density_unit: nil, x_density: nil, y_density: nil, xmp_metadata: nil)
70
+ RawOps.encode_jpeg(image: image, format: format, quality: quality, progressive: progressive, optimize_size: optimize_size, chroma_downsampling: chroma_downsampling, density_unit: density_unit, x_density: x_density, y_density: y_density, xmp_metadata: xmp_metadata)
71
+ end
72
+
73
+ def encode_png(image, compression: nil)
74
+ RawOps.encode_png(image: image, compression: compression)
75
+ end
76
+
77
+ def extract_glimpse(input, size, offsets, centered: nil, normalized: nil, uniform_noise: nil, noise: nil)
78
+ RawOps.extract_glimpse(input: input, size: size, offsets: offsets, centered: centered, normalized: normalized, uniform_noise: uniform_noise, noise: noise)
79
+ end
80
+
81
+ def extract_jpeg_shape(contents, output_type: nil)
82
+ RawOps.extract_jpeg_shape(contents: contents, output_type: output_type)
83
+ end
84
+
85
+ # def extract_patches
86
+ # end
87
+
88
+ # def flip_left_right
89
+ # end
90
+
91
+ # def flip_up_down
92
+ # end
93
+
94
+ # def grayscale_to_rgb
95
+ # end
96
+
97
+ def hsv_to_rgb(images)
98
+ RawOps.hsv_to_rgb(images: images)
99
+ end
100
+
101
+ # def image_gradients
102
+ # end
103
+
104
+ def is_jpeg(contents)
105
+ substr = Strings.substr(contents, 0, 3)
106
+ Math.equal(substr, "\xff\xd8\xff")
107
+ end
108
+
109
+ def non_max_suppression(boxes, scores, max_output_size, iou_threshold: nil)
110
+ RawOps.non_max_suppression(boxes: boxes, scores: scores, max_output_size: max_output_size, iou_threshold: iou_threshold)
111
+ end
112
+
113
+ # def non_max_suppression_overlaps
114
+ # end
115
+
116
+ # def non_max_suppression_padded
117
+ # end
118
+
119
+ # def non_max_suppression_with_scores
120
+ # end
121
+
122
+ # def pad_to_bounding_box
123
+ # end
124
+
125
+ # def per_image_standardization
126
+ # end
127
+
128
+ # def psnr
129
+ # end
130
+
131
+ # def random_brightness
132
+ # end
133
+
134
+ # def random_contrast
135
+ # end
136
+
137
+ def random_crop(image, size, seed: nil, seed2: nil)
138
+ RawOps.random_crop(image: image, size: size, seed: seed, seed2: seed2)
139
+ end
140
+
141
+ # def random_flip_left_right
142
+ # end
143
+
144
+ # def random_flip_up_down
145
+ # end
146
+
147
+ # def random_hue
148
+ # end
149
+
150
+ # def random_jpeg_quality
151
+ # end
152
+
153
+ # def random_saturation
154
+ # end
155
+
156
+ def resize(images, size)
157
+ images = TensorFlow.convert_to_tensor(images)
158
+
159
+ batch = images.shape.size != 3
160
+ images = TensorFlow.expand_dims(images, 0) unless batch
161
+
162
+ # TODO support more methods
163
+ images = RawOps.resize_bilinear(images: images, size: size)
164
+
165
+ images = TensorFlow.squeeze(images, axis: [0]) unless batch
166
+
167
+ images
168
+ end
169
+
170
+ # def resize_with_crop_or_pad
171
+ # end
172
+
173
+ # def resize_with_pad
174
+ # end
175
+
176
+ # def rgb_to_grayscale
177
+ # end
178
+
179
+ def rgb_to_hsv(images)
180
+ RawOps.rgb_to_hsv(images: images)
181
+ end
182
+
183
+ # def rgb_to_yiq
184
+ # end
185
+
186
+ # def rgb_to_yuv
187
+ # end
188
+
189
+ # def rot90
190
+ # end
191
+
192
+ def sample_distorted_bounding_box(image_size, bounding_boxes, seed: nil, seed2: nil, min_object_covered: nil, aspect_ratio_range: nil, area_range: nil, max_attempts: nil, use_image_if_no_bounding_boxes: nil)
193
+ RawOps.sample_distorted_bounding_box(image_size: image_size, bounding_boxes: bounding_boxes, seed: seed, seed2: seed2, min_object_covered: min_object_covered, aspect_ratio_range: aspect_ratio_range, area_range: area_range, max_attempts: max_attempts, use_image_if_no_bounding_boxes: use_image_if_no_bounding_boxes)
194
+ end
195
+
196
+ # def sobel_edges
197
+ # end
198
+
199
+ # def ssim
200
+ # end
201
+
202
+ # def ssim_multiscale
203
+ # end
204
+
205
+ # def total_variation
206
+ # end
207
+
208
+ # def transpose
209
+ # end
210
+
211
+ # def yiq_to_rgb
212
+ # end
213
+
214
+ # def yuv_to_rgb
215
+ # end
216
+ end
217
+ end
218
+ end