use of com.alibaba.alink.common.linalg.tensor.LongTensor in project Alink by alibaba.
the class TFExampleConversionV2 method javaToFeature.
/**
* convert java object to tensorflow feature.
*
* @param dt java object data type.
* @param val given java object.
* @return tensorflow feature.
*/
public static Feature javaToFeature(DataTypesV2 dt, Object val) {
Feature.Builder featureBuilder = Feature.newBuilder();
FloatList.Builder floatListBuilder = FloatList.newBuilder();
Int64List.Builder int64ListBuilder = Int64List.newBuilder();
// When dt is TENSOR, find the exact type first.
if (DataTypesV2.TENSOR.equals(dt)) {
if (val instanceof FloatTensor) {
dt = DataTypesV2.FLOAT_TENSOR;
} else if (val instanceof DoubleTensor) {
dt = DataTypesV2.DOUBLE_TENSOR;
} else if (val instanceof IntTensor) {
dt = DataTypesV2.INT_TENSOR;
} else if (val instanceof LongTensor) {
dt = DataTypesV2.LONG_TENSOR;
} else if (val instanceof BoolTensor) {
dt = DataTypesV2.BOOLEAN_TENSOR;
} else if (val instanceof UByteTensor) {
dt = DataTypesV2.UBYTE_TENSOR;
} else if (val instanceof StringTensor) {
dt = DataTypesV2.STRING_TENSOR;
} else if (val instanceof ByteTensor) {
dt = DataTypesV2.BYTE_TENSOR;
}
}
switch(dt) {
case FLOAT_16:
case FLOAT_32:
case FLOAT_64:
{
floatListBuilder.addValue((Float) val);
featureBuilder.setFloatList(floatListBuilder);
break;
}
case INT_8:
case INT_16:
case INT_32:
case INT_64:
case UINT_8:
case UINT_16:
case UINT_32:
case UINT_64:
{
int64ListBuilder.addValue(castAsLong(val));
featureBuilder.setInt64List(int64ListBuilder);
break;
}
case STRING:
{
BytesList.Builder bb = BytesList.newBuilder();
bb.addValue(castAsBytes(val));
featureBuilder.setBytesList(bb);
break;
}
case FLOAT_TENSOR:
{
FloatTensor floatTensor = (FloatTensor) val;
long size = floatTensor.size();
floatTensor = floatTensor.reshape(new Shape(size));
for (long i = 0; i < size; i += 1) {
floatListBuilder.addValue(floatTensor.getFloat(i));
}
featureBuilder.setFloatList(floatListBuilder);
break;
}
case DOUBLE_TENSOR:
{
DoubleTensor doubleTensor = (DoubleTensor) val;
long size = doubleTensor.size();
doubleTensor = doubleTensor.reshape(new Shape(size));
for (long i = 0; i < size; i += 1) {
floatListBuilder.addValue((float) doubleTensor.getDouble(i));
}
featureBuilder.setFloatList(floatListBuilder);
break;
}
case INT_TENSOR:
{
IntTensor intTensor = (IntTensor) val;
long size = intTensor.size();
intTensor = intTensor.reshape(new Shape(size));
for (long i = 0; i < size; i += 1) {
int64ListBuilder.addValue(intTensor.getInt(i));
}
featureBuilder.setInt64List(int64ListBuilder);
break;
}
case LONG_TENSOR:
{
LongTensor longTensor = (LongTensor) val;
long size = longTensor.size();
longTensor = longTensor.reshape(new Shape(size));
for (long i = 0; i < size; i += 1) {
int64ListBuilder.addValue(longTensor.getLong(i));
}
featureBuilder.setInt64List(int64ListBuilder);
break;
}
case BOOLEAN_TENSOR:
{
BoolTensor boolTensor = (BoolTensor) val;
long size = boolTensor.size();
boolTensor = boolTensor.reshape(new Shape(size));
for (long i = 0; i < size; i += 1) {
int64ListBuilder.addValue(boolTensor.getBoolean(i) ? 1 : 0);
}
featureBuilder.setInt64List(int64ListBuilder);
break;
}
case UBYTE_TENSOR:
{
UByteTensor ubyteTensor = (UByteTensor) val;
long size = ubyteTensor.size();
ubyteTensor = ubyteTensor.reshape(new Shape(size));
for (long i = 0; i < size; i += 1) {
int64ListBuilder.addValue(ubyteTensor.getUByte(i));
}
featureBuilder.setInt64List(int64ListBuilder);
break;
}
case STRING_TENSOR:
{
StringTensor stringTensor = (StringTensor) val;
long size = stringTensor.size();
stringTensor = stringTensor.reshape(new Shape(size));
BytesList.Builder bb = BytesList.newBuilder();
for (long i = 0; i < size; i += 1) {
bb.addValue(castAsBytes(stringTensor.getString(i)));
}
featureBuilder.setBytesList(bb);
break;
}
case BYTE_TENSOR:
default:
throw new RuntimeException("Unsupported data type for TF");
}
return featureBuilder.build();
}
use of com.alibaba.alink.common.linalg.tensor.LongTensor in project Alink by alibaba.
the class StringTFTensorConversionImpl method encodeBatchTensor.
@Override
public String[] encodeBatchTensor(Tensor<?> tensor, int batchAxis) {
long[] shape = tensor.shape().asArray();
long batchSize = shape[batchAxis];
if (TString.DTYPE.equals(tensor.dataType())) {
StringTensor stringTensor = (StringTensor) TFTensorConversionUtils.fromTFTensor(tensor);
StringTensor[] stringTensors = unstack(stringTensor, batchAxis, null);
return Arrays.stream(stringTensors).map(TFTensorConversionUtils::toTFTensor).map(TF2TensorUtils::squeezeTensor).map(d -> StdArrays.array1dCopyOf((TString) d.data(), String.class)).map(d -> String.join(SEP_CHAR, d)).toArray(String[]::new);
} else if (TBool.DTYPE.equals(tensor.dataType())) {
BoolTensor boolTensor = (BoolTensor) TFTensorConversionUtils.fromTFTensor(tensor);
BoolTensor[] boolTensors = unstack(boolTensor, batchAxis, null);
return Arrays.stream(boolTensors).map(TFTensorConversionUtils::toTFTensor).map(TF2TensorUtils::squeezeTensor).map(d -> StdArrays.array1dCopyOf((TBool) d.data())).map(d -> Booleans.join(SEP_CHAR, d)).toArray(String[]::new);
} else if (TInt32.DTYPE.equals(tensor.dataType())) {
IntTensor intTensor = (IntTensor) TFTensorConversionUtils.fromTFTensor(tensor);
IntTensor[] intTensors = unstack(intTensor, batchAxis, null);
return Arrays.stream(intTensors).map(TFTensorConversionUtils::toTFTensor).map(TF2TensorUtils::squeezeTensor).map(d -> StdArrays.array1dCopyOf((TInt32) d.data())).map(d -> Ints.join(SEP_CHAR, d)).toArray(String[]::new);
} else if (TInt64.DTYPE.equals(tensor.dataType())) {
LongTensor longTensor = (LongTensor) TFTensorConversionUtils.fromTFTensor(tensor);
LongTensor[] longTensors = unstack(longTensor, batchAxis, null);
return Arrays.stream(longTensors).map(TFTensorConversionUtils::toTFTensor).map(TF2TensorUtils::squeezeTensor).map(d -> StdArrays.array1dCopyOf((TInt64) d.data())).map(d -> Longs.join(SEP_CHAR, d)).toArray(String[]::new);
} else if (TFloat32.DTYPE.equals(tensor.dataType())) {
FloatTensor floatTensor = (FloatTensor) TFTensorConversionUtils.fromTFTensor(tensor);
FloatTensor[] floatTensors = unstack(floatTensor, batchAxis, null);
return Arrays.stream(floatTensors).map(TFTensorConversionUtils::toTFTensor).map(TF2TensorUtils::squeezeTensor).map(d -> StdArrays.array1dCopyOf((TFloat32) d.data())).map(d -> Floats.join(SEP_CHAR, d)).toArray(String[]::new);
} else if (TFloat64.DTYPE.equals(tensor.dataType())) {
DoubleTensor doubleTensor = (DoubleTensor) TFTensorConversionUtils.fromTFTensor(tensor);
DoubleTensor[] doubleTensors = unstack(doubleTensor, batchAxis, null);
return Arrays.stream(doubleTensors).map(TFTensorConversionUtils::toTFTensor).map(TF2TensorUtils::squeezeTensor).map(d -> StdArrays.array1dCopyOf((TFloat64) d.data())).map(d -> Doubles.join(SEP_CHAR, d)).toArray(String[]::new);
}
throw new UnsupportedOperationException("Unsupported dtype: " + tensor.dataType());
}
use of com.alibaba.alink.common.linalg.tensor.LongTensor in project Alink by alibaba.
the class TensorTFTensorConversionImpl method parseLongTensor.
@Override
public Tensor<TInt64> parseLongTensor(com.alibaba.alink.common.linalg.tensor.Tensor<?>[] tensors, long[] shape) {
LongTensor stackedTensor = new LongTensor(new com.alibaba.alink.common.linalg.tensor.Shape(shape));
stack(castArrayType(tensors, LongTensor[].class), 0, stackedTensor);
// noinspection unchecked
return (Tensor<TInt64>) TFTensorConversionUtils.toTFTensor(stackedTensor);
}
use of com.alibaba.alink.common.linalg.tensor.LongTensor in project Alink by alibaba.
the class TFSavedModelPredictMapperTest method testTensor.
@Category(DLTest.class)
@Test
public void testTensor() throws Exception {
AlinkGlobalConfiguration.setPrintProcessInfo(true);
PluginDownloader pluginDownloader = AlinkGlobalConfiguration.getPluginDownloader();
RegisterKey registerKey = TFPredictorClassLoaderFactory.getRegisterKey();
pluginDownloader.downloadPlugin(registerKey.getName(), registerKey.getVersion());
MLEnvironmentFactory.getDefault().getExecutionEnvironment().setParallelism(2);
int batchSize = 3;
List<Row> rows = new ArrayList<>();
for (int i = 0; i < 1000; i += 1) {
Row row = Row.of(new LongTensor((new Shape(batchSize))), new FloatTensor(new Shape(batchSize, 28, 28)));
rows.add(row);
}
BatchOperator<?> data = new MemSourceBatchOp(rows, "label LONG_TENSOR, image FLOAT_TENSOR");
Params params = new Params();
params.set(HasModelPath.MODEL_PATH, "http://alink-dataset.oss-cn-zhangjiakou.aliyuncs.com/tf/1551968314.zip");
params.set(HasSelectedCols.SELECTED_COLS, new String[] { "image" });
params.set(HasOutputSchemaStr.OUTPUT_SCHEMA_STR, "classes LONG_TENSOR, probabilities FLOAT_TENSOR");
TFSavedModelPredictMapper tfSavedModelPredictMapper = new TFSavedModelPredictMapper(data.getSchema(), params);
tfSavedModelPredictMapper.open();
Assert.assertEquals(TableSchema.builder().field("label", TensorTypes.LONG_TENSOR).field("image", TensorTypes.FLOAT_TENSOR).field("classes", TensorTypes.LONG_TENSOR).field("probabilities", TensorTypes.FLOAT_TENSOR).build(), tfSavedModelPredictMapper.getOutputSchema());
for (Row row : rows) {
Row output = tfSavedModelPredictMapper.map(row);
Assert.assertEquals(row.getField(0), output.getField(0));
Assert.assertEquals(row.getField(1), output.getField(1));
Assert.assertArrayEquals(((LongTensor) output.getField(2)).shape(), new long[] { batchSize });
Assert.assertArrayEquals(((FloatTensor) output.getField(3)).shape(), new long[] { batchSize, 10 });
}
tfSavedModelPredictMapper.close();
}
use of com.alibaba.alink.common.linalg.tensor.LongTensor in project Alink by alibaba.
the class TensorTFTensorConversionTest method testLongTensor.
@Test
public void testLongTensor() {
Random random = new Random(2021);
long[][] arr = new long[3][4];
for (int i = 0; i < 3; i += 1) {
for (int j = 0; j < 4; j += 1) {
arr[i][j] = random.nextLong();
}
}
LongTensor data = new LongTensor(arr);
Tensor<TInt64> std = TInt64.tensorOf(StdArrays.ndCopyOf(arr));
TensorInfo tensorInfo = createTensorInfo(DataType.DT_INT64, new long[] { 3, 4 });
// noinspection unchecked
Tensor<TInt64> tensor = (Tensor<TInt64>) TensorTFTensorConversionImpl.getInstance().parseTensor(data, tensorInfo);
Assert.assertArrayEquals(std.shape().asArray(), tensor.shape().asArray());
Assert.assertArrayEquals(StdArrays.array2dCopyOf(std.data()), StdArrays.array2dCopyOf(tensor.data()));
LongTensor encoded = (LongTensor) TensorTFTensorConversionImpl.getInstance().encodeTensor(tensor);
for (int i = 0; i < 3; i += 1) {
for (int j = 0; j < 4; j += 1) {
Assert.assertEquals(encoded.getLong(i, j), data.getLong(i, j));
}
}
}
Aggregations