我目前正在尝试重新创建在this paper here中作为神经网络项目的一部分进行的实验。
从this post中得到一些启发,我编写了此python函数,以便能够使用与本文中指定的快速梯度符号方法相同的广告训练过程。此函数通过Keras库利用Python togeather的“高阶函数”功能:
def adveserial_loss(loss_function, model, x ,eps = 0.01):
def loss_wrapper(y_true, y_pred):
loss = loss_function(y_true, y_pred)t
grad = K.gradients(loss, x)
xb = x + eps * K.sign(grad)
advloss = loss_function(y_true, model(xb))
return K.mean(loss+advloss,axis=-1)
return loss_wrapper
然后我将其用作我的损失(或能量)函数(例如,使用绝对交叉熵):
model.compile(loss=adveserial_loss(loss_function = categorical_crossentropy, model, model.inputs)
但是,运行这样的代码,会出现以下错误:
Traceback (most recent call last):
File "src/etap3sim.py", line 48, in <module>
model.compile(optimizer="adam", loss=ann.adveserial_loss(klosses.categorical_crossentropy,model,model.inputs), metrics=["accuracy"])
File "/home/xiaoming/.local/lib/python3.7/site-packages/keras/engine/training.py", line 342, in compile
sample_weight, mask)
File "/home/xiaoming/.local/lib/python3.7/site-packages/keras/engine/training_utils.py", line 404, in weighted
score_array = fn(y_true, y_pred)
File "/home/xiaoming/gitRepos/TIFX05-MScThesis-HenryYang/src/ANN/loss_functions.py", line 29, in loss_wrapper
advloss = loss_function(y_true, model(xb))
File "/home/xiaoming/.local/lib/python3.7/site-packages/keras/engine/base_layer.py", line 457, in __call__
output = self.call(inputs, **kwargs)
File "/home/xiaoming/.local/lib/python3.7/site-packages/keras/engine/network.py", line 564, in call
output_tensors, _, _ = self.run_internal_graph(inputs, masks)
File "/home/xiaoming/.local/lib/python3.7/site-packages/keras/engine/network.py", line 721, in run_internal_graph
layer.call(computed_tensor, **kwargs))
File "/home/xiaoming/.local/lib/python3.7/site-packages/keras/layers/convolutional.py", line 171, in call
dilation_rate=self.dilation_rate)
File "/home/xiaoming/.local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 3650, in conv2d
data_format=tf_data_format)
File "/home/xiaoming/.local/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py", line 893, in convolution
name=name)
File "/home/xiaoming/.local/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py", line 955, in convolution_internal
strides = _get_sequence(strides, n, channel_index, "strides")
File "/home/xiaoming/.local/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py", line 72, in _get_sequence
name, n, n + 2, current_n))
ValueError: strides should be of length 1, 3 or 5 but was 2
有人遇到相同的错误或类似的东西吗?关于如何解决此问题的任何好的建议?任何帮助将不胜感激!