简体   繁体   中英

How do I capture weight updates while using tf.train.optimizer

How do I capture weight updates after a train step while using tf.train.optimizer?

One can get gradients, but I could not find any effective way other than finding the difference of weights from their previous copy. It becomes tedious as I have stored weights as a part of a dictionary, key being hidden layer, values being list of weights.

weights = {
'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1], 0, 0.1)),
'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], 0, 0.1)),
'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes], 0, 0.1))
}
biases = {
'b1': tf.Variable(tf.random_normal([n_hidden_1], 0, 0.1)),
'b2': tf.Variable(tf.random_normal([n_hidden_2], 0, 0.1)),
'out': tf.Variable(tf.random_normal([n_classes], 0, 0.1))
}
print(sess.run(weights['h1']))

在每个时期的循环结束时。

I think the easiest way is to capture the snapshot of all trainable variables before and after a training step and get the difference:

with tf.Session() as session:
  ...

  def get_snapshot():
    return {var: value for var, value in zip(tf.trainable_variables(), session.run(tf.trainable_variables()))}

  ...

  snapshot_before = get_snapshot()
  session.run([optimizer], feed_dict={x: train_x, y: train_y})
  snapshot_after = get_snapshot()
  for var in tf.trainable_variables():
    print var.name, ' -> ', (snapshot_after[var] - snapshot_before[var])

This can look even nicer if implemented via a context manager . In terms of efficiency, this requires two extra session.run() invocations, however it's much better than evaluating each variable one by one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM