0

这是我在自动编码器中使用的 EMD 损失函数:

def tril_indices(n, k=0):
m1 = tf.tile(tf.expand_dims(tf.range(n), axis=0), [n, 1])
m2 = tf.tile(tf.expand_dims(tf.range(n), axis=1), [1, n])
mask = (m1 - m2) >= -k
ix1 = tf.boolean_mask(m2, tf.transpose(mask))
ix2 = tf.boolean_mask(m1, tf.transpose(mask))
return ix1, ix2

def ecdf(p):
n = p.get_shape().as_list()[-1]
indices = tril_indices(n)
indices = tf.transpose(tf.stack([indices[1], indices[0]]))
ones = tf.ones([n * (n + 1) / 2])
triang = tf.scatter_nd(indices, ones, [n, n])
return tf.matmul(p, triang)

def emd_loss(p, p_hat, r=2, scope=None):
ecdf_p = ecdf(p)
ecdf_p_hat = ecdf(p_hat)
emd = tf.reduce_mean(tf.pow(tf.abs(ecdf_p - ecdf_p_hat), r), axis=-1)
emd = tf.pow(emd, 1 / r)
return tf.reduce_mean(emd)

def my_loss(y_true, y_pred):
y_true = tf.convert_to_tensor(x_train,  dtype = tf.float32)
y_pred = tf.convert_to_tensor(x_train, dtype= tf.float32)

loss = emd_loss(y_true, y_pred)
return loss

而她就是Autoencoder的架构

自动编码器架构

但我收到了这个错误:

ValueError:变量 <tf.Variable 'conv1d/kernel:0' shape=(3, 1, 256) dtype=float32>None用于渐变。请确保您的所有操作都定义了渐变(即可微分)。没有梯度的常见操作:K.argmax、K.round、K.eval。

4

0 回答 0