我试图获得相对于Keras输入的损耗梯度。我正在执行以下操作:
import tensorflow as tf
from tensorflow.python.keras.models import Model,Sequential
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.losses import mean_squared_error
# the Keras autoencoder model is defined below
model = Sequential()
model.add(Conv1D(filters=64,kernel_size=10,strides=1,activation='relu',input_shape=(data_shape[1],data_shape[2]),padding="same"))
model.add(Conv1D(32,10,padding="same"))
model.add(MaxPooling1D(4))
model.add(Conv1D(32,padding="same"))
model.add(UpSampling1D(4))
model.add(Conv1D(data_shape[2],padding="same"))
model.add(Conv1D(data_shape[2],activation=None,padding="same"))
# get gradient of the loss w.r.t. the input
loss = mean_squared_error(model.input,model.output)
grad = K.gradients(loss,model.input)[0]
func = K.function(model.input,[loss,grad])
session = K.get_session()
session.run(tf.global_variables_initializer())
loss_gradient = session.run(func,feed_dict={model.input: x})
我收到以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\Anaconda3\envs\tf_gpu_pytorch_gpu\lib\site-packages\tensorflow\python\client\session.py in __init__(self,fetches,contraction_fn)
299 self._unique_fetches.append(ops.get_default_graph().as_graph_element(
--> 300 fetch,allow_tensor=True,allow_operation=True))
301 except TypeError as e:
~\Anaconda3\envs\tf_gpu_pytorch_gpu\lib\site-packages\tensorflow\python\framework\ops.py in as_graph_element(self,obj,allow_tensor,allow_operation)
3477 with self._lock:
-> 3478 return self._as_graph_element_locked(obj,allow_operation)
3479
~\Anaconda3\envs\tf_gpu_pytorch_gpu\lib\site-packages\tensorflow\python\framework\ops.py in _as_graph_element_locked(self,allow_operation)
3566 raise TypeError("Can not convert a %s into a %s." % (type(obj).__name__,-> 3567 types_str))
3568
TypeError: Can not convert a GraphExecutionFunction into a Tensor or Operation.
During handling of the above exception,another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-28-28014aeac718> in <module>
----> 1 a = session.run(func,feed_dict={model.input: x})
~\Anaconda3\envs\tf_gpu_pytorch_gpu\lib\site-packages\tensorflow\python\client\session.py in run(self,feed_dict,options,run_metadata)
927 try:
928 result = self._run(None,options_ptr,--> 929 run_metadata_ptr)
930 if run_metadata:
931 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~\Anaconda3\envs\tf_gpu_pytorch_gpu\lib\site-packages\tensorflow\python\client\session.py in _run(self,handle,run_metadata)
1135 # Create a fetch handler to take care of the structure of fetches.
1136 fetch_handler = _FetchHandler(
-> 1137 self._graph,feed_dict_tensor,feed_handles=feed_handles)
1138
1139 # Run request and get response.
~\Anaconda3\envs\tf_gpu_pytorch_gpu\lib\site-packages\tensorflow\python\client\session.py in __init__(self,graph,feeds,feed_handles)
469 """
470 with graph.as_default():
--> 471 self._fetch_mapper = _FetchMapper.for_fetch(fetches)
472 self._fetches = []
473 self._targets = []
~\Anaconda3\envs\tf_gpu_pytorch_gpu\lib\site-packages\tensorflow\python\client\session.py in for_fetch(fetch)
269 if isinstance(fetch,tensor_type):
270 fetches,contraction_fn = fetch_fn(fetch)
--> 271 return _ElementFetchMapper(fetches,contraction_fn)
272 # Did not find anything.
273 raise TypeError('Fetch argument %r has invalid type %r' % (fetch,~\Anaconda3\envs\tf_gpu_pytorch_gpu\lib\site-packages\tensorflow\python\client\session.py in __init__(self,contraction_fn)
302 raise TypeError('Fetch argument %r has invalid type %r,'
303 'must be a string or Tensor. (%s)' %
--> 304 (fetch,type(fetch),str(e)))
305 except ValueError as e:
306 raise ValueError('Fetch argument %r cannot be interpreted as a '
TypeError: Fetch argument <tensorflow.python.keras.backend.GraphExecutionFunction object at 0x000002641A998240> has invalid type <class 'tensorflow.python.keras.backend.GraphExecutionFunction'>,must be a string or Tensor. (Can not convert a GraphExecutionFunction into a Tensor or Operation.)
我已经搜索了一些计算所需梯度的解决方案:K.gradients(loss,input_img)[0] return "None". (Keras CNN visualization with tensorflow backend)
How to compute loss gradient w.r.t to model inputs in a Keras model?
此人谈论使用我正在使用的K.get_session():keras/tensorflow model: gradient w.r.t. input return the same (wrong?) value for all input data
我想计算损失w.r.t.输入,然后我将使用它来更新输入样本x。我只想使用Keras,而避免使用Keras模型包装器在Tensorflow中进行梯度计算,这在此博客中建议:https://github.com/tensorflow/cleverhans/blob/master/cleverhans_tutorials/mnist_tutorial_keras_tf.py
我找不到解决方案或发布有关此错误的信息。请帮忙,非常感谢!