Getting,modifying,custom initializing variables

In [39]:
import tensorflow as tf
import numpy as np
In [127]:
tf.reset_default_graph()
tf.set_random_seed(101)
In [129]:
a = tf.ones((1,5))
x = tf.layers.dense(a,5)

Getting the weights belonging to a layer

In [131]:
tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,scope=x.name.split('/')[0])
Out[131]:
[<tf.Variable 'dense/kernel:0' shape=(5, 5) dtype=float32_ref>,
 <tf.Variable 'dense/bias:0' shape=(5,) dtype=float32_ref>]

Getting W

In [132]:
dense_W = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,scope= x.name.split('/')[0] + '/kernel:0')[0]
dense_W
Out[132]:
<tf.Variable 'dense/kernel:0' shape=(5, 5) dtype=float32_ref>

Getting the variable as tensor

Equivalent to

dense_W.values()
In [133]:
tf.get_default_graph().get_tensor_by_name(x.name.split('/')[0] + '/kernel:0')
Out[133]:
<tf.Tensor 'dense/kernel:0' shape=(5, 5) dtype=float32_ref>
In [134]:
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    w = sess.run(dense_W)
In [135]:
w
Out[135]:
array([[-0.4783678 , -0.47883928, -0.67987746,  0.12508851,  0.21986353],
       [-0.6352771 ,  0.08145899,  0.17867547,  0.15549004,  0.03497177],
       [ 0.7717041 , -0.6796187 , -0.6153394 , -0.01366824,  0.22456986],
       [ 0.46102703,  0.38562346,  0.1560592 , -0.7276155 , -0.6331084 ],
       [ 0.3180946 , -0.3253305 , -0.24750745,  0.11823857,  0.6152173 ]],
      dtype=float32)

Modifying the weights

Make sure that the shape of the new tensor is compatable

In [145]:
new_val = dense_W+1.0
op = dense_W.assign(new_val)
op #this has to be run to carry out the assign op. 
Out[145]:
<tf.Tensor 'Assign_2:0' shape=(5, 5) dtype=float32_ref>
In [146]:
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(op) #this has to be run to carry out the assign op. 
    w = sess.run(dense_W) #This will change the value of the variable
In [147]:
w
Out[147]:
array([[0.5216322 , 0.5211607 , 0.32012254, 1.1250885 , 1.2198635 ],
       [0.3647229 , 1.081459  , 1.1786754 , 1.15549   , 1.0349717 ],
       [1.7717041 , 0.32038128, 0.3846606 , 0.98633176, 1.2245698 ],
       [1.461027  , 1.3856235 , 1.1560593 , 0.27238452, 0.36689162],
       [1.3180946 , 0.6746695 , 0.75249255, 1.1182386 , 1.6152173 ]],
      dtype=float32)

Initializing custom layer weights

In [153]:
kernel_init = tf.initializers.constant(np.ones((5,5))) #Cannot pass tensors to initializer
y = tf.layers.dense(a,5,kernel_initializer=kernel_init)
In [154]:
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    w = sess.run(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,scope= y.name.split('/')[0] + '/kernel:0')[0])
In [155]:
w
Out[155]:
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]], dtype=float32)