Tensor Transpose
This is an example of taking the transpose of a tensor.
from csdl_om import Simulatorfrom csdl import Modelimport csdlimport numpy as np
class ExampleTensor(Model):
def define(self):
# Declare tens as an input tensor with shape = (4, 3, 2, 5) tens = self.declare_variable( 'Tens', val=np.arange(4 * 3 * 5 * 2).reshape((4, 3, 5, 2)), )
# Compute the transpose of tens self.register_output('tensor_transpose', csdl.transpose(tens))
sim = Simulator(ExampleTensor())sim.run()
print('Tens', sim['Tens'].shape)print(sim['Tens'])print('tensor_transpose', sim['tensor_transpose'].shape)print(sim['tensor_transpose'])
[[[[ 0. 1.] [ 2. 3.] [ 4. 5.] [ 6. 7.] [ 8. 9.]]
[[ 10. 11.] [ 12. 13.] [ 14. 15.] [ 16. 17.] [ 18. 19.]]
[[ 20. 21.] [ 22. 23.] [ 24. 25.] [ 26. 27.] [ 28. 29.]]]
[[[ 30. 31.] [ 32. 33.] [ 34. 35.] [ 36. 37.] [ 38. 39.]]
[[ 40. 41.] [ 42. 43.] [ 44. 45.] [ 46. 47.] [ 48. 49.]]
[[ 50. 51.] [ 52. 53.] [ 54. 55.] [ 56. 57.] [ 58. 59.]]]
[[[ 60. 61.] [ 62. 63.] [ 64. 65.] [ 66. 67.] [ 68. 69.]]
[[ 70. 71.] [ 72. 73.] [ 74. 75.] [ 76. 77.] [ 78. 79.]]
[[ 80. 81.] [ 82. 83.] [ 84. 85.] [ 86. 87.] [ 88. 89.]]]
[[[ 90. 91.] [ 92. 93.] [ 94. 95.] [ 96. 97.] [ 98. 99.]]
[[100. 101.] [102. 103.] [104. 105.] [106. 107.] [108. 109.]]
[[110. 111.] [112. 113.] [114. 115.] [116. 117.] [118. 119.]]]]tensor_transpose (2, 5, 3, 4)[[[[ 0. 30. 60. 90.] [ 10. 40. 70. 100.] [ 20. 50. 80. 110.]]
[[ 2. 32. 62. 92.] [ 12. 42. 72. 102.] [ 22. 52. 82. 112.]]
[[ 4. 34. 64. 94.] [ 14. 44. 74. 104.] [ 24. 54. 84. 114.]]
[[ 6. 36. 66. 96.] [ 16. 46. 76. 106.] [ 26. 56. 86. 116.]]
[[ 8. 38. 68. 98.] [ 18. 48. 78. 108.] [ 28. 58. 88. 118.]]]
[[[ 1. 31. 61. 91.] [ 11. 41. 71. 101.] [ 21. 51. 81. 111.]]
[[ 3. 33. 63. 93.] [ 13. 43. 73. 103.] [ 23. 53. 83. 113.]]
[[ 5. 35. 65. 95.] [ 15. 45. 75. 105.] [ 25. 55. 85. 115.]]
[[ 7. 37. 67. 97.] [ 17. 47. 77. 107.] [ 27. 57. 87. 117.]]
[[ 9. 39. 69. 99.] [ 19. 49. 79. 109.] [ 29. 59. 89. 119.]]]]