如何使用python在Tensorboard上显示模型的权重和偏差 [英] How to display weights and bias of the model on Tensorboard using python

查看:76
本文介绍了如何使用python在Tensorboard上显示模型的权重和偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created the following model for training and want to get it visualized on 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))

我想知道如何才能看到权重和偏差以及我为训练提供的输入之间的相关性.

I want to know how I can get to see the weights and bias and the correlation between the inputs that I am giving for training.

请帮助我.如果我的问题没有答案,请告诉我是否有任何建议.请询问我是否需要任何东西,我会得到它并告诉您.

Kindly, help me. Let me know if there is any suggestion if there is no answer to what I ask. Please ask me if there is anything required I will get it and let you know.

推荐答案

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

I think the easiest way to visualize weights on Tensorboard is to plot them as histograms. For instance, you could log your layers as follows.

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

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

Once you have created a summary for each layer or variable that you want to log, you have to collect them all with the merge_all function and create a FileWriter.

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

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

Finally, you have to run the summaries with the other ops and add the results to your writer.

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

如果您想对权重进行进一步分析,我建议将它们恢复为 numpy 数组,如此处所述.

If you want to any further analysis with your weights, I would recommend to recover them as numpy arrays, as explained here.

不过,我不知道在 Tensorboard 上绘制相关性的任何简单方法.如果您只想获得输入的相关性,我建议使用 scikit 甚至 pandas (.corr 函数),如果您的数据集不是很大.

I do not know any easy way to plot correlations on Tensorboard though. If you just want to get the correlation for your inputs, I would suggest using scikit or even pandas (.corr function) if your data set is not huge.

希望有帮助.您还可以参考此教程以获得更深入的解释.

I hope that helps. You can also refer to this tutorial for a more in depth explanation.

这篇关于如何使用python在Tensorboard上显示模型的权重和偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆