I get results in the form of tensor from a model and I want to save the result in a .csv file. I use torch::save(input_tensors2.data(), “./tensor_test.csv”); but the .csv file can not be opened. Anyone can give some suggestions? Thanks.

You could get the numpy array, create a `pandas.DataFrame`

and save it to a `csv`

via:

```
import torch
import pandas as pd
import numpy as np
x = torch.randn(1)
x_np = x.numpy()
x_df = pd.DataFrame(x_np)
x_df.to_csv('tmp.csv')
```

In C++, you will probably have to write your own, assuming your tensor contains results from N batches and you wanted to write it as one sample per row, something like this could work:

```
void SaveCsv(torch::Tensor t, const std::string& filepath, const std::string& separator = ",")
{
t = t.flatten(1).contiguous().cpu();
float* ptr = t.data_ptr<float>();
std::ofstream csvHandle(filepath);
for (size_t i = 0; i < t.sizes()[0]; ++i)
{
for (size_t k = 0; k < t.sizes()[1]; ++k)
{
csvHandle << *ptr++;
if (k < (t.sizes()[1] - 1))
{
csvHandle << separator;
}
}
csvHandle << "\n";
}
}
void main()
{
try
{
auto t = torch::randn({ 3, 4, 5 });
SaveCsv(t, "D:\\test.csv");
}
catch (std::runtime_error& e)
{
std::cout << e.what() << std::endl;
}
catch (const c10::Error& e)
{
std::cout << e.msg() << std::endl;
}
system("PAUSE");
}
```