Tensorflow仅针对变量的某些元素进行最小化

Jef*_*eff 10 python machine-learning tensorflow

是否可以通过仅更改变量的某些元素来最小化损失函数?换句话说,如果我有一个X长度为2 的变量,我如何通过改变X[0]和保持X[1]常数来最小化我的损失函数?

希望我试过的这段代码能描述我的问题:

import tensorflow as tf
import tensorflow.contrib.opt as opt

X = tf.Variable([1.0, 2.0])
X0 = tf.Variable([3.0])

Y = tf.constant([2.0, -3.0])

scatter = tf.scatter_update(X, [0], X0)

with tf.control_dependencies([scatter]):
    loss = tf.reduce_sum(tf.squared_difference(X, Y))

opt = opt.ScipyOptimizerInterface(loss, [X0])

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    opt.minimize(sess)

    print("X: {}".format(X.eval()))
    print("X0: {}".format(X0.eval()))
Run Code Online (Sandbox Code Playgroud)

哪个输出:

INFO:tensorflow:Optimization terminated with:
  Message: b'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL'
  Objective function value: 26.000000
  Number of iterations: 0
  Number of functions evaluations: 1
X: [3. 2.]
X0: [3.]
Run Code Online (Sandbox Code Playgroud)

在哪里我想找到最佳值,X0 = 2从而X = [2, 2]

编辑

这样做的动机:我想导入训练有素的图形/模型,然后根据我的一些新数据调整一些变量的各种元素.

Blu*_*Sun 5

您可以使用此技巧将渐变计算限制为一个索引:

import tensorflow as tf
import tensorflow.contrib.opt as opt

X = tf.Variable([1.0, 2.0])

part_X = tf.scatter_nd([[0]], [X[0]], [2])

X_2 = part_X + tf.stop_gradient(-part_X + X)

Y = tf.constant([2.0, -3.0])

loss = tf.reduce_sum(tf.squared_difference(X_2, Y))

opt = opt.ScipyOptimizerInterface(loss, [X])

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    opt.minimize(sess)

    print("X: {}".format(X.eval()))
Run Code Online (Sandbox Code Playgroud)

part_X变为你想要在与X相同形状的单热矢量中改变的值.part_X + tf.stop_gradient(-part_X + X)与正向传递中的X相同,因为part_X - part_X是0.但是在向后传递中,tf.stop_gradient防止所有不必要的梯度计算.