import tensorflow as tf
import numpy as np
tf.reset_default_graph()
tf.set_random_seed(101)
a = tf.ones((1,5))
x = tf.layers.dense(a,5)
tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,scope=x.name.split('/')[0])
Getting W
dense_W = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,scope= x.name.split('/')[0] + '/kernel:0')[0]
dense_W
Getting the variable as tensor
Equivalent to
dense_W.values()
tf.get_default_graph().get_tensor_by_name(x.name.split('/')[0] + '/kernel:0')
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
w = sess.run(dense_W)
w
Make sure that the shape of the new tensor is compatable
new_val = dense_W+1.0
op = dense_W.assign(new_val)
op #this has to be run to carry out the assign op.
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
w
kernel_init = tf.initializers.constant(np.ones((5,5))) #Cannot pass tensors to initializer
y = tf.layers.dense(a,5,kernel_initializer=kernel_init)
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])
w