TensorFlow2.x计算两个向量的欧氏距离(Python3)

一、计算原理

        假设有两个d维向量\vec{v_{1}}\vec{v_{2}},其中\vec{v_{1}}=(x_{1},x_{2},...,x_{d})\vec{v_{2}}=(y_{1},y_{2},...,y_{d})。那么根据欧氏距离计算公式,它们之间的欧氏距离为

\sqrt{\sum_{i=1}^{d}(x_{i}-y_{i})^{2}}=\sqrt{(x_{1}-y_{1})^{2}+(x_{2}-y_{2})^{2}+...+(x_{d}-y_{d})^{2}}

二、利用Python3直接计算

import math
# calculating euclidean distance by formulation
# vector1 and vector2 are both 1d tensor (1d tensor = vector)
def euclidean_distance(vector1, vector2, dim):
    s = 0
    for i in range(dim):
        s = s + (vector1[i] - vector2[i]) ** 2
    return math.sqrt(s)

三、调用TensorFlow2.x函数计算

import tensorflow as tf
# calculating euclidean distance by tensorflow2.x
# vector1 and vector2 are both 1d tensor (1d tensor = vector)
def euclidean_distance_by_tf(vector1, vector2):
    return tf.sqrt(tf.reduce_sum(tf.square(vector1 - vector2)))

        代码解释(个人的理解):第一步:vector1 - vector2,得到一个新的临时向量temp_vector1;第二步:对temp_vector1的每个元素求平方,得到一个新的临时向量temp_vector2;第三步:对temp_vector2的每个元素求和,得到一个标量s;第四步:对s开方。最终求得两个向量之间的欧氏距离。所以,上述代码与下面的代码等价:

import tensorflow as tf
# calculating euclidean distance by tensorflow2.x
# vector1 and vector2 are both 1d tensor (1d tensor = vector)
def euclidean_distance_by_tf(vector1, vector2):
    temp_vector1 = vector1 - vector2
    temp_vector2 = tf.square(temp_vector1)
    s = tf.reduce_sum(temp_vector2)
    return tf.sqrt(s)

        在你搜索到的有关博客中,有可能是带tf.Session()的,但tf.Session()实际上是TensorFlow1.x的内容,那么在TensorFlow2.x版本中是已经被完全抛弃。所以在使用TensorFlow2.x时,如果你是一位TensorFlow初学者,那么强烈建议不要使用像下面一样的代码:

with tf.Session() as sess:
    #TODO

四、参考

        tf.reduce_sum() 

        tf.square()

五、附录

        完整代码

import math
import tensorflow as tf

# test case for calculating euclidean distance of two vectors by tensorflow
def tc():
    # create vectors
    zero = tf.constant([0, 0, 0], tf.float32)
    v1 = tf.constant([1, 0, 2], tf.float32)
    v2 = tf.constant([99, 1000, -200], tf.float32)
    print(zero)
    print(v1)
    print(v2)

    # calculating euclidean distance by tensorflow
    print(euclidean_distance_by_tf(v1, zero))
    print(euclidean_distance_by_tf(v2, zero))
    print(euclidean_distance_by_tf(v1, v2))
    print(euclidean_distance_by_tf(v2, v1))

    # calculating euclidean distance by formulation
    print(euclidean_distance(v1, zero, 3))
    print(euclidean_distance(v2, zero, 3))
    print(euclidean_distance(v1, v2, 3))
    print(euclidean_distance(v2, v1, 3))

# calculating euclidean distance by tensorflow
def euclidean_distance_by_tf(vector1, vector2):
    return tf.sqrt(tf.reduce_sum(tf.square(vector1 - vector2)))

# calculating euclidean distance by tensorflow
def euclidean_distance_by_tf1(vector1, vector2):
    temp_vector1 = vector1 - vector2
    temp_vector2 = tf.square(temp_vector1)
    s = tf.reduce_sum(temp_vector2)
    return tf.sqrt(s)

# calculating euclidean distance by formulation
def euclidean_distance(vector1, vector2, dim):
    s = 0
    for i in range(dim):
        s = s + (vector1[i] - vector2[i]) ** 2
    return math.sqrt(s)

# enter of program
if __name__ == '__main__':
    tc()

        输出

tf.Tensor([0. 0. 0.], shape=(3,), dtype=float32)
tf.Tensor([1. 0. 2.], shape=(3,), dtype=float32)
tf.Tensor([  99. 1000. -200.], shape=(3,), dtype=float32)
tf.Tensor(2.236068, shape=(), dtype=float32)
tf.Tensor(1024.598, shape=(), dtype=float32)
tf.Tensor(1024.8942, shape=(), dtype=float32)
tf.Tensor(1024.8942, shape=(), dtype=float32)
2.23606797749979
1024.598030937011
1024.8941408750466
1024.8941408750466

欧氏距离的公式是:d(x,y) = sqrt((x1-y1)^2 + (x2-y2)^2 + ... + (xn-yn)^2) 以下是使用CUDA实现的代码示例: ```C++ #include <stdio.h> #include <stdlib.h> #include <cuda_runtime.h> #define N 1024 __global__ void euclideanDistance(float *x, float *y, float *result) { int tid = blockIdx.x * blockDim.x + threadIdx.x; if (tid < N) { float sum = 0.0f; for (int i = 0; i < N; i++) { float diff = x[i] - y[i]; sum += diff * diff; } result[tid] = sqrt(sum); } } int main() { float *x, *y, *result; cudaMallocManaged(&x, N * sizeof(float)); cudaMallocManaged(&y, N * sizeof(float)); cudaMallocManaged(&result, N * sizeof(float)); // initialize x and y with random values for (int i = 0; i < N; i++) { x[i] = static_cast<float>(rand()) / RAND_MAX; y[i] = static_cast<float>(rand()) / RAND_MAX; } int blockSize = 256; int numBlocks = (N + blockSize - 1) / blockSize; euclideanDistance<<<numBlocks, blockSize>>>(x, y, result); cudaDeviceSynchronize(); // print the result for (int i = 0; i < N; i++) { printf("Distance between x and y[%d] = %f\n", i, result[i]); } cudaFree(x); cudaFree(y); cudaFree(result); return 0; } ``` 在这个示例中,我们使用了CUDA的并行计算能力,通过在GPU上同时计算多个距离来加速计算。首先,我们在GPU上分配了内存用于存储两个向量计算结果。然后,我们使用随机数初始化了两个向量。接下来,我们将距离计算函数euclideanDistance定义为一个CUDA核函数,在每个线程中计算两个向量之间的距离。最后,我们在主程序中调用这个核函数,并用cudaDeviceSynchronize()等待所有线程完成计算。最后打印结果并释放内存。 请注意,由于这个示例使用了CUDA,因此需要在支持CUDA的GPU上运行。如果您的计算机不支持CUDA,或者您没有安装CUDA,那么您将无法运行这个示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞机火车巴雷特

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值