Exemple #1
0
def make_sparse_tensor(values: TensorProto, indices: TensorProto,
                       dims: Sequence[int]) -> SparseTensorProto:
    sparse = SparseTensorProto()
    sparse.values.CopyFrom(values)
    sparse.indices.CopyFrom(indices)
    sparse.dims.extend(dims)
    return sparse
Exemple #2
0
def make_sparse_tensor(
    values,  # type: TensorProto
    indices,  # type: TensorProto
    dims  # type: Sequence[int]
):  # type: (...) -> SparseTensorProto
    sparse = SparseTensorProto()
    sparse.values.CopyFrom(values)
    sparse.indices.CopyFrom(indices)
    sparse.dims.extend(dims)
    return sparse
def _build_random_sparse_tensor(shape, sparsity):
    array_size = numpy.prod(shape)
    num_sparse = int(array_size * sparsity)
    array = numpy.random.randn(num_sparse)
    indices = numpy.random.choice(
        numpy.arange(array_size), replace=False, size=num_sparse
    ).astype(numpy.int64)
    return SparseTensorProto(
        values=numpy_helper.from_array(array),
        indices=numpy_helper.from_array(indices),
        dims=shape,
    )
Exemple #4
0
 def make_sparse(self,
                 shape,  # type: Sequence[int]
                 values,  # type: Sequence[int]
                 indices_shape,  # type: Sequence[int]
                 indices  # type: Sequence[int]
                 ):  # type: (...) -> SparseTensorProto
     sparse = SparseTensorProto()
     sparse.dims.extend(shape)
     nnz = len(values)
     sparse.values.CopyFrom(helper.make_tensor('spval', TensorProto.INT64, (nnz,), values))
     sparse.indices.CopyFrom(helper.make_tensor('spind', TensorProto.INT64, indices_shape, indices))
     return sparse
Exemple #5
0
    def make_sparse(self,
                    shape: Sequence[int],
                    values: Sequence[int],
                    indices_shape: Sequence[int],
                    indices: Sequence[int],
                    name: Text = 'spval'
                    ) -> SparseTensorProto:
        sparse = SparseTensorProto()
        sparse.dims.extend(shape)
        nnz = len(values)

        sparse.values.CopyFrom(helper.make_tensor(name, TensorProto.INT64, (nnz,), values))
        sparse.indices.CopyFrom(helper.make_tensor('spind', TensorProto.INT64, indices_shape, indices))
        return sparse
Exemple #6
0
def make_sparse_tensor(values: TensorProto, indices: TensorProto,
                       dims: Sequence[int]) -> SparseTensorProto:
    """Construct a SparseTensorProto

    Arguments:
        values (TensorProto): the values
        indices (TensorProto): the indices
        dims: the shape

    Returns:
        SparseTensorProto
    """
    sparse = SparseTensorProto()
    sparse.values.CopyFrom(values)
    sparse.indices.CopyFrom(indices)
    sparse.dims.extend(dims)
    return sparse
Exemple #7
0
def check_sparse_tensor(sparse: SparseTensorProto,
                        ctx: C.CheckerContext = DEFAULT_CONTEXT) -> None:
    C.check_sparse_tensor(sparse.SerializeToString(), ctx)