-
Notifications
You must be signed in to change notification settings - Fork 307
Add libsvm dataset support #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
licenses(["notice"]) # Apache 2.0 | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
cc_binary( | ||
name = "python/ops/_libsvm_ops.so", | ||
srcs = [ | ||
"kernels/decode_libsvm_op.cc", | ||
"ops/libsvm_ops.cc", | ||
], | ||
linkshared = 1, | ||
deps = [ | ||
"@local_config_tf//:libtensorflow_framework", | ||
"@local_config_tf//:tf_header_lib", | ||
"@kafka//:kafka", | ||
], | ||
copts = ["-pthread", "-std=c++11", "-D_GLIBCXX_USE_CXX11_ABI=0", "-DNDEBUG"] | ||
) | ||
|
||
py_library( | ||
name = "libsvm_ops_py", | ||
srcs = [ | ||
"python/ops/libsvm_dataset_ops.py", | ||
], | ||
data = [ | ||
":python/ops/_libsvm_ops.so", | ||
], | ||
srcs_version = "PY2AND3", | ||
) | ||
|
||
py_test( | ||
name = "decode_libsvm_op_test", | ||
srcs = [ | ||
"python/kernel_tests/decode_libsvm_op_test.py" | ||
], | ||
main = "python/kernel_tests/decode_libsvm_op_test.py", | ||
deps = [ | ||
":libsvm_ops_py", | ||
], | ||
srcs_version = "PY2AND3", | ||
) | ||
|
||
py_library( | ||
name = "libsvm_py", | ||
srcs = ([ | ||
"__init__.py", | ||
"python/__init__.py", | ||
"python/ops/__init__.py", | ||
]), | ||
deps = [ | ||
":libsvm_ops_py" | ||
], | ||
srcs_version = "PY2AND3", | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
"""LibSVM Dataset. | ||
|
||
@@make_libsvm_dataset | ||
""" | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
from tensorflow.contrib.libsvm.python.ops.libsvm_dataset_ops import make_libsvm_dataset | ||
|
||
from tensorflow.python.util.all_util import remove_undocumented | ||
|
||
_allowed_symbols = [ | ||
"make_libsvm_dataset", | ||
] | ||
|
||
remove_undocumented(__name__) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include "tensorflow/core/framework/op_kernel.h" | ||
#include "tensorflow/core/framework/tensor.h" | ||
#include "tensorflow/core/framework/tensor_shape.h" | ||
#include "tensorflow/core/framework/types.h" | ||
#include "tensorflow/core/lib/core/errors.h" | ||
#include "tensorflow/core/lib/strings/numbers.h" | ||
#include "tensorflow/core/lib/strings/str_util.h" | ||
|
||
namespace tensorflow { | ||
|
||
template <typename T, typename Tlabel> | ||
class DecodeLibsvmOp : public OpKernel { | ||
public: | ||
explicit DecodeLibsvmOp(OpKernelConstruction* ctx) : OpKernel(ctx) { | ||
OP_REQUIRES_OK(ctx, ctx->GetAttr("num_features", &num_features_)); | ||
OP_REQUIRES(ctx, (num_features_ >= 1), | ||
errors::InvalidArgument("Invalid number of features \"", | ||
num_features_, "\"")); | ||
} | ||
|
||
void Compute(OpKernelContext* ctx) override { | ||
const Tensor* input_tensor; | ||
OP_REQUIRES_OK(ctx, ctx->input("input", &input_tensor)); | ||
const auto& input_flat = input_tensor->flat<string>(); | ||
|
||
Tensor* label_tensor; | ||
OP_REQUIRES_OK( | ||
ctx, ctx->allocate_output(0, input_tensor->shape(), &label_tensor)); | ||
auto label = label_tensor->flat<Tlabel>(); | ||
|
||
std::vector<T> out_values; | ||
std::vector<std::pair<int64, int64>> out_indices; | ||
for (int i = 0; i < input_flat.size(); ++i) { | ||
StringPiece line(input_flat(i)); | ||
str_util::RemoveWhitespaceContext(&line); | ||
|
||
StringPiece piece; | ||
OP_REQUIRES(ctx, str_util::ConsumeNonWhitespace(&line, &piece), | ||
errors::InvalidArgument("No label found for input[", i, | ||
"]: \"", input_flat(i), "\"")); | ||
|
||
Tlabel label_value; | ||
OP_REQUIRES(ctx, | ||
strings::SafeStringToNumeric<Tlabel>(piece, &label_value), | ||
errors::InvalidArgument("Label format incorrect: ", piece)); | ||
|
||
label(i) = label_value; | ||
|
||
str_util::RemoveLeadingWhitespace(&line); | ||
while (str_util::ConsumeNonWhitespace(&line, &piece)) { | ||
size_t p = piece.find(':'); | ||
OP_REQUIRES(ctx, (p != StringPiece::npos), | ||
errors::InvalidArgument("Invalid feature \"", piece, "\"")); | ||
|
||
int64 feature_index; | ||
OP_REQUIRES( | ||
ctx, strings::safe_strto64(piece.substr(0, p), &feature_index), | ||
errors::InvalidArgument("Feature format incorrect: ", piece)); | ||
OP_REQUIRES(ctx, (feature_index >= 0), | ||
errors::InvalidArgument( | ||
"Feature index should be >= 0, got ", feature_index)); | ||
|
||
T feature_value; | ||
OP_REQUIRES( | ||
|
||
ctx, | ||
strings::SafeStringToNumeric<T>(piece.substr(p + 1), | ||
&feature_value), | ||
errors::InvalidArgument("Feature format incorrect: ", piece)); | ||
|
||
out_values.emplace_back(feature_value); | ||
out_indices.emplace_back(std::pair<int64, int64>(i, feature_index)); | ||
|
||
str_util::RemoveLeadingWhitespace(&line); | ||
} | ||
} | ||
|
||
Tensor* indices_tensor; | ||
OP_REQUIRES_OK(ctx, ctx->allocate_output( | ||
1, | ||
TensorShape({static_cast<int64>(out_indices.size()), | ||
input_tensor->shape().dims() + 1}), | ||
&indices_tensor)); | ||
auto indices = indices_tensor->matrix<int64>(); | ||
// Translate flat index to shaped index like np.unravel_index | ||
// Calculate factors for each dimension | ||
std::vector<int64> factors(input_tensor->shape().dims()); | ||
factors[input_tensor->shape().dims() - 1] = 1; | ||
for (int j = input_tensor->shape().dims() - 2; j >= 0; j--) { | ||
factors[j] = factors[j + 1] * input_tensor->shape().dim_size(j + 1); | ||
} | ||
for (int i = 0; i < out_indices.size(); i++) { | ||
indices(i, 0) = out_indices[i].first; | ||
int64 value = out_indices[i].first; | ||
for (int j = 0; j < input_tensor->shape().dims(); j++) { | ||
indices(i, j) = value / factors[j]; | ||
value = value % factors[j]; | ||
} | ||
indices(i, input_tensor->shape().dims()) = out_indices[i].second; | ||
} | ||
|
||
Tensor* values_tensor; | ||
OP_REQUIRES_OK(ctx, | ||
ctx->allocate_output( | ||
2, TensorShape({static_cast<int64>(out_values.size())}), | ||
&values_tensor)); | ||
auto values = values_tensor->vec<T>(); | ||
std::copy_n(out_values.begin(), out_values.size(), &values(0)); | ||
|
||
Tensor* shape_tensor; | ||
OP_REQUIRES_OK(ctx, ctx->allocate_output( | ||
3, TensorShape({input_tensor->shape().dims() + 1}), | ||
&shape_tensor)); | ||
auto shape = shape_tensor->flat<int64>(); | ||
for (int i = 0; i < input_tensor->shape().dims(); i++) { | ||
shape(i) = input_tensor->shape().dim_size(i); | ||
} | ||
shape(input_tensor->shape().dims()) = num_features_; | ||
} | ||
|
||
private: | ||
int64 num_features_; | ||
}; | ||
|
||
#define REGISTER_KERNEL(type) \ | ||
REGISTER_KERNEL_BUILDER(Name("DecodeLibsvm") \ | ||
.Device(DEVICE_CPU) \ | ||
.TypeConstraint<type>("dtype") \ | ||
.TypeConstraint<int32>("label_dtype"), \ | ||
DecodeLibsvmOp<type, int32>); \ | ||
REGISTER_KERNEL_BUILDER(Name("DecodeLibsvm") \ | ||
.Device(DEVICE_CPU) \ | ||
.TypeConstraint<type>("dtype") \ | ||
.TypeConstraint<int64>("label_dtype"), \ | ||
DecodeLibsvmOp<type, int64>); \ | ||
REGISTER_KERNEL_BUILDER(Name("DecodeLibsvm") \ | ||
.Device(DEVICE_CPU) \ | ||
.TypeConstraint<type>("dtype") \ | ||
.TypeConstraint<float>("label_dtype"), \ | ||
DecodeLibsvmOp<type, float>); \ | ||
REGISTER_KERNEL_BUILDER(Name("DecodeLibsvm") \ | ||
.Device(DEVICE_CPU) \ | ||
.TypeConstraint<type>("dtype") \ | ||
.TypeConstraint<double>("label_dtype"), \ | ||
DecodeLibsvmOp<type, double>); | ||
|
||
REGISTER_KERNEL(float); | ||
REGISTER_KERNEL(double); | ||
REGISTER_KERNEL(int32); | ||
REGISTER_KERNEL(int64); | ||
#undef REGISTER_KERNEL | ||
|
||
} // namespace tensorflow |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include "tensorflow/core/framework/common_shape_fns.h" | ||
#include "tensorflow/core/framework/op.h" | ||
#include "tensorflow/core/framework/shape_inference.h" | ||
|
||
namespace tensorflow { | ||
|
||
using shape_inference::InferenceContext; | ||
|
||
REGISTER_OP("DecodeLibsvm") | ||
.Input("input: string") | ||
.Output("label: label_dtype") | ||
.Output("feature_indices: int64") | ||
.Output("feature_values: dtype") | ||
.Output("feature_shape: int64") | ||
.Attr("dtype: {float, double, int32, int64} = DT_FLOAT") | ||
.Attr("label_dtype: {float, double, int32, int64} = DT_INT64") | ||
.Attr("num_features: int >= 1") | ||
.SetShapeFn([](InferenceContext* c) { | ||
c->set_output(0, c->input(0)); | ||
|
||
c->set_output(1, c->Matrix(InferenceContext::kUnknownDim, | ||
InferenceContext::kUnknownDim)); | ||
c->set_output(2, c->Vector(InferenceContext::kUnknownDim)); | ||
c->set_output(3, c->Vector(InferenceContext::kUnknownDim)); | ||
|
||
return Status::OK(); | ||
}) | ||
|
||
.Doc(R"doc( | ||
Convert LibSVM input to tensors. The output consists of | ||
a label and a feature tensor. The shape of the label tensor | ||
is the same as input and the shape of the feature tensor is | ||
`[input_shape, num_features]`. | ||
|
||
input: Each string is a record in the LibSVM. | ||
label: A tensor of the same shape as input. | ||
feature_indices: A 2-D int64 tensor of dense_shape [N, ndims]. | ||
feature_values: A 1-D tensor of any type and dense_shape [N]. | ||
feature_shape: A 1-D int64 tensor of dense_shape [ndims]. | ||
num_features: The number of features. | ||
)doc"); | ||
|
||
} // namespace tensorflow |
Empty file.
71 changes: 71 additions & 0 deletions
71
tensorflow_io/libsvm/python/kernel_tests/decode_libsvm_op_test.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
"""Tests for DecodeLibsvm op.""" | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import numpy as np | ||
|
||
from tensorflow_io.libsvm.python.ops import libsvm_dataset_ops | ||
from tensorflow.python.framework import dtypes | ||
from tensorflow.python.ops import sparse_ops | ||
from tensorflow.python.platform import test | ||
|
||
|
||
class DecodeLibsvmOpTest(test.TestCase): | ||
|
||
def testBasic(self): | ||
with self.cached_session() as sess: | ||
content = [ | ||
"1 1:3.4 2:0.5 4:0.231", "1 2:2.5 3:inf 5:0.503", | ||
"2 3:2.5 2:nan 1:0.105" | ||
] | ||
sparse_features, labels = libsvm_dataset_ops.decode_libsvm( | ||
content, num_features=6) | ||
features = sparse_ops.sparse_tensor_to_dense( | ||
sparse_features, validate_indices=False) | ||
|
||
self.assertAllEqual(labels.get_shape().as_list(), [3]) | ||
|
||
features, labels = sess.run([features, labels]) | ||
self.assertAllEqual(labels, [1, 1, 2]) | ||
self.assertAllClose( | ||
features, [[0, 3.4, 0.5, 0, 0.231, 0], [0, 0, 2.5, np.inf, 0, 0.503], | ||
[0, 0.105, np.nan, 2.5, 0, 0]]) | ||
|
||
def testNDimension(self): | ||
with self.cached_session() as sess: | ||
content = [["1 1:3.4 2:0.5 4:0.231", "1 1:3.4 2:0.5 4:0.231"], | ||
["1 2:2.5 3:inf 5:0.503", "1 2:2.5 3:inf 5:0.503"], | ||
["2 3:2.5 2:nan 1:0.105", "2 3:2.5 2:nan 1:0.105"]] | ||
sparse_features, labels = libsvm_dataset_ops.decode_libsvm( | ||
content, num_features=6, label_dtype=dtypes.float64) | ||
features = sparse_ops.sparse_tensor_to_dense( | ||
sparse_features, validate_indices=False) | ||
|
||
self.assertAllEqual(labels.get_shape().as_list(), [3, 2]) | ||
|
||
features, labels = sess.run([features, labels]) | ||
self.assertAllEqual(labels, [[1, 1], [1, 1], [2, 2]]) | ||
self.assertAllClose( | ||
features, [[[0, 3.4, 0.5, 0, 0.231, 0], [0, 3.4, 0.5, 0, 0.231, 0]], [ | ||
[0, 0, 2.5, np.inf, 0, 0.503], [0, 0, 2.5, np.inf, 0, 0.503] | ||
], [[0, 0.105, np.nan, 2.5, 0, 0], [0, 0.105, np.nan, 2.5, 0, 0]]]) | ||
|
||
|
||
if __name__ == "__main__": | ||
test.main() |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.