TensorFlow学习笔记(十八)tf.reshape矩阵变形

来源:互联网 发布:淘宝网首页登陆电脑版 编辑:程序博客网 时间:2024/06/11 22:48

tf.reshape(tensor, shape, name=None)

矩阵变形是常用的操作,在Tensorflow中调用方式有多种,例如:

1. tf.reshape

  1. tf.reshape(L3, [-1, W4.get_shape().as_list()[0]])  

2. object.reshape

  1. mnist.test.images.reshape(-128281

所有reshape函数中,关键的是shape这个参数

下以tf.reshape为例介绍shape参数对结果的影响

  1. tf.reshape(tensor, shape, name=None
  1. # tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]  
  2. # tensor 't' has shape [9]  
  3. reshape(t, [33]) ==> [[123],  
  4.                         [456],  
  5.                         [789]]  
  6.   
  7.   
  8. # tensor 't' is [[[1, 1], [2, 2]],  
  9. #                [[3, 3], [4, 4]]]  
  10. # tensor 't' has shape [2, 2, 2]  
  11. reshape(t, [24]) ==> [[1122],  
  12.                         [3344]]  
  13.   
  14.   
  15. # tensor 't' is [[[1, 1, 1],  
  16. #                 [2, 2, 2]],  
  17. #                [[3, 3, 3],  
  18. #                 [4, 4, 4]],  
  19. #                [[5, 5, 5],  
  20. #                 [6, 6, 6]]]  
  21. # tensor 't' has shape [3, 2, 3]  
  22. # pass '[-1]' to flatten 't'  
  23. reshape(t, [-1]) ==> [111222333444555666]  
  24.   
  25.   
  26. # -1 can also be used to infer the shape  
  27.   
  28.   
  29. # -1 is inferred to be 9:  
  30. reshape(t, [2, -1]) ==> [[111222333],  
  31.                          [444555666]]  
  32. # -1 is inferred to be 2:  
  33. reshape(t, [-19]) ==> [[111222333],  
  34.                          [444555666]]  
  35. # -1 is inferred to be 3:  
  36. reshape(t, [ 2, -13]) ==> [[[111],  
  37.                               [222],  
  38.                               [333]],  
  39.                              [[444],  
  40.                               [555],  
  41.                               [666]]]  
  42.   
  43.   
  44. # tensor 't' is [7]  
  45. # shape `[]` reshapes to a scalar  
  46. reshape(t, []) ==> 7 

原创粉丝点击