我正在尝试对TF模型中的输入变量应用梯度下降,以使模型输出任意值。我首先使用实际数据训练模型,然后生成随机数组以从模型获得预测。然后,我为此输入计算梯度,最后尝试仅对此应用优化算法,而模型本身保持不变。这是我陷入困境的最后一步。
我是Tensorflow的新手,在网上找不到该应用程序的其他示例。
这里有一些代码可以重现我的方法和我得到的错误(当第37行未注释时)。
import tensorflow as tf
import numpy as np
def some_op(x):
return 0.8 * x
epochs = 5
batch_size = 8
x_shape1 = 6
y_shape1 = 1
is_training = tf.placeholder(tf.bool, name='is_training')
x = tf.placeholder(tf.float32, [None, x_shape1], name='X')
x_var = tf.Variable(0.)
y = tf.placeholder(tf.float32, [None, y_shape1], name='Y')
with tf.name_scope('ops'):
# trainable input
input_var = tf.assign(x_var, x, validate_shape=False, name='input_tensor')
# NN
X = tf.layers.dense(input_var, 8, name='dense1', reuse=tf.AUTO_REUSE)
X = tf.layers.dropout(X, 0.5)
X = tf.layers.dense(X, 16, name='dense2', reuse=tf.AUTO_REUSE)
X = tf.layers.dropout(X, 0.5)
X = tf.layers.dense(X, 4, name='dense3', reuse=tf.AUTO_REUSE)
output = tf.layers.dense(X, y_shape1, name='output', reuse=tf.AUTO_REUSE)
with tf.name_scope('loss'):
loss = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(y, output))))
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
optimizer = tf.train.AdamOptimizer(0.01)
do_optimization = optimizer.minimize(loss)
grads_and_vars = optimizer.compute_gradients(loss, var_list=[input_var])
my_grads_and_vars = [(some_op(gv[0]), gv[1]) for gv in grads_and_vars]
# apply_grad = optimizer.apply_gradients(my_grads_and_vars) # uncomment here
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# train model
for e in range(epochs):
random_input = np.random.normal(size=[batch_size, x_shape1]) * np.arange(1, 7)
random_y = np.random.uniform(size=[batch_size, y_shape1]) * 8
loss_, _ = sess.run([loss, do_optimization], feed_dict={x: random_input, y: random_y, is_training: True})
print("Epoch", e, "loss", loss_)
# generate desired input
gen_random_input = np.random.uniform(size=[1, x_shape1])
desired_y = [[8]]
input_grad = sess.run(grads_and_vars, feed_dict={x: gen_random_input, y: desired_y, is_training: False})
取消第37行注释时,您将获得以下信息:
ValueError: Input 'ref' passed float expected ref type while building NodeDef 'ops/input_tensor/Adam/ops/input_tensor_ops/input_tensor/Adam_0' using Op<name=Assign; signature=ref:Ref(T), value:T -> output_ref:Ref(T); attr=T:type; attr=validate_shape:bool,default=true; attr=use_locking:bool,default=true; allows_uninitialized_input=true>
任何人都必须面对这个问题吗?