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.
@@ -0,0 +1,125 @@
1
+ module TensorFlow
2
+ module IO
3
+ class << self
4
+ 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)
5
+ 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)
6
+ end
7
+
8
+ def decode_base64(input)
9
+ RawOps.decode_base64(input: input)
10
+ end
11
+
12
+ def decode_bmp(contents, channels: nil)
13
+ RawOps.decode_bmp(contents: contents, channels: channels)
14
+ end
15
+
16
+ def decode_compressed(bytes, compression_type: nil)
17
+ RawOps.decode_compressed(bytes: bytes, compression_type: compression_type)
18
+ end
19
+
20
+ def decode_csv(records, record_defaults, field_delim: nil, use_quote_delim: nil, na_value: nil, select_cols: nil)
21
+ RawOps.decode_csv(records: records, record_defaults: record_defaults, field_delim: field_delim, use_quote_delim: use_quote_delim, na_value: na_value, select_cols: select_cols)
22
+ end
23
+
24
+ def decode_gif(contents)
25
+ RawOps.decode_gif(contents: contents)
26
+ end
27
+
28
+ # def decode_image
29
+ # end
30
+
31
+ def decode_jpeg(contents, channels: nil, ratio: nil, fancy_upscaling: nil, try_recover_truncated: nil, acceptable_fraction: nil, dct_method: nil)
32
+ 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)
33
+ end
34
+
35
+ def decode_json_example(json_examples)
36
+ RawOps.decode_json_example(json_examples: json_examples)
37
+ end
38
+
39
+ def decode_png(contents, channels: nil, dtype: nil)
40
+ RawOps.decode_png(contents: contents, channels: channels, dtype: dtype)
41
+ end
42
+
43
+ # def decode_proto
44
+ # end
45
+
46
+ def decode_raw(bytes, out_type: nil, little_endian: nil)
47
+ RawOps.decode_raw(bytes: bytes, out_type: out_type, little_endian: little_endian)
48
+ end
49
+
50
+ def deserialize_many_sparse(serialized_sparse, dtype: nil)
51
+ RawOps.deserialize_many_sparse(serialized_sparse: serialized_sparse, dtype: dtype)
52
+ end
53
+
54
+ def encode_base64(input, pad: nil)
55
+ RawOps.encode_base64(input: input, pad: pad)
56
+ end
57
+
58
+ 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)
59
+ 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)
60
+ end
61
+
62
+ def encode_proto(sizes, values, field_names: nil, message_type: nil, descriptor_source: nil)
63
+ RawOps.encode_proto(sizes: sizes, values: values, field_names: field_names, message_type: message_type, descriptor_source: descriptor_source)
64
+ end
65
+
66
+ def extract_jpeg_shape(contents, output_type: nil)
67
+ RawOps.extract_jpeg_shape(contents: contents, output_type: output_type)
68
+ end
69
+
70
+ def is_jpeg(contents)
71
+ Image.is_jpeg(contents)
72
+ end
73
+
74
+ # def match_filenames_once
75
+ # end
76
+
77
+ def matching_files(pattern)
78
+ RawOps.matching_files(pattern: pattern)
79
+ end
80
+
81
+ def parse_example(serialized, names, sparse_keys, dense_keys, dense_defaults, sparse_types: nil, dense_shapes: nil)
82
+ RawOps.parse_example(serialized: serialized, names: names, sparse_keys: sparse_keys, dense_keys: dense_keys, dense_defaults: dense_defaults, sparse_types: sparse_types, dense_shapes: dense_shapes)
83
+ end
84
+
85
+ def parse_sequence_example(serialized, debug_name, context_dense_defaults, feature_list_dense_missing_assumed_empty: nil, context_sparse_keys: nil, context_dense_keys: nil, feature_list_sparse_keys: nil, feature_list_dense_keys: nil, context_sparse_types: nil, feature_list_dense_types: nil, context_dense_shapes: nil, feature_list_sparse_types: nil, feature_list_dense_shapes: nil)
86
+ RawOps.parse_sequence_example(serialized: serialized, debug_name: debug_name, context_dense_defaults: context_dense_defaults, feature_list_dense_missing_assumed_empty: feature_list_dense_missing_assumed_empty, context_sparse_keys: context_sparse_keys, context_dense_keys: context_dense_keys, feature_list_sparse_keys: feature_list_sparse_keys, feature_list_dense_keys: feature_list_dense_keys, context_sparse_types: context_sparse_types, feature_list_dense_types: feature_list_dense_types, context_dense_shapes: context_dense_shapes, feature_list_sparse_types: feature_list_sparse_types, feature_list_dense_shapes: feature_list_dense_shapes)
87
+ end
88
+
89
+ def parse_single_example(serialized, dense_defaults, num_sparse: nil, sparse_keys: nil, dense_keys: nil, sparse_types: nil, dense_shapes: nil)
90
+ RawOps.parse_single_example(serialized: serialized, dense_defaults: dense_defaults, num_sparse: num_sparse, sparse_keys: sparse_keys, dense_keys: dense_keys, sparse_types: sparse_types, dense_shapes: dense_shapes)
91
+ end
92
+
93
+ def parse_single_sequence_example(serialized, feature_list_dense_missing_assumed_empty, context_sparse_keys, context_dense_keys, feature_list_sparse_keys, feature_list_dense_keys, context_dense_defaults, debug_name, context_sparse_types: nil, feature_list_dense_types: nil, context_dense_shapes: nil, feature_list_sparse_types: nil, feature_list_dense_shapes: nil)
94
+ RawOps.parse_single_sequence_example(serialized: serialized, feature_list_dense_missing_assumed_empty: feature_list_dense_missing_assumed_empty, context_sparse_keys: context_sparse_keys, context_dense_keys: context_dense_keys, feature_list_sparse_keys: feature_list_sparse_keys, feature_list_dense_keys: feature_list_dense_keys, context_dense_defaults: context_dense_defaults, debug_name: debug_name, context_sparse_types: context_sparse_types, feature_list_dense_types: feature_list_dense_types, context_dense_shapes: context_dense_shapes, feature_list_sparse_types: feature_list_sparse_types, feature_list_dense_shapes: feature_list_dense_shapes)
95
+ end
96
+
97
+ def parse_tensor(serialized, out_type: nil)
98
+ RawOps.parse_tensor(serialized: serialized, out_type: out_type)
99
+ end
100
+
101
+ def read_file(filename)
102
+ RawOps.read_file(filename: filename)
103
+ end
104
+
105
+ def serialize_many_sparse(sparse_indices, sparse_values, sparse_shape, out_type: nil)
106
+ RawOps.serialize_many_sparse(sparse_indices: sparse_indices, sparse_values: sparse_values, sparse_shape: sparse_shape, out_type: out_type)
107
+ end
108
+
109
+ def serialize_sparse(sparse_indices, sparse_values, sparse_shape, out_type: nil)
110
+ RawOps.serialize_sparse(sparse_indices: sparse_indices, sparse_values: sparse_values, sparse_shape: sparse_shape, out_type: out_type)
111
+ end
112
+
113
+ def serialize_tensor(tensor)
114
+ RawOps.serialize_tensor(tensor: tensor)
115
+ end
116
+
117
+ def write_file(filename, contents)
118
+ RawOps.write_file(filename: filename, contents: contents)
119
+ end
120
+
121
+ # def write_graph
122
+ # end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,32 @@
1
+ module TensorFlow
2
+ module Keras
3
+ module Datasets
4
+ module BostonHousing
5
+ def self.load_data(path: "boston_housing.npz", test_split: 0.2, seed: 113)
6
+ file = Utils.get_file(
7
+ path,
8
+ "https://storage.googleapis.com/tensorflow/tf-keras-datasets/boston_housing.npz",
9
+ file_hash: "f553886a1f8d56431e820c5b82552d9d95cfcb96d1e678153f8839538947dff5"
10
+ )
11
+
12
+ data = Npy.load_npz(file)
13
+
14
+ x = data["x"]
15
+ y = data["y"]
16
+
17
+ len = x.shape[0]
18
+ indices = (0...len).to_a.shuffle(random: Random.new(seed))
19
+ x = x[indices, true]
20
+ y = y[indices]
21
+
22
+ x_train = x[0...(len * (1 - test_split)), true]
23
+ y_train = y[0...(len * (1 - test_split))]
24
+ x_test = x[(len * (1 - test_split))..-1, true]
25
+ y_test = y[(len * (1 - test_split))..-1]
26
+
27
+ [[x_train, y_train], [x_test, y_test]]
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ module TensorFlow
2
+ module Keras
3
+ module Datasets
4
+ module CIFAR10
5
+ # unfortunately, npy can't read pickle and numo can't store objects
6
+ # def self.load_data
7
+ # end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module TensorFlow
2
+ module Keras
3
+ module Datasets
4
+ module CIFAR100
5
+ # unfortunately, npy can't read pickle and numo can't store objects
6
+ # def self.load_data
7
+ # end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,44 @@
1
+ module TensorFlow
2
+ module Keras
3
+ module Datasets
4
+ module FashionMNIST
5
+ def self.load_data
6
+ base_url = "https://storage.googleapis.com/tensorflow/tf-keras-datasets"
7
+ files = [
8
+ "train-labels-idx1-ubyte.gz", "train-images-idx3-ubyte.gz",
9
+ "t10k-labels-idx1-ubyte.gz", "t10k-images-idx3-ubyte.gz"
10
+ ]
11
+
12
+ paths = []
13
+ files.each do |file|
14
+ paths << Utils.get_file(file, "#{base_url}/#{file}", cache_subdir: "datasets/fashion-mnist")
15
+ end
16
+
17
+ x_train, y_train, x_test, y_test = nil
18
+
19
+ Zlib::GzipReader.open(paths[0]) do |gz|
20
+ gz.read(8) # move to offset
21
+ y_train = Numo::UInt8.from_string(gz.read)
22
+ end
23
+
24
+ Zlib::GzipReader.open(paths[1]) do |gz|
25
+ gz.read(16) # move to offset
26
+ x_train = Numo::UInt8.from_string(gz.read, [y_train.shape[0], 28, 28])
27
+ end
28
+
29
+ Zlib::GzipReader.open(paths[2]) do |gz|
30
+ gz.read(8) # move to offset
31
+ y_test = Numo::UInt8.from_string(gz.read)
32
+ end
33
+
34
+ Zlib::GzipReader.open(paths[3]) do |gz|
35
+ gz.read(16) # move to offset
36
+ x_test = Numo::UInt8.from_string(gz.read, [y_test.shape[0], 28, 28])
37
+ end
38
+
39
+ [[x_train, y_train], [x_test, y_test]]
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,30 @@
1
+ module TensorFlow
2
+ module Keras
3
+ module Datasets
4
+ module IMDB
5
+ # unfortunately, npy can't read pickle and numo can't store objects
6
+ # def self.load_data(path: "imdb.npz", seed: 113)
7
+ # data = Utils.load_dataset(
8
+ # path,
9
+ # "https://storage.googleapis.com/tensorflow/tf-keras-datasets/imdb.npz",
10
+ # "69664113be75683a8fe16e3ed0ab59fda8886cb3cd7ada244f7d9544e4676b9f"
11
+ # )
12
+
13
+ # x_train = data["x_train"]
14
+ # labels_train = data["y_train"]
15
+ # x_test = data["x_test"]
16
+ # labels_test = data["y_test"]
17
+ # end
18
+
19
+ def self.get_word_index(path: "imdb_word_index.json")
20
+ file = Utils.get_file(
21
+ path,
22
+ "https://storage.googleapis.com/tensorflow/tf-keras-datasets/imdb_word_index.json",
23
+ file_hash: "bfafd718b763782e994055a2d397834f"
24
+ )
25
+ JSON.parse(File.read(file))
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -2,13 +2,14 @@ module TensorFlow
2
2
  module Keras
3
3
  module Datasets
4
4
  module MNIST
5
- def self.load_data
6
- # TODO verify hash
7
- data = Utils.load_dataset(
8
- "mnist.npz",
9
- "https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz"
5
+ def self.load_data(path: "mnist.npz")
6
+ file = Utils.get_file(
7
+ path,
8
+ "https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz",
9
+ file_hash: "731c5ac602752760c8e48fbffcf8c3b850d9dc2a2aedcf2cc48468fc17b673d1"
10
10
  )
11
11
 
12
+ data = Npy.load_npz(file)
12
13
  [[data["x_train"], data["y_train"]], [data["x_test"], data["y_test"]]]
13
14
  end
14
15
  end
@@ -0,0 +1,28 @@
1
+ module TensorFlow
2
+ module Keras
3
+ module Datasets
4
+ module Reuters
5
+ # unfortunately, npy can't read pickle and numo can't store objects
6
+ # def self.load_data(path: "reuters.npz", test_split: 0.2, seed: 113)
7
+ # data = Utils.load_dataset(
8
+ # path,
9
+ # "https://storage.googleapis.com/tensorflow/tf-keras-datasets/reuters.npz",
10
+ # "d6586e694ee56d7a4e65172e12b3e987c03096cb01eab99753921ef915959916"
11
+ # )
12
+
13
+ # xs = data["x"]
14
+ # labels = data["y"]
15
+ # end
16
+
17
+ def self.get_word_index(path: "reuters_word_index.json")
18
+ file = Utils.get_file(
19
+ path,
20
+ "https://storage.googleapis.com/tensorflow/tf-keras-datasets/reuters_word_index.json",
21
+ file_hash: "4d44cc38712099c9e383dc6e5f11a921"
22
+ )
23
+ JSON.parse(File.read(file))
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ module TensorFlow
2
+ module Keras
3
+ module Metrics
4
+ class Mean
5
+ def update_state(values)
6
+ input = TensorFlow.convert_to_tensor(values, dtype: :float)
7
+ @total = Math.reduce_sum(input)
8
+ @count = RawOps.size(input: input)
9
+ end
10
+
11
+ def result
12
+ RawOps.div_no_nan(x: @total, y: TensorFlow.cast(@count, :float))
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,56 @@
1
+ module TensorFlow
2
+ module Keras
3
+ module Utils
4
+ class << self
5
+ def get_file(fname, origin, file_hash: nil, cache_subdir: "datasets")
6
+ # destination
7
+ # TODO handle this better
8
+ raise "No HOME" unless ENV["HOME"]
9
+ dest = "#{ENV["HOME"]}/.keras/#{cache_subdir}/#{fname}"
10
+ FileUtils.mkdir_p(File.dirname(dest))
11
+
12
+ return dest if File.exist?(dest)
13
+
14
+ temp_dir ||= File.dirname(Tempfile.new("tensorflow"))
15
+ temp_path = "#{temp_dir}/#{Time.now.to_f}" # TODO better name
16
+
17
+ digest = file_hash&.size == 32 ? Digest::MD5.new : Digest::SHA2.new
18
+
19
+ uri = URI(origin)
20
+
21
+ # Net::HTTP automatically adds Accept-Encoding for compression
22
+ # of response bodies and automatically decompresses gzip
23
+ # and deflateresponses unless a Range header was sent.
24
+ # https://ruby-doc.org/stdlib-2.6.4/libdoc/net/http/rdoc/Net/HTTP.html
25
+ Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
26
+ request = Net::HTTP::Get.new(uri)
27
+
28
+ puts "Downloading data from #{origin}"
29
+ i = 0
30
+ File.open(temp_path, "wb") do |f|
31
+ http.request(request) do |response|
32
+ response.read_body do |chunk|
33
+ f.write(chunk)
34
+ digest.update(chunk)
35
+
36
+ # print progress
37
+ putc "." if i % 50 == 0
38
+ i += 1
39
+ end
40
+ end
41
+ puts # newline
42
+ end
43
+ end
44
+
45
+ if file_hash && digest.hexdigest != file_hash
46
+ raise Error, "Bad hash"
47
+ end
48
+
49
+ FileUtils.mv(temp_path, dest)
50
+
51
+ dest
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,133 @@
1
+ module TensorFlow
2
+ module Linalg
3
+ class << self
4
+ # def adjoint
5
+ # end
6
+
7
+ # def band_part
8
+ # end
9
+
10
+ def cholesky(input)
11
+ RawOps.cholesky(input: input)
12
+ end
13
+
14
+ # def cholesky_solve
15
+ # end
16
+
17
+ def cross(a, b)
18
+ RawOps.cross(a: a, b: b)
19
+ end
20
+
21
+ # def det
22
+ # end
23
+
24
+ def diag(diagonal)
25
+ RawOps.diag(diagonal: diagonal)
26
+ end
27
+
28
+ def diag_part(input)
29
+ RawOps.diag_part(input: input)
30
+ end
31
+
32
+ # def eigh
33
+ # end
34
+
35
+ # def eigvalsh
36
+ # end
37
+
38
+ # def einsum
39
+ # end
40
+
41
+ # def expm
42
+ # end
43
+
44
+ def eye(num_rows, num_columns: nil)
45
+ num_columns ||= num_rows
46
+ zeros = TensorFlow.zeros([num_rows, num_columns])
47
+ ones = TensorFlow.ones([num_rows])
48
+ RawOps.matrix_set_diag(input: zeros, diagonal: ones)
49
+ end
50
+
51
+ # def global_norm
52
+ # end
53
+
54
+ def inv(x)
55
+ RawOps.inv(x: x)
56
+ end
57
+
58
+ # def l2_normalize
59
+ # end
60
+
61
+ # def logdet
62
+ # end
63
+
64
+ # def logm
65
+ # end
66
+
67
+ # def lstsq
68
+ # end
69
+
70
+ def lu(input, output_idx_type: nil)
71
+ RawOps.lu(input: input, output_idx_type: output_idx_type)
72
+ end
73
+
74
+ def matmul(a, b, transpose_a: false, transpose_b: false)
75
+ RawOps.mat_mul(a: a, b: b, transpose_a: transpose_a, transpose_b: transpose_b)
76
+ end
77
+
78
+ # def matrix_transpose
79
+ # end
80
+
81
+ # def matvec
82
+ # end
83
+
84
+ # def norm
85
+ # end
86
+
87
+ # def normalize
88
+ # end
89
+
90
+ def qr(input, full_matrices: nil)
91
+ RawOps.qr(input: input, full_matrices: full_matrices)
92
+ end
93
+
94
+ # def set_diag
95
+ # end
96
+
97
+ # def slogdet
98
+ # end
99
+
100
+ # def solve
101
+ # end
102
+
103
+ # def sqrtm
104
+ # end
105
+
106
+ def svd(input, compute_uv: nil, full_matrices: nil)
107
+ RawOps.svd(input: input, compute_uv: compute_uv, full_matrices: full_matrices)
108
+ end
109
+
110
+ # def tensor_diag
111
+ # end
112
+
113
+ # def tensor_diag_part
114
+ # end
115
+
116
+ # def tensordot
117
+ # end
118
+
119
+ # def trace
120
+ # end
121
+
122
+ # def triangular_solve
123
+ # end
124
+
125
+ # def tridiagonal_matmul
126
+ # end
127
+
128
+ def tridiagonal_solve(diagonals, rhs, partial_pivoting: nil)
129
+ RawOps.tridiagonal_solve(diagonals: diagonals, rhs: rhs, partial_pivoting: partial_pivoting)
130
+ end
131
+ end
132
+ end
133
+ end