用Keras实现对抗训练+lstm的一个报错
本帖最后由 猪仔很忙 于 2020-8-7 18:12 编辑我正在尝试建立结合对抗训练的LSTM模型,代码如下:
def gradient_operation(args):
y_true = args
y_pred = args
v_final = args
pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
loss = -K.mean(0.75 * K.pow(1. - pt_1, 0) * K.log(pt_1)) - K.mean((1 - 0.75) * K.pow(pt_0, 0) * K.log(1. - pt_0))
perturb = K.gradients(loss, v_final)
adv_v_final = K.gradients(loss, v_final) + v_final
return adv_v_final
def build_train(datas, machineID, modelName):
train_data, train_labels, val_data, val_labels, test_data, test_labels = datas
num_samples = train_data.shape
time_step = train_data.shape
feature_dim = train_data.shape
trace_input = Input(shape=(time_step, feature_dim))
label_input = Input(shape=(1,))
if modelName == 'FEMT_LSTM':
v_final = RNN_SEPARATE_2(time_step, feature_dim)(trace_input)
else:
v_final = RNN(time_step, feature_dim)(trace_input)
pred = Dense(1, activation='sigmoid', name='pred')(v_final)
v_final_adv = Lambda(gradient_operation)()
adv_pred = Dense(1, activation='sigmoid', name='adv_pred')(v_final_adv)
model = Model(inputs=, outputs=)
model.compile(optimizer=optimizers.Adam(lr=0.001, clipvalue=15),
loss={'pred': myloss(alpha=0.75, gamma=0), 'adv_pred': myloss(alpha=0.75, gamma=0)},
loss_weights={'pred': 1., 'adv_pred': 0.1})
print('Train...')
model_save_path = './lib/model_cp_rnn_{}'.format(machineID)
call_backs = , # test_data, test_labels
model_save_path=model_save_path),
ReduceLROnPlateau(monitor='val_loss', factor=0.8, patience=4, mode='min'), # 4
ModelCheckpoint(filepath=model_save_path, monitor='val_loss', save_best_only=True, save_weights_only=True, mode='min')]
model.fit({'trace_input': train_data, 'label_input': train_labels},
y=,
batch_size=64,
epochs=60, # 30 60
callbacks=call_backs,
validation_data=)
目标函数是:loss=(-1/N) Σ ylogy1 + (1-y)log(1-y1) + 0.01 * ((-1/N)Σ ylogy2 + (1-y)log(1-y2)),y是真实值,对应代码中的label_input、y_true;y_1是非对抗样本的预测结果,对应pred、y_pred;y_2是对抗样本的预测结果,对应adv_pred。
训练数据的每个样本是一个10*7矩阵的时序数据(暂时称为x),预测分类结果y_2 = f(x)。在模型的中间过程,我想通过求梯度制造一个噪声(见图中的adv_v_final),并将其与v_final相加,再用相加结果进行激活得到分类预测值。
为了避免出现NoneType' object has no attribute '_inbound_nodes'这个报错,我把求噪声的过程放入一个Lambda层。但是现在出现报错:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "D:\JetBrains\PyCharm 2020.1.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars)# execute the script
File "D:\JetBrains\PyCharm 2020.1.2\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:/PycharmProjects/turningpoint-master/main.py", line 67, in <module>
modelName=modelName)
File "D:\PycharmProjects\turningpoint-master\baseline_main_rnn.py", line 37, in train_rnn_turning_point
clf = rnn_turning_point.build_train(datas, machineID, modelName)
File "D:\PycharmProjects\turningpoint-master\model\rnn_turning_point.py", line 52, in build_train
perturbation = Lambda(gradient_operation)()
File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\keras\engine\base_layer.py", line 474, in __call__
output_shape = self.compute_output_shape(input_shape)
File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\keras\layers\core.py", line 654, in compute_output_shape
return
File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\keras\layers\core.py", line 654, in <listcomp>
return
File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\keras\backend\tensorflow_backend.py", line 593, in int_shape
return tuple(x.get_shape().as_list())
AttributeError: 'NoneType' object has no attribute 'get_shape'
我是一个机器学习和python的小白{:5_96:} 除了这个报错,代码逻辑可能也有问题,对此也请各位不吝赐教:) 抱歉,我应该发到python交流区{:10_282:}
页:
[1]