如何使用python在Tensorboard上显示模型的权重和偏差

时间:2018-08-31 09:48:00

标签: python python-3.x tensorflow tensorboard

我创建了以下模型进行训练,并希望在Tensorboard上将其可视化:

## Basic Cell LSTM tensorflow

index_in_epoch = 0;
perm_array  = np.arange(x_train.shape[0])
np.random.shuffle(perm_array)

# function to get the next batch
def get_next_batch(batch_size):
    global index_in_epoch, x_train, perm_array   
    start = index_in_epoch
    index_in_epoch += batch_size

    if index_in_epoch > x_train.shape[0]:
        np.random.shuffle(perm_array) # shuffle permutation array
        start = 0 # start next epoch
        index_in_epoch = batch_size

    end = index_in_epoch
    return x_train[perm_array[start:end]], y_train[perm_array[start:end]]

# parameters
n_steps = seq_len-1 
n_inputs = 4 
n_neurons = 200 
n_outputs = 4
n_layers = 2
learning_rate = 0.001
batch_size = 50
n_epochs = 100 
train_set_size = x_train.shape[0]
test_set_size = x_test.shape[0]

tf.reset_default_graph()

X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.float32, [None, n_outputs])

# use LSTM Cell with peephole connections
layers = [tf.contrib.rnn.LSTMCell(num_units=n_neurons, 
                                  activation=tf.nn.leaky_relu, use_peepholes = True)
          for layer in range(n_layers)]

multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers)
rnn_outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32)

stacked_rnn_outputs = tf.reshape(rnn_outputs, [-1, n_neurons]) 
stacked_outputs = tf.layers.dense(stacked_rnn_outputs, n_outputs)
outputs = tf.reshape(stacked_outputs, [-1, n_steps, n_outputs])
outputs = outputs[:,n_steps-1,:] # keep only last output of sequence

loss = tf.reduce_mean(tf.square(outputs - y)) # loss function = mean squared error 
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) 
training_op = optimizer.minimize(loss)

# run graph
with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer())
    for iteration in range(int(n_epochs*train_set_size/batch_size)):
        x_batch, y_batch = get_next_batch(batch_size) # fetch the next training batch 
        sess.run(training_op, feed_dict={X: x_batch, y: y_batch}) 
        if iteration % int(5*train_set_size/batch_size) == 0:
            mse_train = loss.eval(feed_dict={X: x_train, y: y_train}) 
            mse_valid = loss.eval(feed_dict={X: x_valid, y: y_valid}) 
            print('%.2f epochs: MSE train/valid = %.6f/%.6f'%(
                iteration*batch_size/train_set_size, mse_train, mse_valid))

我想知道如何了解权重和偏差以及我为训练提供的输入之间的相关性。

请帮助我。让我知道是否有任何建议,如果我的要求没有答案。请问我是否有任何要求,我会告诉您。

1 个答案:

答案 0 :(得分:4)

我认为在Tensorboard上可视化权重的最简单方法是将其绘制为直方图。例如,您可以按如下所示记录图层。

for i, layer in enumerate(layers):
    tf.summary.histogram('layer{0}'.format(i), layer)

一旦为要记录的每个层或变量创建了摘要,则必须使用merge_all函数收集它们并创建FileWriter。

merged = tf.summary.merge_all()
writer = tf.summary.FileWriter('directory_name', sess.graph)

最后,您必须与其他操作一起运行摘要,然后将结果添加到编写器中。

summary, _ = sess.run([merged, training_op], feed_dict={X: x_batch, y: y_batch})
writer.add_summary(summary, iteration_number)

如果您想对权重进行任何进一步的分析,我建议将其恢复为numpy数组,如here所述。

我不知道在Tensorboard上绘制关联的任何简单方法。如果您只是想获取输入的相关性,建议您使用scikit或熊猫(.corr function),如果您的数据集不大。

我希望能有所帮助。您也可以参考此tutorial以获得更深入的说明。