在tf.metrics.mean_cosine_distance上使用哪个调光?

罗德里戈·西尔维拉

我很困惑哪个dim通常在Tensorflow中指的是哪个实际尺寸,但具体来说是在使用tf.metrics.mean_cosine_distance时

给定

x = [
   [1, 2, 3, 4, 5],
   [0, 2, 3, 4, 5],
]

我想按列计算距离。换句话说,哪个维解析为(伪代码):

mean([
    cosine_distance(x[0][0], x[1][0]),
    cosine_distance(x[0][1], x[1][1]),
    cosine_distance(x[0][2], x[1][2]),
    cosine_distance(x[0][3], x[1][3]),
    cosine_distance(x[0][4], x[1][4]),
])
kmario23

这是dim 0您的意见x将输入构造x为numpy数组后,可以很直观地看到这一点

In [49]: x_arr = np.array(x, dtype=np.float32)

In [50]: x_arr
Out[50]: 
array([[ 1.,  2.,  3.,  4.,  5.],
       [ 0.,  2.,  3.,  4.,  5.]], dtype=float32)


# compute (mean) cosine distance between `x[0]` & `x[1]`
# where `x[0]` can be considered as `labels`
# while `x[1]` can be considered as `predictions`
In [51]: cosine_dist_axis0 = tf.metrics.mean_cosine_distance(x_arr[0], x_arr[1], 0)

dim对应axis于NumPy术语中的名称例如,一个简单的sum操作可以axis 0像这样完成

In [52]: x_arr
Out[52]: 
array([[ 1.,  2.,  3.,  4.,  5.],
       [ 0.,  2.,  3.,  4.,  5.]], dtype=float32)

In [53]: np.sum(x_arr, axis=0)
Out[53]: array([  1.,   4.,   6.,   8.,  10.], dtype=float32)

计算时tf.metrics.mean_cosine_distance如果输入的形状为本质上是在计算向量之间以及沿向量余弦距离(然后取平均值),其中每个向量的长度在哪里(即标签/预测中的条目数)labelspredictionsdim 0(n, )n

但是,如果您将labelspredictions作为列向量传递,则tf.metrics.mean_cosine_distance必须沿着计算dim 1

范例

如果您输入的labelprediction是列向量,

# if your `label` is a column vector
In [66]: (x_arr[0])[:, None]
Out[66]: 
array([[ 1.],
       [ 2.],
       [ 3.],
       [ 4.],
       [ 5.]], dtype=float32)

# if your `prediction` is a column vector
In [67]: (x_arr[1])[:, None]
Out[67]: 
array([[ 0.],
       [ 2.],
       [ 3.],
       [ 4.],
       [ 5.]], dtype=float32)

然后,tf.metrics.mean_cosine_distance必须按照dim 1

# inputs
In [68]: labels = (x_arr[0])[:, None]
In [69]: predictions = (x_arr[1])[:, None]

# compute mean cosine distance between them
In [70]: cosine_dist_dim1 = tf.metrics.mean_cosine_distance(labels, predictions, 1)

tf.metrics.mean_cosine_distance或多或少地在做同样的事情,scipy.spatial.distance.cosine但也需要做mean

对于您的示例案例:

In [77]: x
Out[77]: [[1, 2, 3, 4, 5], [0, 2, 3, 4, 5]]

In [78]: import scipy

In [79]: scipy.spatial.distance.cosine(x[0], x[1])
Out[79]: 0.009132

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Keras中使用tf.metrics?

如何正确使用tf.metrics.accuracy?

如何使用tensorflow tf.metrics.mean_iou?

在Keras中使用tf.metrics.mean_per_class_accuracy

Tensorflow tf.metrics.mean_iou返回0

tf.metrics.mean_squared_error 的准确性

如何正确使用tf.metrics.recall_at_k?

使用tf.metrics.auc进行培训和验证

如何在Tensorflow中正确使用tf.metrics.mean_iou在Tensorboard上显示混淆矩阵?

TensorFlow:如何在tf.contrib.metrics.streaming_mean_iou中获得total_cm

Tensorflow:如何在多类分类中使用tf.keras.metrics?

如何使用tf.metrics计算多标签分类的准确性?

tf.metrics.accuracy与实际精度不符

sigmoid层的tf.keras.metrics.MeanIoU

为什么 'metrics = tf.keras.metrics.Accuracy()' 给出错误但 'metrics=['accuracy']' 不是?

在Tensorflow 2.2中将tf.metrics.MeanIoU()与SparseCategoricalCrossEntropy损失一起使用时,尺寸不匹配错误

tf.metrics 的返回值是什么意思?

tf.keras.metrics.SpecificityAtSensitivity num_thresholds解释

tf.contrib.metrics.f1_score无法导入

tf.metrics.auc与sklearn.metrics.roc_auc_score产生很大不同

tf.loss和tf.metrics有什么区别?

通过eval_metric_ops在Tensorboard中的Tensorflow图tf.metrics.precision_at_thresholds

从tf.metrics.accuracy返回的第一个值代表什么

tf.contrib.metrics.streaming_auc中的update_op返回值的目的是什么

Tensorflow tf.losses.cosine_distance 大于 1

对图像使用sklearn.metrics Jaccard Index?

使用TF训练模型

tf.keras.metrics.TruePositives()在作为度量传递给model.compile()时在model.fit()中返回错误值

哪个SSIM是正确的:skimage.metrics.structural_similarity()?