Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get weights in tf.layers.dense?

Tags:

tensorflow

I wanna draw the weights of tf.layers.dense in tensorboard histogram, but it not show in the parameter, how could I do that?

like image 653
user8381550 Avatar asked Jul 28 '17 11:07

user8381550


People also ask

How do you find the weight of a dense layer TensorFlow?

For obtaining the weights, just use obj. trainable_weights this returns a list of all the trainable variables found in that layer's scope.

What is TF layer dense?

dense() is an inbuilt function of Tensorflow. js library. This function is used to create fully connected layers, in which every output depends on every input. Syntax: tf.layers.dense(args)


2 Answers

The weights are added as a variable named kernel, so you could use

x = tf.dense(...)
weights = tf.get_default_graph().get_tensor_by_name(
  os.path.split(x.name)[0] + '/kernel:0')

You can obviously replace tf.get_default_graph() by any other graph you are working in.  

like image 78
P-Gn Avatar answered Sep 21 '22 06:09

P-Gn


I came across this problem and just solved it. tf.layers.dense 's name is not necessary to be the same with the kernel's name's prefix. My tensor is "dense_2/xxx" but it's kernel is "dense_1/kernel:0". To ensure that tf.get_variable works, you'd better set the name=xxx in the tf.layers.dense function to make two names owning same prefix. It works as the demo below:

l=tf.layers.dense(input_tf_xxx,300,name='ip1')
with tf.variable_scope('ip1', reuse=True):
    w = tf.get_variable('kernel')

By the way, my tf version is 1.3.

like image 27
Yerrick Avatar answered Sep 19 '22 06:09

Yerrick