0
votes

The tensor I need to initialize is of shape (3,3,2). So there are 3 matrices of shape (3,2) that I need to initialize the following way:

1st matrix: [[x1, x2],[0,0],[0,0]]

2nd matrix: [[0,0], [x1, x2],[0,0]]

3rd matrix: [[0,0], [0,0], [x1, x2]]

I am given x(numpy array) as an input. I am having trouble figuring out the loop to initialize a tensor using numpy.

   y= np.zeros((3, 3, 2))
   for i in range(3):
      for j in range(3):
         for k in range(2):
             y[i,j,k] = ?

I think given x, I'm going about this the wrong way. Any help is appreciated!

1

1 Answers

0
votes
In [8]: arr = np.zeros((3,3,2))                                                                                      
In [9]: arr[np.arange(3), np.arange(3),:] = [1.2, 2.3]                                                               
In [10]: arr                                                                                                         
Out[10]: 
array([[[1.2, 2.3],
        [0. , 0. ],
        [0. , 0. ]],

       [[0. , 0. ],
        [1.2, 2.3],
        [0. , 0. ]],

       [[0. , 0. ],
        [0. , 0. ],
        [1.2, 2.3]]])