如何在 libtorch 中将 2d 张量修改为 3d?
How to modify a 2d tensor to 3d in libtorch?
我有一个二维数组vector,我已经把它转化为tensor,但是如何修改tensor的维数,我想把维数从2d修改为3d?
std::vector<std::vector<float>> voice(434, std::vector<float>(80))
ifstream fp("data.txt");
if (! fp) {
cout << "Error, file couldn't be opened" << endl;
return 1;
}
for(int i=0;i<80;i++)
{
for(int j=0;j<434;j++)
{
if ( ! fp )
{
std::cout << "read error" << std::endl;
break;
}
fp >> voice[i][j]
}
}
auto options = torch::TensorOptions().dtype(at::kDouble);
auto tensor = torch::zeros({80,434}, options);
for (int i = 0; i < 80; i++)
tensor.slice(0, i,i+1) = torch::from_blob(vect[i].data(), {434}, options);
现在 tensor
是 80 * 434,我如何在这个 tensor
中添加一维到 3d,我想要 1 * 80 * 434
auto tensor = torch::zeros({80,434}, options);
后面是这一行
auto tensor = tensor.view({1, 80, 434});
我建议在第二行中创建另一个变量而不是 tensor
,例如:
auto transformed_tensor = tensor.view({1, 80, 434});
我有一个二维数组vector
std::vector<std::vector<float>> voice(434, std::vector<float>(80))
ifstream fp("data.txt");
if (! fp) {
cout << "Error, file couldn't be opened" << endl;
return 1;
}
for(int i=0;i<80;i++)
{
for(int j=0;j<434;j++)
{
if ( ! fp )
{
std::cout << "read error" << std::endl;
break;
}
fp >> voice[i][j]
}
}
auto options = torch::TensorOptions().dtype(at::kDouble);
auto tensor = torch::zeros({80,434}, options);
for (int i = 0; i < 80; i++)
tensor.slice(0, i,i+1) = torch::from_blob(vect[i].data(), {434}, options);
现在 tensor
是 80 * 434,我如何在这个 tensor
中添加一维到 3d,我想要 1 * 80 * 434
auto tensor = torch::zeros({80,434}, options);
后面是这一行
auto tensor = tensor.view({1, 80, 434});
我建议在第二行中创建另一个变量而不是 tensor
,例如:
auto transformed_tensor = tensor.view({1, 80, 434});