将向量<张量流::张量>转换为张量的张量

Converting a vector<tensorflow::Tensor> to tensor of tensors

本文关键字:张量 转换 gt 张量流 向量 lt      更新时间:2024-09-29

假设我有一个图像张量的向量,每个图像张量的维度为[frames,height,width,num_channels],我想取该向量并将其转换为[num_tracks(向量的大小(,frames,high,widght,num_cannels]的一个更大的张量。使用tensorflow最简单的方法是什么::Tensorapi?这是为了构造图的输入张量,而不是在图执行本身中。

谢谢!

您可以创建一个具有所需形状的新张量,并通过迭代循环中的所有dims来填充它(要访问单个项目,请使用Eigen的TensorMapoperator(),您可以通过Tensor上的tensor<DataType,DIMS>获得(:

tensorflow::Tensor concat(const std::vector<tensorflow::Tensor>& in){
int frames = in[0].dim_size(0);
int height = in[0].dim_size(1);
int width = in[0].dim_size(2);
int num_channels = in[0].dim_size(3);

int num_tracks = in.size();

tensorflow::Tensor res(DT_FLOAT,tensorflow::TensorShape{num_tracks,frames,height,width,num_channels});
auto& resMap = res.tensor<float,5>();

for (int nt = 0; nt < num_tracks; ++nt) {
auto& inFrame = in[nt];
auto& inMap = inFrame.tensor<float,4>(); // Eigen's TensorMap which has operator()(Indices...)

for (int f = 0; f < frames; ++f) {
for (int r = 0; r < height; ++r) {
for (int c = 0; c < width; ++c) {
for (int ch = 0; ch < num_channels; ++ch) {
resMap(nt,f,r,c,ch) = inMap(f,r,c,ch);
}
}
}
}
}
return res;
}