我的custom_loss函数有问题。
我有(200,50,1)形状的y_true和y_pred数据
但我需要忽略我的损失函数的第一个元素。所以我在没有每个向量的第一个元素的情况下创建局部y_pred和y_true,而y_true_new / y_pred_new形状是(200,49,1)。
之后我需要找到他们的腹肌差异。然后我需要用条件来执行它:
差异< 0.5 => 0
差异> = 0.5 => 1
然后我尝试将这个bool张量转换为float32并总结这些数字。我需要尽量减少这个价值
但是在运行时K.cast()之后出现错误:
Traceback (most recent call last):
File "/home/bocharick/HDD/UbuntuFiles/PycharmProjects/punctuation/bin/keras_punctuator_train.py", line 195, in <module>
model.fit(X, Y, epochs=1, batch_size=BATCH_SIZE, verbose=1, callbacks=[tensorboard, save_model_weights, save_pretrain_model])
File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 867, in fit
initial_epoch=initial_epoch)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 1575, in fit
self._make_train_function()
File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 960, in _make_train_function
loss=self.total_loss)
File "/usr/local/lib/python3.5/dist-packages/keras/legacy/interfaces.py", line 87, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/keras/optimizers.py", line 432, in get_updates
m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/math_ops.py", line 856, in binary_op_wrapper
y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y")
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 611, in convert_to_tensor
as_ref=False)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 676, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/constant_op.py", line 121, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/constant_op.py", line 102, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_util.py", line 364, in make_tensor_proto
raise ValueError("None values not supported.")
ValueError: None values not supported.
我的custom_loss函数是:
BATCH_SIZE = 200
MAX_SEQUENCE_LEN = 50
def custom_loss(y_true, y_pred):
y_true_new = K.reshape(y_true, (BATCH_SIZE, MAX_SEQUENCE_LEN))
y_true_new = K.transpose(y_true_new)
y_true_new = y_true_new[1:]
y_true_new = K.transpose(y_true_new)
y_true_new = K.reshape(y_true_new, (BATCH_SIZE, MAX_SEQUENCE_LEN-1, 1))
y_pred_new = K.reshape(y_pred, (BATCH_SIZE, MAX_SEQUENCE_LEN))
y_pred_new = K.transpose(y_pred_new)
y_pred_new = y_pred_new[1:]
y_pred_new = K.transpose(y_pred_new)
y_pred_new = K.reshape(y_pred_new, (BATCH_SIZE, MAX_SEQUENCE_LEN-1, 1))
diff = y_true_new - y_pred_new
#print(1, diff.shape, diff)
diff = K.abs(diff)
#print(2, diff.shape, diff)
diff = K.greater(diff, 0.5)
#print(3, diff.shape, diff)
diff = K.cast(diff, 'float32')
#print(4, diff.shape, diff)
return K.sum(diff)
我的模特是:
model = Sequential()
model.add(Bidirectional(LSTM(128, return_sequences=True), input_shape=(MAX_SEQUENCE_LEN, 1)))
model.add(TimeDistributed(Dense(1, activation="sigmoid")))