gpt4 book ai didi

python - 更新 TensorFlow 中的权重子集

转载 作者:太空狗 更新时间:2023-10-30 00:39:26 24 4
gpt4 key购买 nike

有谁知道如何更新前向传播中使用的权重的子集(即只有一些索引)?

我的猜测是,在按如下方式应用 compute_gradients 之后,我也许能够做到这一点:

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

...然后对 grads_vars 中的元组列表做一些事情。

最佳答案

您可以结合使用 gatherscatter_update。这是一个将位置 02

的值加倍的示例
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])

你应该看到

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

关于python - 更新 TensorFlow 中的权重子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34935464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com