How to get weight in tf.layers.dense?

I want to draw the tf.layers.dense weights in the tensor histogram, but it does not appear in the parameter, how can I do this?

+16
source share
7 answers

Weights are added as a variable named kernelso you can use

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

Obviously, you can replace it tf.get_default_graph()with any other schedule in which you work.

+32
source

The last layers of the tenorflow api create all variables with a tf.get_variablecall tf.get_variable. This ensures that if you want to use the variable again, you can simply use the function tf.get_variableand provide the name of the variable you want to get.

tf.layers.dense : layer_name/kernel. , , :

with tf.variable_scope("layer_name", reuse=True):
    weights = tf.get_variable("kernel") # do not specify
    # the shape here or it will confuse tensorflow into creating a new one.

[]: Tensorflow - API. , API - . , → tf.layers.dense(...). , → tf.layers.Dense(...). , . obj.trainable_weights , .

+8

. tf.layers.dense . "dense_2/xxx", ​​ "dense_1/kernel: 0". , tf.get_variable , name=xxx tf.layers.dense, . :

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

, tf - 1.3.

+8

.

:

sess.run(x.kernel)

, .

.

, , , - tf, .

+4

-

model.get_weights()

After I created the model, compiled it and launched fit, this function returns an empty array of weights for me.

0
source
h =  tf.layers.Dense()
h.weights

working

0
source

In TF 2, if you are inside the @ tf.function function (graph mode):

weights = optimizer.weights

If you are in active mode (by default in TF2, with the exception of functions decorated with @ tf.function):

weights = optimizer.get_weights()

0
source

All Articles