python - 带有马氏距离损失的 Keras 自定义损失函数如何

标签 python tensorflow keras mahalanobis

我正在尝试使用 Mahalanobis 距离损失在 Keras 中实现自定义损失函数。但是我总是遇到这个烦人的错误。

Mahalanobis 距离(或其平方值的“广义平方点间距离”[3])也可以定义为具有协方差矩阵 S 的相同分布的两个随机向量 x 和 y 之间的相异性度量。

d(x,y) = 平方 [转置(x-y) * 逆(S)* (x-y)]

( https://en.wikipedia.org/wiki/Mahalanobis_distance )

n_classes = 4
n_samples=800
X, y = make_classification(n_samples=n_samples, n_features=20, n_informative=4, n_redundant=0, n_classes=n_classes, n_clusters_per_class=2)
y = to_categorical(y)
Xtrainb, testXb, ytrainb, ytestb = train_test_split(X, y, test_size = 0.3, random_state=42)

x_trainb = np.reshape(Xtrainb, (Xtrainb.shape[0], Xtrainb.shape[1], 1))
Xtestb = np.reshape(testXb, (testXb.shape[0], testXb.shape[1], 1))

densesize = 4
input_datab = Input(shape=(Xtrainb.shape[1],1)) 
epochs = 10
batch_size = 32
dropout= 0.1
lr= 0.001

########
def mahalanobis(y_true, y_pred):
    x_minus_mn_with_transpose = K.transpose(y_true - y_pred)
    Covariance = covr1(y_true, y_pred)
    inv_covmat = tf.linalg.inv(Covariance)
    x_minus_mn = y_true - y_pred
    left_term = K.dot(x_minus_mn, inv_covmat)
    D_square = K.dot(left_term, x_minus_mn_with_transpose)
    return D_square 

def covr1(y_true, y_pred):
    #x_mean = K.mean(y_true)
    #y_mean = K.mean(y_pred)
    Cov_numerator = K.sum(((y_true - y_pred)*(y_true - y_pred)))
    Cov_denomerator = len(Xtrainb)-1
    Covariance = (Cov_numerator / Cov_denomerator)
    return Covariance


conv1= Conv1D(filters=80, kernel_size=2, padding='same',   input_dim=Xtrainb.shape[1])(input_datab)
maxpool = MaxPooling1D(pool_size=3, stride=3 )(conv1)
conv2= Conv1D(filters=50, kernel_size=2, padding='same',   input_dim=Xtrainb.shape[1])(maxpool)
maxpool = MaxPooling1D(pool_size=3, stride=3)(conv2)
flatten = Flatten()(maxpool)
dense = Dense(84, activation='relu')(flatten)
dense = Dense(1024, activation='relu')(flatten)
dense = Dense(densesize, activation='softmax')(dense)
model = Model(inputs=[input_datab],outputs=[dense])
model.compile(loss= mahalanobis,  optimizer='adam', metrics=['acc'])
hist = model.fit(x_trainb, ytrainb, validation_data=(Xtestb, ytestb), epochs=epochs, batch_size=batch_size)





ValueError:形状必须至少为 2 级,但对于输入形状为 [] 的“loss_88/dense_270_loss/MatrixInverse”(操作:“MatrixInverse”)为 0 级。

最佳答案

您的代码的问题是在计算协方差矩阵时
有我的马哈拉诺比斯距离:希望这对你有用;因为它对我有用:)

def mahala_dist(m, n):
 diff = m - n
 cov = tfp.stats.covariance(tf.transpose(n))
 mull = K.dot(tf.linalg.inv(cov), diff)
 mull2 = K.dot(mull, tf.transpose(diff))
 dist = tf.sqrt(mull2)
 return dist
它适用于 TF 和 Keras 框架。
祝你好运。

关于python - 带有马氏距离损失的 Keras 自定义损失函数如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58351249/

相关文章:

python - pythonanywhere 媒体文件夹出现问题

python - Tkinter Gui 读取 csv 文件并根据第一行中的条目生成按钮

python - Numpy 向量化相对距离

python - Google Cloud ML FAILED_PRECONDITION

tensorflow - 在 Debug模式下将文件名和行号添加到 tensorflow op name

python - Keras 将功能模型转换为模型子类

python - 在CNN中,如何查看多个filter的权重?

python - 转换 Pandas 数据框的数据类型以匹配另一个

python - 将 pandas 列转换为特定位置的列表

python - 如何计算给定输入和预期输出的 ctc 概率?