Sparsify Tensor of shape [N, 1]. Return the indices and values vectors for non-zero tensor content.
| 38 | // Sparsify Tensor of shape [N, 1]. Return the indices and values vectors for |
| 39 | // non-zero tensor content. |
| 40 | Status SparsifyWeights(const Tensor& tensor, Tensor* indices_tensor, |
| 41 | Tensor* values_tensor) { |
| 42 | if (tensor.dims() != 2 || tensor.dim_size(1) != 1) { |
| 43 | return tensorflow::errors::FailedPrecondition( |
| 44 | "Transform only applicable to subgraph with 'Const' with " |
| 45 | "tensor of shape [N, 1]. But instead get shape ", |
| 46 | tensor.shape().DebugString(), "."); |
| 47 | } |
| 48 | |
| 49 | auto flat = tensor.flat<float>(); |
| 50 | std::vector<int64> indices; |
| 51 | std::vector<float> values; |
| 52 | |
| 53 | for (int64 i = 0; i < flat.size(); i++) { |
| 54 | float val = flat(i); |
| 55 | if (std::abs(val) >= 1.0e-5) { |
| 56 | indices.push_back(i); |
| 57 | values.push_back(val); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // During model initialization, InitializeTableOp makes use of |
| 62 | // KeyValueTensorIterator, which does not accept empty keys or values. |
| 63 | // Consequently, adding a dummy pair of indices and values as a walkaround. |
| 64 | if (indices.empty() || values.empty()) { |
| 65 | indices.push_back(0); |
| 66 | values.push_back(0); |
| 67 | } |
| 68 | *indices_tensor = Tensor(DataTypeToEnum<int64>::value, |
| 69 | {static_cast<int64>(indices.size())}); |
| 70 | std::copy_n(indices.begin(), indices.size(), |
| 71 | indices_tensor->flat<int64>().data()); |
| 72 | |
| 73 | *values_tensor = |
| 74 | Tensor(DataTypeToEnum<float>::value, {static_cast<int64>(values.size())}); |
| 75 | std::copy_n(values.begin(), values.size(), |
| 76 | values_tensor->flat<float>().data()); |
| 77 | |
| 78 | return Status::OK(); |
| 79 | } |
| 80 | |
| 81 | void CreateConstNode(const Tensor& tensor, const string& name, |
| 82 | NodeDef* node_def) { |
no test coverage detected