从RNN小区获得权重和偏差的值

时间:2016-10-30 00:33:22

标签: tensorflow

我在张量流中有以下vanilla RNN实现。如何从basicRNNCell获得权重和偏差的值?

import tensorflow as tf
import numpy as np

input_size = 5
batch_size = 2
max_length = 1

cell = tf.nn.rnn_cell.BasicRNNCell(num_units = 4)

# Batch size x time steps x features.
data = tf.placeholder(tf.float32, [None, max_length, input_size])
output, _ = tf.nn.dynamic_rnn(cell, data, dtype=tf.float32)

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    result = sess.run([output], feed_dict={data: np.ones((batch_size, max_length, input_size))})

    print result
    print result[0].shape

    for v in tf.trainable_variables():
        print v.name
        print dir(v)

1 个答案:

答案 0 :(得分:14)

您可以按名称从会话中提取任何张量的值:

variables_names =[v.name for v in tf.trainable_variables()]
values = sess.run(variables_names)
for k,v in zip(variables_names, values):
    print(k, v)

返回:

RNN/BasicRNNCell/Linear/Matrix:0
RNN/BasicRNNCell/Linear/Bias:0
(u'RNN/BasicRNNCell/Linear/Matrix:0', array([[ 0.0612123 , -0.3020778 ,  0.39463997,  0.09347564],
       [ 0.45926428,  0.23726827, -0.4563897 , -0.23666686],
       [-0.45560977, -0.13659951, -0.51252407,  0.54929543],
       [-0.54475051, -0.20766461,  0.01690435, -0.11470184],
       [ 0.31095517, -0.5281173 ,  0.50487423, -0.12220767],
       [ 0.09355438,  0.14729732, -0.31751576,  0.39974809],
       [ 0.30579591, -0.46520707, -0.48943958,  0.22013563],
       [-0.08513373,  0.30004191,  0.06920779,  0.38332987],
       [-0.36613646, -0.26537177, -0.18271935,  0.4455297 ]], dtype=float32))
(u'RNN/BasicRNNCell/Linear/Bias:0', array([ 0.,  0.,  0.,  0.], dtype=float32))