如何将三个2D张量组合成张量流中的单个三维张量?

时间:2017-04-19 07:17:25

标签: python image tensorflow

您好我是tensorflow的新手,目前,我正在使用彩色图像,它是PCAS。

我以“红色”,“绿色”和“蓝色”的形式提取了PCAS,并计算了与“红色”,“绿色”和“蓝色”组件相关的权重。

完成上述所有操作后,我想将所有三个2D矩阵组合成单个3D矩阵。

对于张量流,它将是一个3D张量。

def multi(h0,ppca,mu,i,scope=None):

with tf.variable_scope(scope or"multi"):
        return tf.matmul(ppca[:,:,0],h0[i,:,:,0]) + tf.reshape(mu[:,0],[4096,1]) , tf.matmul(ppca[:,:,1],h0[i,:,:,1]) + tf.reshape(mu[:,1],[4096,1]) ,tf.matmul(ppca[:,:,2],h0[i,:,:,2]) + tf.reshape(mu[:,2],[4096,1]) 

所以从上面的函数中,我将获得所有三种不同的2D张量,并希望将这些2D张量组合成具有尺寸的单个3D张量[4096,1,3]

我该怎么做? 任何帮助都非常感谢。

3 个答案:

答案 0 :(得分:3)

你需要像这样连接它们:

three_d_image = tf.concat(0, [[r], [g], [b]])

这告诉tensorflow将它们沿x维度连接起来,并将每个张量视为矩阵。

在没有r,g,b张量周围的附加括号的情况下执行相同操作会尝试将它们连接到一个大的2D矩阵

答案 1 :(得分:3)

一个干净,简单的方法是使用tf.stack操作(旧版本的tensorflow中的tf.pack),它会沿着一个新维度连接所有张量。如果您希望新维度在之前完成,则需要将axis参数设置为张量的维数。

    three_d_image = tf.stack([r,g,b], axis=2)

答案 2 :(得分:0)

其中一个解决方案是你可以为你的2D添加一个空的维度,这样你将有3个3D维矩阵[4096,1,1],然后你可以通过轴2 public class KeyboardPagerAdapter extends PagerAdapter{ // This holds all the currently displayable views, in order from left to right. private ArrayList<View> views = new ArrayList<View>(); //----------------------------------------------------------------------------- // Used by ViewPager. "Object" represents the page; tell the ViewPager where the // page should be displayed, from left-to-right. If the page no longer exists, // return POSITION_NONE. @Override public int getItemPosition (Object object) { int index = views.indexOf (object); if (index == -1) return POSITION_NONE; else return index; } // Used by ViewPager. Called when ViewPager needs a page to display; it is our job // to add the page to the container, which is normally the ViewPager itself. Since // all our pages are persistent, we simply retrieve it from our "views" ArrayList. @Override public Object instantiateItem (ViewGroup container, int position) { View v = views.get (position); container.addView (v); return v; } // Used by ViewPager. Called when ViewPager no longer needs a page to display; it // is our job to remove the page from the container, which is normally the // ViewPager itself. Since all our pages are persistent, we do nothing to the // contents of our "views" ArrayList. @Override public void destroyItem (ViewGroup container, int position, Object object) { container.removeView (views.get (position)); } // Used by ViewPager; can be used by app as well. // Returns the total number of pages that the ViewPage can display. This must // never be 0. @Override public int getCount () { return views.size(); } // Used by ViewPager. @Override public boolean isViewFromObject (View view, Object object) { return view == object; } // Add "view" to right end of "views". // Returns the position of the new view. // The app should call this to add pages; not used by ViewPager. public int addView (View v) { return addView (v, views.size()); } //----------------------------------------------------------------------------- // Add "view" at "position" to "views". // Returns position of new view. // The app should call this to add pages; not used by ViewPager. public int addView (View v, int position) { views.add (position, v); return position; } //----------------------------------------------------------------------------- // Removes "view" from "views". // Retuns position of removed view. // The app should call this to remove pages; not used by ViewPager. public int removeView (ViewPager pager, View v) { return removeView (pager, views.indexOf (v)); } //----------------------------------------------------------------------------- // Removes the "view" at "position" from "views". // Retuns position of removed view. // The app should call this to remove pages; not used by ViewPager. public int removeView (ViewPager pager, int position) { // ViewPager doesn't have a delete method; the closest is to set the adapter // again. When doing so, it deletes all its views. Then we can delete the view // from from the adapter and finally set the adapter to the pager again. Note // that we set the adapter to null before removing the view from "views" - that's // because while ViewPager deletes all its views, it will call destroyItem which // will in turn cause a null pointer ref. pager.setAdapter (null); views.remove (position); pager.setAdapter (this); return position; } //----------------------------------------------------------------------------- // Returns the "view" at "position". // The app should call this to retrieve a view; not used by ViewPager. public View getView (int position) { return views.get (position); } } 连接这3个矩阵。给你[4096,1,3]

第二个解决方案可以是轴1的连续,tf.concat(2,matrices)然后将其重新整形为3D