如何在libtorch中重复张量
我正在使用libtorch
进行推理,我已将数据从txt文件读取到向量
并转换为张量
,我想重复张量
三次,然后将其更改为 3D, 我试过了,
std::vector<std::vector<float>> feature_data(255, std::vector<float>(221));
ifstream f_data("../data.txt"); //
if (! f_data) {
cout << "Error, file couldn't be opened" << endl;
return 1;
}
for(int i=0;i<255;i++)
{
for(int j=0;j<221;j++)
{
if ( !f_data )
{
std::cout << "read error" << std::endl;
break;
}
f_data >> feature_data[i][j];
}
}
auto data_options = torch::TensorOptions().dtype(at::kFloat);
auto feature_tensor = torch::zeros({255,221}, data_options);
for (int i = 0; i < 255; i++)
feature_tensor.slice(0, i,i+1) = torch::from_blob(feature_data[i].data(), {221},
data_options);
// begin to repeat three times
auto tensor_clone = feature_tensor.clone();
auto one_time_clone = torch::cat({feature_tensor, tensor_clone}, 0);
auto two_times_clone = torch::cat({one_time_clone, tensor_clone}, 0);
auto transformed_asr = two_times_clone.view({3, 255, 221});
看起来很麻烦,不知道是否正确,有没有简单的方法?
I am using libtorch
to inference, I have read data from txt file to vector
and convert to tensor
, I want to repeat a tensor
three times then change it to 3D,
I tried this
std::vector<std::vector<float>> feature_data(255, std::vector<float>(221));
ifstream f_data("../data.txt"); //
if (! f_data) {
cout << "Error, file couldn't be opened" << endl;
return 1;
}
for(int i=0;i<255;i++)
{
for(int j=0;j<221;j++)
{
if ( !f_data )
{
std::cout << "read error" << std::endl;
break;
}
f_data >> feature_data[i][j];
}
}
auto data_options = torch::TensorOptions().dtype(at::kFloat);
auto feature_tensor = torch::zeros({255,221}, data_options);
for (int i = 0; i < 255; i++)
feature_tensor.slice(0, i,i+1) = torch::from_blob(feature_data[i].data(), {221},
data_options);
// begin to repeat three times
auto tensor_clone = feature_tensor.clone();
auto one_time_clone = torch::cat({feature_tensor, tensor_clone}, 0);
auto two_times_clone = torch::cat({one_time_clone, tensor_clone}, 0);
auto transformed_asr = two_times_clone.view({3, 255, 221});
it looks troublesome and I am not sure if it is right, is there an easy way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)