Update a subset of weights in TensorFlow

Does anyone know how to update a subset (i.e. just some indices) of weights that are used in direct distribution?

My assumption is that I could do this after applying compute_gradients as follows:

optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
grads_vars = optimizer.compute_gradients(loss, var_list=[weights, bias_h, bias_v])

... and then do something with a list of tuples in grads_vars.

+4
source share
2 answers

You can use a combination of gatherand scatter_update. Here is an example that doubles the values ​​in position 0and2

indices = tf.constant([0,2])
data = tf.Variable([1,2,3])
data_subset = tf.gather(data, indices)
updated_data_subset = 2*data_subset
sparse_update = tf.scatter_update(data, indices, updated_data_subset)
init_op = tf.initialize_all_variables()

sess = tf.Session()
sess.run([init_op])
print "Values before:", sess.run([data])
sess.run([sparse_update])
print "Values after:", sess.run([data])

You should see

Values before: [array([1, 2, 3], dtype=int32)]
Values after: [array([2, 2, 6], dtype=int32)]
+9
source

- tf.Variable python ( numpy) npvar = sess.run(tfvar), , npvar[1, 2] = -10. sess.run(tfvar.assign(npvar)).

, , .

+1

Source: https://habr.com/ru/post/1625415/


All Articles