Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to create tensor from java/scala normal array 1d 2d 3d array , #1558

Open
mullerhai opened this issue Dec 28, 2024 · 7 comments
Open

how to create tensor from java/scala normal array 1d 2d 3d array , #1558

mullerhai opened this issue Dec 28, 2024 · 7 comments

Comments

@mullerhai
Copy link

HI, I want to create Tensor ,but make me confuse ,some func is not work ,in pytorch ,we create tensor very easy ,why in javacpp not easy. someone tell me use ones or some func ,but it is now work ,console all 1
my code ,but no correct ,make jvm down

  val data = Array[Float](13, 42, 23, 14, 15,45)
//  val t = org.bytedeco.javacpp.torch.empty(Array[Long](193, 102, 23), new org.bytedeco.pytorch.ScalarTypeOptional(torch.ScalarType.Float), new org.bytedeco.pytorch.LayoutOptional(torch.Layout.Strided), new org.bytedeco.pytorch.DeviceOptional(CPU), new org.bytedeco.pytorch.BoolOptional(false), // pinned or not
//    new org.bytedeco.pytorch.MemoryFormatOptional(org.bytedeco.pytorch.torch.MemoryFormat.Contiguous))
//  t.numel()
  val floatT = new org.bytedeco.javacpp.FloatPointer(data: _*)
  val tensor = new org.bytedeco.pytorch.Tensor(floatT)
  val dataPtr = new org.bytedeco.javacpp.LongPointer(tensor.data_ptr)
  val features8 = new Array[Float](6)
  val fp8 = new org.bytedeco.javacpp.FloatPointer(dataPtr)
  fp8.get(features8)
  println(s"tensor data ptr get ${features8.mkString(", ")}")
@saudet
Copy link
Member

saudet commented Dec 28, 2024

Using Tensor.create() is probably the easiest way. Are you having issues with that method?

@mullerhai
Copy link
Author

Using Tensor.create() is probably the easiest way. Are you having issues with that method?

15.1 we not have this function , in pytorch .we could use new_tensor or full or constant ,now in javacpp we not have ,so import ant func to create some data from normal array ,

@HGuillemet
Copy link
Collaborator

  import static org.bytedeco.pytorch.global.torch.*;
  Tensor data = tensor(13F, 42F, 23F, 14F, 15F, 45F);
  Tensor empty =  empty(5);
  Tensor full  = full(new long[] { 4 }, new Scalar(12));

Javacpp pytorch is a binding to C++ libtorch, not to Python pytorch. When looking for documentation or sample code, look first at libtorch documentation.

@mullerhai
Copy link
Author

  import static org.bytedeco.pytorch.global.torch.*;
  Tensor data = tensor(13F, 42F, 23F, 14F, 15F, 45F);
  Tensor empty =  empty(5);
  Tensor full  = full(new long[] { 4 }, new Scalar(12));

Javacpp pytorch is a binding to C++ libtorch, not to Python pytorch. When looking for documentation or sample code, look first at libtorch documentation.

thanks .I found this is also ok
···
val data = Array[Float](13, 42, 23, 14, 15, 45)
val datas = Array(data,data,data)
val CPU = new Device(torch.DeviceType.CPU)
val layout = torch.Layout.Strided
val requiresGrad = false
val inputDType = torch.ScalarType.Float
val tensorOptions =TensorOptions()
.dtype(ScalarTypeOptional(inputDType))
.layout(LayoutOptional(layout))
.device(DeviceOptional(CPU))
.requires_grad(BoolOptional(requiresGrad))
// tensorOptions.layout(). inputDType, layout, CPU, requiresGrad)
val point = new FloatPointer(FloatBuffer.wrap(data))
val t = torch.from_blob(point, Array(datas.length.toLong),tensorOptions)//, torch.float32, torch.device(CPU))
// val t = torch.rand(Array[Long](193, 102, 23):_*)//,new Scalar(), new ScalarTypeOptional(torch.ScalarType.Float), new LayoutOptional(torch.Layout.Strided), new DeviceOptional(CPU), new BoolOptional(false))//, // pinned or not
// new MemoryFormatOptional(torch.MemoryFormat.Contiguous))
t.numel()
val dataPtr = new LongPointer(t.data_ptr)
val fp8 = new FloatPointer(dataPtr)

···

@mullerhai
Copy link
Author

  import static org.bytedeco.pytorch.global.torch.*;
  Tensor data = tensor(13F, 42F, 23F, 14F, 15F, 45F);
  Tensor empty =  empty(5);
  Tensor full  = full(new long[] { 4 }, new Scalar(12));

Javacpp pytorch is a binding to C++ libtorch, not to Python pytorch. When looking for documentation or sample code, look first at libtorch documentation.

but for 2D array Array[Array[Long]] or 3D Array Array[ Array[Array[Long]]], how to use tensor() method, I try is fault

    val v = Array(13F, 423F, 23F, 142F, 15F, 45F)
    val vz = Array(v,v)
    val vzs= Array(vz,vz)
    val t = tensor(vzs:_*)  //fault 
//    val longRef = new LongArrayRef(v, data.length.toLong)
    val t = tensor(vzs.flatten.flatten:_*).view(2,6,2) // right

@HGuillemet
Copy link
Collaborator

Create 1D tensor and reshape

@mullerhai
Copy link
Author

Create 1D tensor and reshape

OK, thanks, the java**dataset pass data path ,generate tensor batch could you write an example,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants