东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 3112|回复: 0
打印 上一主题 下一主题

[课堂笔记] 同样是计算欧式距离的平方 float32比float16还要快N倍?

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14439
QQ
跳转到指定楼层
楼主
发表于 2020-5-29 14:12:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
同样是计算欧式距离的平方 float32比float16还要快N倍?



  1. import numpy as np
  2. import time

  3. # 同样是计算欧式距离的平方 float32比float16还要快N倍?

  4. face_encodings = np.random.random(size=(300000, 512))
  5. face_to_compare = np.random.random(size=(512, ))

  6. face_encodings = face_encodings.astype(np.float32)
  7. face_to_compare = face_to_compare.astype(np.float32)
  8. # np.float32  1比所有200000个重点人脸库计算欧式距离的平方,耗时(s)= 0.502
  9. # np.float16  1比所有300000个重点人脸库计算欧式距离的平方,耗时(s)= 4.411
  10. # np.float64  1比所有300000个重点人脸库计算欧式距离的平方,耗时(s)= 1.539
  11. print(type(face_encodings), face_encodings.dtype)
  12. print(type(face_to_compare), face_to_compare.dtype)
  13. # return np.linalg.norm(face_encodings - face_to_compare, axis=1)
  14. print('重点库中的人脸编码个数=', len(face_encodings))
  15. print('待比较的人脸编码的shape=', face_to_compare.shape)
  16. start_time = time.time()
  17. # all_distance = np.square(np.linalg.norm(face_encodings - face_to_compare, axis=1))
  18. # 换成这样 时间效率高些
  19. diff = np.subtract(face_encodings, face_to_compare)
  20. all_distance = np.sum(np.square(diff), 1)
  21. print('1比所有%d个重点人脸库计算欧式距离的平方,耗时(s)=' % len(face_encodings), time.time() - start_time)
  22. print("结果距离的shape:", all_distance.shape, all_distance.dtype)
复制代码


让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-5-18 12:45 , Processed in 0.171463 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表