|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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 "D:\Anaconda3\envs\edogawaAi\lib\contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\ops.py", line 5652, in get_controller
yield g
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 649, in compute_output_shape
x = self.call(xs)
File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\keras\layers\core.py", line 687, in call
return self.function(inputs, **arguments)
File "D:\PycharmProjects\turningpoint-master\model\rnn_turning_point.py", line 35, in gradient_operation
return K.gradients(loss, [v_final]) + v_final
File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\ops\math_ops.py", line 909, in r_binary_op_wrapper
x = ops.convert_to_tensor(x, dtype=y.dtype.base_dtype, name="x")
File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\ops.py", line 1087, in convert_to_tensor
return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\ops.py", line 1145, in convert_to_tensor_v2
as_ref=False)
File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\ops.py", line 1224, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\constant_op.py", line 305, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\constant_op.py", line 246, in constant
allow_broadcast=True)
File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\constant_op.py", line 284, in _constant_impl
allow_broadcast=allow_broadcast))
File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 466, in make_tensor_proto
_AssertCompatible(values, dtype)
File "D:\Anaconda3\envs\edogawaAi\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 371, in _AssertCompatible
(dtype.name, repr(mismatch), type(mismatch).__name__))
TypeError: Expected float32, got None of type '_Message' instead.
本人初学机器学习和python,除了这个报错,如果代码逻辑也有问题,还请各位不吝赐教:) |
|