# Double\* to tensor and back

**URL:** <https://discuss.pytorch.org/t/double-to-tensor-and-back/81449>\
**Category:** C++\
**Created:** [May 15, 2020, 11:10pm UTC](https://discuss.pytorch.org/t/double-to-tensor-and-back/81449 "2020-05-15T23:10:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![teddykoker](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/teddykoker/32/30011_2.png) [@teddykoker](https://discuss.pytorch.org/u/teddykoker)\
**Post date:** [May 15, 2020, 11:10pm UTC](https://discuss.pytorch.org/t/double-to-tensor-and-back/81449/1 "2020-05-15T23:10:01Z")

</div>

I have a model that excepts an input of shape `[1, 1, 1000]`. I am creating a real-time application in which I would like to feed in a buffer (of type `double*`) into the model, and then write the output back into the buffer. I am having trouble with the `double*` to `torch::Tensor` conversion and back.

Example **Python** :

```python
input = torch.rand(1, 1, 1000)
input = model(input)

```

**C++** :

```cpp
double* input; // written to by some other function
int len = 1000;
torch::jit::script::Module model; // loaded in from torchscript
/* 
Now how do I convert input into a tensor of shape [1, 1, len],
pass it through the model, and convert it back into an array of
type double*
*/

// EDIT: figured out conversion to tensor
std::vector<torch::jit::IValue> input_tensors;
input_tensors.push_back(torch::from_blob(input, {1, 1, len}, torch::kFloat64));
torch::Tensor output_tensor = model.forward(input_tensors).toTensor();
// now how do copy back into double*

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![glaringlee](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@glaringlee](https://discuss.pytorch.org/u/glaringlee)\
**Post date:** [May 15, 2020, 11:24pm UTC](https://discuss.pytorch.org/t/double-to-tensor-and-back/81449/2 "2020-05-15T23:24:33Z")

</div>

I think you can use torch::from\_blob.  
Take a look at the comment in this old issue  
‘[https://github.com/pytorch/pytorch/issues/37201](https://github.com/pytorch/pytorch/issues/37201)’  
You need to specify torch::kFloat64 as the input type in torch::from\_blob in your case.

---

<div class="post-metadata">

**Author:** ![teddykoker](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/teddykoker/32/30011_2.png) [@teddykoker](https://discuss.pytorch.org/u/teddykoker)\
**Post date:** [May 15, 2020, 11:54pm UTC](https://discuss.pytorch.org/t/double-to-tensor-and-back/81449/3 "2020-05-15T23:54:06Z")

</div>

Thanks! Using `from_blob` I can create a tensor from `double*`, now how would I get it back into `double*` after passing through the model?

---

<div class="post-metadata">

**Author:** ![teddykoker](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/teddykoker/32/30011_2.png) [@teddykoker](https://discuss.pytorch.org/u/teddykoker)\
**Post date:** [May 16, 2020, 2:18am UTC](https://discuss.pytorch.org/t/double-to-tensor-and-back/81449/4 "2020-05-16T02:18:13Z")

</div>

I think I figured it out: I can use `std::memcpy()`:

```cpp
double* input; // address of input
double* output; // address of output
int len = 1000; // length of input and output buffers
torch::jit::script::Module model; // model loaded in from torchscript

// Convert input to tensor
std::vector<torch::jit::IValue> input_tensors;
input_tensors.push_back(torch::from_blob(input, {1, 1, len}, torch::kFloat64));

// Forward pass
torch::Tensor output_tensor = model.forward(input_tensors).toTensor();

// Copy into output
std::memcpy(output_tensor.data_ptr(), output, sizeof(double)*len);

```
