鱼C论坛

 找回密码
 立即注册
查看: 1172|回复: 1

用Keras实现对抗训练+lstm的一个报错

[复制链接]
发表于 2020-8-6 09:08:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 猪仔很忙 于 2020-8-7 18:12 编辑

我正在尝试建立结合对抗训练的LSTM模型,代码如下:
def gradient_operation(args):
    y_true = args[0]
    y_pred = args[1]
    v_final = args[2]
    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[0]
    time_step = train_data.shape[1]
    feature_dim = train_data.shape[2]

    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)([label_input, pred, v_final])
    adv_pred = Dense(1, activation='sigmoid', name='adv_pred')(v_final_adv)
    model = Model(inputs=[trace_input, label_input], outputs=[pred, adv_pred])
    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 = [Call_back_0(valid_data=[val_data, val_labels, test_data, test_labels], # 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=[train_labels, train_labels],
            batch_size=64,
            epochs=60, # 30 60
            callbacks=call_backs,
            validation_data=[val_data, val_labels])
目标函数是: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)([label_input, pred, v_final])
  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 [K.int_shape(x_elem) for x_elem in x]
  File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\keras\layers\core.py", line 654, in <listcomp>
    return [K.int_shape(x_elem) for x_elem in x]
  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的小白 除了这个报错,代码逻辑可能也有问题,对此也请各位不吝赐教:)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-8-6 09:12:32 | 显示全部楼层
抱歉,我应该发到python交流区
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-11 08:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表