优化变量w.r.t.张量流中的输入占位符

时间:2018-10-13 11:32:21

标签: tensorflow

我正在尝试实现Carlini & Wagner方法的症结,以在预先训练的模型上生成对抗图像。使用下面的代码片段,在初始化优化器时会遇到无法计算梯度的错误:

with tf.Session() as sess:
    #Load a pre-existing model:
    model = tf.saved_model.loader.load(export_dir=trainedModel, sess=sess, tags=[tag_constants.SERVING])

    #extract the relevant tensors:
    inputImage = tf.get_default_graph().get_tensor_by_name('inputImage_placeholder')
    inputLabel = tf.get_default_graph().get_tensor_by_name('inputLabel_placeholder')
    isTraining = tf.get_default_graph().get_tensor_by_name('isTraining_placeholder')

    logits = tf.get_default_graph().get_tensor_by_name('logits_output/MatMul:0')

    # test predictions:
    logitsScore = sess.run(logits, feed_dict={inputImage:sampleImages, isTraining:False})
    print("predicted classes:", np.argmax(logitsScore, axis=1))

    ### Works fine.

    #create variables for CW method to perturb the input images:
    perturbImageVariables = tf.Variable(initial_value=np.random.normal(size=(_NUM_PERTURB_IMAGEs, _HEIGHT, _WIDTH, _CHANNELS)), dtype=tf.float32)
    perturbImage = tf.sigmoid(x=perturbImageVariables) #to ensure the image is between 0 & 1

    # Choose the target class for a given image
    targetClass = 0 # for illustration purposes.

    # convert targetClass to one_hot
    targetClass_oneHot = tf.one_hot(targetClass, _NUM_CLASSES)
    targetClass_oneHot = tf.expand_dims(targetClass_oneHot, 0) #incorporate batch

    loss = tf.losses.softmax_cross_entropy(onehot_labels=targetClass_oneHot , logits=logits)

    optimizer = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE)
    trainStep = optimizer.minimize(loss, var_list=[perturbImageVariables]) ######## Getting the error here #################

    # we initialise new variables:
    initialise_new_variables()

    for optStep in range(20):
      img = sess.run(perturbImage)

      _, lossValue = sess.run([trainStep, loss], feed_dict={inputImage: img, isTraining:False } )

    # we expect to get the perturbed image here.        

但是,在“ trainStep”处,出现了

错误

ValueError:没有为任何变量提供渐变,请检查图形中不支持渐变的操作。

如果我在张量板上查看该图,则在perturbImageVariables和logits之间确实没有连接。但是,我通过feed_dict提供了链接。

我们也不能通过tf.assign()之类的方法来初始化占位符,也无法找到没有占位符的渐变。

关于如何解决错误的任何线索?

0 个答案:

没有答案