Search in sources :

Example 6 with DoubleTensor

use of com.alibaba.alink.common.linalg.tensor.DoubleTensor in project Alink by alibaba.

the class TFTensorConversionUtilsTest method testFromTFTensor.

@Test
public void testFromTFTensor() {
    Tensor<TFloat64> tensor = TFloat64.tensorOf(StdArrays.ndCopyOf(new double[][] { { 1., 2. }, { 3., 4. } }));
    DoubleTensor doubleTensor = (DoubleTensor) TFTensorConversionUtils.fromTFTensor(tensor);
    Assert.assertEquals(DataType.DOUBLE, doubleTensor.getType());
    Assert.assertArrayEquals(new long[] { 2, 2 }, doubleTensor.shape());
    double[] arr = new double[4];
    DoubleDataBuffer buffer = DataBuffers.of(arr, false, false);
    TensorInternalUtils.getTensorData(doubleTensor).read(buffer);
    Assert.assertArrayEquals(new double[] { 1., 2., 3., 4. }, arr, 1e-9);
}
Also used : DoubleTensor(com.alibaba.alink.common.linalg.tensor.DoubleTensor) DoubleDataBuffer(org.tensorflow.ndarray.buffer.DoubleDataBuffer) TFloat64(org.tensorflow.types.TFloat64) Test(org.junit.Test)

Example 7 with DoubleTensor

use of com.alibaba.alink.common.linalg.tensor.DoubleTensor in project Alink by alibaba.

the class KerasSequentialClassifierBatchOpTest method testInputWithTensorType.

@Category(DLTest.class)
@Test
public void testInputWithTensorType() throws Exception {
    Random random = new Random();
    int n = 1000;
    int nTimesteps = 200;
    int nvars = 3;
    List<Row> rows = new ArrayList<>();
    for (int k = 0; k < n; k += 1) {
        double[][] xArr = new double[nTimesteps][nvars];
        for (int i = 0; i < nTimesteps; i += 1) {
            for (int j = 0; j < nvars; j += 1) {
                xArr[i][j] = random.nextFloat();
            }
        }
        DoubleTensor x = new DoubleTensor(xArr);
        int label = random.nextInt(2);
        rows.add(Row.of(x, label));
    }
    BatchOperator<?> source = new MemSourceBatchOp(rows, "tensor DOUBLE_TENSOR, label int");
    KerasSequentialClassifierTrainBatchOp trainBatchOp = new KerasSequentialClassifierTrainBatchOp().setTensorCol("tensor").setLabelCol("label").setLayers(new String[] { "Conv1D(256, 5, padding='same', activation='relu')", "Conv1D(128, 5, padding='same', activation='relu')", "Dropout(0.1)", "MaxPooling1D(pool_size=8)", "Conv1D(128, 5, padding='same', activation='relu')", "Conv1D(128, 5, padding='same', activation='relu')", "Flatten()" }).setOptimizer("Adam()").setNumEpochs(1).linkFrom(source);
    KerasSequentialClassifierPredictBatchOp predictBatchOp = new KerasSequentialClassifierPredictBatchOp().setPredictionCol("pred").setPredictionDetailCol("pred_detail").setReservedCols("label").linkFrom(trainBatchOp, source);
    predictBatchOp.lazyPrint(10);
    BatchOperator.execute();
}
Also used : MemSourceBatchOp(com.alibaba.alink.operator.batch.source.MemSourceBatchOp) DoubleTensor(com.alibaba.alink.common.linalg.tensor.DoubleTensor) Random(java.util.Random) ArrayList(java.util.ArrayList) Row(org.apache.flink.types.Row) Category(org.junit.experimental.categories.Category) Test(org.junit.Test) DLTest(com.alibaba.alink.testutil.categories.DLTest)

Example 8 with DoubleTensor

use of com.alibaba.alink.common.linalg.tensor.DoubleTensor in project Alink by alibaba.

the class VectorToTensorMapperTest method testFloatType.

@Test
public void testFloatType() throws Exception {
    final Mapper mapper = new VectorToTensorMapper(new TableSchema(new String[] { "vec" }, new TypeInformation<?>[] { VectorTypes.DENSE_VECTOR }), new Params().set(VectorToTensorParams.SELECTED_COL, "vec").set(VectorToTensorParams.TENSOR_SHAPE, new Long[] { 2L, 3L }).set(VectorToTensorParams.TENSOR_DATA_TYPE, DataType.FLOAT));
    Assert.assertEquals(TensorTypes.FLOAT_TENSOR, mapper.getOutputSchema().getFieldTypes()[0]);
    final DoubleTensor tensor = DoubleTensor.of(TensorUtil.getTensor("FLOAT#6#0.0 0.1 1.0 1.1 2.0 2.1 "));
    final FloatTensor expect = FloatTensor.of(tensor.reshape(new Shape(2L, 3L)));
    final Tensor<?> result = (Tensor<?>) mapper.map(Row.of(tensor.toVector())).getField(0);
    Assert.assertEquals(expect, result);
}
Also used : Mapper(com.alibaba.alink.common.mapper.Mapper) DoubleTensor(com.alibaba.alink.common.linalg.tensor.DoubleTensor) Shape(com.alibaba.alink.common.linalg.tensor.Shape) DoubleTensor(com.alibaba.alink.common.linalg.tensor.DoubleTensor) Tensor(com.alibaba.alink.common.linalg.tensor.Tensor) FloatTensor(com.alibaba.alink.common.linalg.tensor.FloatTensor) TableSchema(org.apache.flink.table.api.TableSchema) VectorToTensorParams(com.alibaba.alink.params.dataproc.VectorToTensorParams) Params(org.apache.flink.ml.api.misc.param.Params) FloatTensor(com.alibaba.alink.common.linalg.tensor.FloatTensor) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) Test(org.junit.Test)

Example 9 with DoubleTensor

use of com.alibaba.alink.common.linalg.tensor.DoubleTensor in project Alink by alibaba.

the class VectorToTensorMapperTest method testReshape.

@Test
public void testReshape() throws Exception {
    final Mapper mapper = new VectorToTensorMapper(new TableSchema(new String[] { "vec" }, new TypeInformation<?>[] { VectorTypes.DENSE_VECTOR }), new Params().set(VectorToTensorParams.SELECTED_COL, "vec").set(VectorToTensorParams.TENSOR_SHAPE, new Long[] { 2L, 3L }));
    final DoubleTensor tensor = DoubleTensor.of(TensorUtil.getTensor("FLOAT#6#0.0 0.1 1.0 1.1 2.0 2.1 "));
    final DoubleTensor expect = tensor.reshape(new Shape(2L, 3L));
    final Tensor<?> result = (Tensor<?>) mapper.map(Row.of(tensor.toVector())).getField(0);
    Assert.assertEquals(expect, result);
}
Also used : Mapper(com.alibaba.alink.common.mapper.Mapper) DoubleTensor(com.alibaba.alink.common.linalg.tensor.DoubleTensor) Shape(com.alibaba.alink.common.linalg.tensor.Shape) DoubleTensor(com.alibaba.alink.common.linalg.tensor.DoubleTensor) Tensor(com.alibaba.alink.common.linalg.tensor.Tensor) FloatTensor(com.alibaba.alink.common.linalg.tensor.FloatTensor) TableSchema(org.apache.flink.table.api.TableSchema) VectorToTensorParams(com.alibaba.alink.params.dataproc.VectorToTensorParams) Params(org.apache.flink.ml.api.misc.param.Params) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) Test(org.junit.Test)

Example 10 with DoubleTensor

use of com.alibaba.alink.common.linalg.tensor.DoubleTensor in project Alink by alibaba.

the class VectorToTensorMapperTest method testOp.

@Test
public void testOp() throws Exception {
    final DoubleTensor tensor = DoubleTensor.of(TensorUtil.getTensor("FLOAT#6#0.0 0.1 1.0 1.1 2.0 2.1 "));
    final FloatTensor expect = FloatTensor.of(tensor.reshape(new Shape(2L, 3L)));
    Row[] rows = new Row[] { Row.of(tensor.toVector()) };
    MemSourceBatchOp memSourceBatchOp = new MemSourceBatchOp(rows, new String[] { "vec" });
    memSourceBatchOp.link(new VectorToTensorBatchOp().setSelectedCol("vec").setTensorShape(2, 3).setTensorDataType("float")).lazyCollect(new Consumer<List<Row>>() {

        @Override
        public void accept(List<Row> rows) {
            Assert.assertEquals(expect, TensorUtil.getTensor(rows.get(0).getField(0)));
        }
    });
    BatchOperator.execute();
}
Also used : MemSourceBatchOp(com.alibaba.alink.operator.batch.source.MemSourceBatchOp) DoubleTensor(com.alibaba.alink.common.linalg.tensor.DoubleTensor) Shape(com.alibaba.alink.common.linalg.tensor.Shape) FloatTensor(com.alibaba.alink.common.linalg.tensor.FloatTensor) List(java.util.List) Row(org.apache.flink.types.Row) VectorToTensorBatchOp(com.alibaba.alink.operator.batch.dataproc.VectorToTensorBatchOp) Test(org.junit.Test)

Aggregations

DoubleTensor (com.alibaba.alink.common.linalg.tensor.DoubleTensor)21 Test (org.junit.Test)18 FloatTensor (com.alibaba.alink.common.linalg.tensor.FloatTensor)15 StringTensor (com.alibaba.alink.common.linalg.tensor.StringTensor)10 Mapper (com.alibaba.alink.common.mapper.Mapper)10 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)10 Params (org.apache.flink.ml.api.misc.param.Params)10 TableSchema (org.apache.flink.table.api.TableSchema)10 Tensor (com.alibaba.alink.common.linalg.tensor.Tensor)9 Shape (com.alibaba.alink.common.linalg.tensor.Shape)8 ToTensorParams (com.alibaba.alink.params.dataproc.ToTensorParams)7 Row (org.apache.flink.types.Row)5 BoolTensor (com.alibaba.alink.common.linalg.tensor.BoolTensor)4 IntTensor (com.alibaba.alink.common.linalg.tensor.IntTensor)4 LongTensor (com.alibaba.alink.common.linalg.tensor.LongTensor)4 MemSourceBatchOp (com.alibaba.alink.operator.batch.source.MemSourceBatchOp)4 Tensor (org.tensorflow.Tensor)4 TFloat64 (org.tensorflow.types.TFloat64)4 VectorToTensorParams (com.alibaba.alink.params.dataproc.VectorToTensorParams)3 List (java.util.List)3