使用Keras和TensorFlow查找使神经网络的输出最大化的输入 [英] Find input that maximises output of a neural network using Keras and TensorFlow

查看:133
本文介绍了使用Keras和TensorFlow查找使神经网络的输出最大化的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Keras和TensorFlow对时尚MNIST 进行了分类. ://www.tensorflow.org/tutorials/keras/basic_classification"rel =" noreferrer>本教程.

I have used Keras and TensorFlow to classify the Fashion MNIST following this tutorial .

它使用 AdamOptimizer 来查找用于最小化网络的丢失功能.网络的输入是形状为[28,28]的2D张量,输出是形状为[10]的1D张量,这是softmax函数的结果.

It uses the AdamOptimizer to find the value for model parameters that minimize the loss function of the network. The input for the network is a 2-D tensor with shape [28, 28], and output is a 1-D tensor with shape [10] which is the result of a softmax function.

一旦对网络进行了训练,我想将优化器用于另一项任务:找到使输出张量的元素之一最大化的输入.如何才能做到这一点?可以使用Keras这样做还是必须使用较低级别的API?

Once the network has been trained, I want to use the optimizer for another task: find an input that maximizes one of the elements of the output tensor. How can this be done? Is it possible to do so using Keras or one have to use a lower level API?

由于输入对于给定的输出不是唯一的,因此最好对输入可以采用的值施加一些约束.

Since the input is not unique for a given output, it would be even better if we could impose some constraints on the values the input can take.

训练后的模型具有以下格式

The trained model has the following format

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

推荐答案

我觉得您想对输入进行反向传播,从而冻结了模型的所有权重.您可以做的是:

I feel you would want to backprop with respect to the input freezing all the weights to your model. What you could do is:

  1. 在输入层后添加一个与输入尺寸相同的密集层并将其设置为可训练
  2. 冻结模型的所有其他层. (除了您添加的那个)
  3. 作为输入,输入一个单位矩阵并根据所需的输出来训练模型.

文章和如果要基于输入进行反向传播,这篇文章可能会为您提供帮助反而.这有点像您的目标,但是您可以得到直觉.

This article and this post might be able to help you if you want to backprop based on the input instead. It's a bit like what you are aiming for but you can get the intuition.

这篇关于使用Keras和TensorFlow查找使神经网络的输出最大化的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆