-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2ed6aa
commit 3dcf266
Showing
5 changed files
with
88 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
__pycache__ | ||
data | ||
*.so |
This file contains 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,3 @@ | ||
#!/bin/bash | ||
TF_INC=$(python3.5 -c 'import tensorflow as tf; print(tf.sysconfig.get_include())') | ||
g++ --std=c++11 --shared inhibit.cc -o inhibit.so -fPIC -I $TF_INC -O2 |
This file contains 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,78 @@ | ||
#include "tensorflow/core/framework/op.h" | ||
#include "tensorflow/core/framework/shape_inference.h" | ||
#include "tensorflow/core/framework/op_kernel.h" | ||
|
||
#include <tuple> | ||
#include <queue> | ||
#include <unordered_set> | ||
#include <math.h> | ||
|
||
using namespace std; | ||
using namespace tensorflow; | ||
|
||
REGISTER_OP("Inhibit") | ||
.Input("to_inhibit: float") | ||
.Output("inhibited: float") | ||
.SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) { | ||
c->set_output(0, c->input(0)); | ||
return Status::OK(); | ||
}); | ||
|
||
/** | ||
* Applies inhibition to a matrix, setting all but the highest values | ||
* in the matrix to zero, and all highest values to one. | ||
*/ | ||
class InhibitOp : public OpKernel { | ||
public: | ||
explicit InhibitOp(OpKernelConstruction* context) : OpKernel(context) {} | ||
|
||
void Compute(OpKernelContext* context) override { | ||
// Grab the input tensor | ||
const Tensor& input_tensor = context->input(0); | ||
auto input = input_tensor.flat<float>(); | ||
|
||
// Create an output tensor | ||
Tensor* output_tensor = NULL; | ||
OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(), | ||
&output_tensor)); | ||
auto output = output_tensor->flat<float>(); | ||
|
||
// Set all but the first element of the output tensor to 0. | ||
const int N = input.size(); | ||
const int maxActive = (int) ceil(N * 0.2f); | ||
|
||
priority_queue<tuple<int, int>> keep; | ||
|
||
for (int i = 0; i < N; i++) { | ||
auto score = input(i); | ||
|
||
if(keep.empty() || score > get<0>(keep.top())) { | ||
// We want to keep this | ||
if(keep.size() >= maxActive) { | ||
// Replace lowest score with this one | ||
keep.pop(); | ||
} | ||
|
||
keep.push(make_tuple(score, i)); | ||
} | ||
} | ||
|
||
// Create a set of indicies to keep | ||
unordered_set<int> keepIndicies; | ||
|
||
while(!keep.empty()) { | ||
keepIndicies.insert(get<1>(keep.top())); | ||
keep.pop(); | ||
} | ||
|
||
// Handle output | ||
for (int i = 0; i < N; i++) { | ||
if(keepIndicies.find(i) != keepIndicies.end()) | ||
output(i) = 1; | ||
else | ||
output(i) = 0; | ||
} | ||
} | ||
}; | ||
|
||
REGISTER_KERNEL_BUILDER(Name("Inhibit").Device(DEVICE_CPU), InhibitOp); |
This file contains 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