@Override
    public TensorProto serialize() {
        TensorProto.Builder builder = TensorProto.newBuilder();

        builder.setVersion(CURRENT_VERSION);
        builder.setClassName(SparseVector.class.getName());

        SparseTensorProto.Builder dataBuilder = SparseTensorProto.newBuilder();
        dataBuilder.addAllDimensions(Arrays.stream(shape).boxed().collect(Collectors.toList()));
        ByteBuffer indicesBuffer = ByteBuffer.allocate(indices.length * 4).order(ByteOrder.LITTLE_ENDIAN);
        IntBuffer intBuffer = indicesBuffer.asIntBuffer();
        intBuffer.put(indices);
        intBuffer.rewind();
        ByteBuffer valuesBuffer = ByteBuffer.allocate(values.length * 8).order(ByteOrder.LITTLE_ENDIAN);
        DoubleBuffer doubleBuffer = valuesBuffer.asDoubleBuffer();
        doubleBuffer.put(values);
        doubleBuffer.rewind();
        dataBuilder.setIndices(ByteString.copyFrom(indicesBuffer));
        dataBuilder.setValues(ByteString.copyFrom(valuesBuffer));
        dataBuilder.setNumNonZero(values.length);
        builder.setSerializedData(Any.pack(dataBuilder.build()));

        return builder.build();
    }
@Test
    public void serializationTest() {
        SparseVector a = generateVectorA();
        TensorProto proto = a.serialize();
        Tensor deser = Tensor.deserialize(proto);
        assertEquals(a, deser);

        SparseVector empty = new SparseVector(10, new int[0], new double[0]);
        proto = empty.serialize();
        deser = Tensor.deserialize(proto);
        assertEquals(empty, deser);

        SparseVector full = new SparseVector(10, new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 5);
        proto = full.serialize();
        deser = Tensor.deserialize(proto);
        assertEquals(full, deser);

    }