title: Assign Custom Layer's Weight from 1D to 2D
body: <p>I am writing a custom layer in Tensorflow 2.0 and I ran to a problem as follow:</p>
  
  <p>I want to transform a 1D weight array (5x1) to a 2D array (10x10). Suppose I have the index to transform from 1D to 2D as follow, weight_index_lst:</p>
  
  <pre><code>weight_id, row, col
  1,5,6
  2,6,7
  3,7,8
  4,8,9
  5,9,10
  
  </code></pre>
  
  <p>The others location of the 2D array will just get a value of 0. Here's my script for the custom layers. My input is in (10x1) shape. For the w_mat, it receives 0 anywhere else that self.w is not assigned</p>
  
  <pre><code>import tensorflow as tf
  from tensorflow import keras
  from tensorflow.keras import layers
  
  class mylayer(layers.Layer):
      def __init__(self, weight_index_lst, **kwargs):
          super(mylayer, self).__init__(**kwargs)
          self.weight_index_lst= weight_index_lst
  
      def build(self):
          self.w = self.add_weight(shape = (5,1),
                                   initializer = 'he_normal',
                                   trainable = True)
  
      def call(self, inputs):          
          ct = 0
          w_mat = tf.Variable(np.zeros((21, 21)),dtype='float32',trainable=False)
          for i in range(20):
              i1 = self.weight_index_lst[i,1] #row index
              i2 = self.weight_index_lst[i,2] #column index
              w_mat[i1,i2].assign(self.w[ct,0]) #problem with no gradient provided
              #or w_mat[i1,i2] = self.w[ct,0] #resource variable cannot be assigned
              ct = ct+1
  
          y = tf.matmul(w_mat,inputs)           
          return y
  </code></pre>
  
  <p>I could have declared a (10x10) weight array but my deep learning wants the others weight to be 0 and cannot be trained.</p>