在 swift 中找到两个 MLMultiArrays 之间的距离

Find the distance between two MLMultiArrays in swift

我制作了一个 coreML 模型,它将拍摄图像 (96x96) 并输出 MultiArray (Double 128)。这是一个人脸识别模型,我需要找到这些 MultiArrays 中的两个之间的距离(平方距离)。这怎么可能?

这些向量的示例:

--------
Vector1
--------
Double 128 vector
[0.01845766976475716,0.05280150845646858,0.1369639039039612,-0.0252350065857172,0.01947484165430069,0.1531472355127335,-0.06450469791889191,-0.05124717578291893,0.0004407268716022372,-0.09389982372522354,0.003280093660578132,0.0743335485458374,0.07707648724317551,-0.02539501152932644,0.05641303956508636,0.007531643845140934,-0.09060829877853394,-0.06427612155675888,-0.1163918972015381,0.160278856754303,0.1260835975408554,-0.02992085367441177,0.04576130583882332,0.1053287237882614,-0.04093831032514572,0.023772107437253,-0.1365067511796951,-0.1086202412843704,-0.02165776118636131,0.04445841163396835,0.06637904047966003,0.04886996746063232,0.04336123913526535,0.1434555202722549,0.0009678847854956985,0.1387925297021866,0.06199034303426743,0.1622903496026993,0.0325266420841217,-0.01909768767654896,-0.01476613618433475,-0.07575073093175888,-0.00176005030516535,-0.1617417633533478,0.002060058759525418,-0.02092631161212921,0.1287351101636887,-0.02372639253735542,-0.1242549791932106,0.1332152336835861,-0.04404697194695473,-0.02562358789145947,0.006291608326137066,0.1066087558865547,0.1756393015384674,0.0369839146733284,-0.03209234401583672,0.06528186053037643,-0.1533300876617432,-0.08855109661817551,-0.0881853774189949,0.06153318658471107,0.09280265122652054,-0.2009657323360443,0.06898482888936996,0.06756764650344849,0.02573787793517113,0.06999056786298752,-0.1814908981323242,0.05463013052940369,0.01590902544558048,0.008274521678686142,-0.137786790728569,0.03670961782336235,0.1239806860685349,0.04873282089829445,0.02708648703992367,-0.04704134166240692,0.1348609924316406,0.1050544306635857,-0.04262978956103325,0.04100688546895981,0.01750907115638256,0.01980628073215485,-0.02896082773804665,-0.007560215890407562,0.0266979057341814,0.07808222621679306,0.06839052587747574,0.1548844277858734,0.08475670963525772,-0.177742213010788,0.0980142280459404,0.1826795041561127,-0.1060601696372032,-0.009234549477696419,-0.05275579169392586,0.04224120453000069,0.03110945969820023,0.003620103467255831,0.01845766976475716,-0.03104088641703129,-0.2148632854223251,0.06025315076112747,-0.01897197030484676,0.1699705719947815,-0.0732363760471344,-0.05188719555735588,-0.003505814354866743,-0.07620789110660553,-0.07273350656032562,0.1166661903262138,-0.08567101508378983,0.03245807066559792,0.09499699622392654,0.08077944815158844,-0.07072202116250992,-0.04160118848085403,-0.04582988098263741,0.1963941752910614,0.08603674173355103,0.01219463348388672,0.06016171723604202,0.03108660131692886,0.01193176954984665,0.1359581649303436,0.1495814174413681,-0.03700676932930946]


--------
Vector2
--------
Double 128 vector
[0.02237874269485474,0.02019208669662476,0.1252106428146362,0.07265211641788483,0.05405569449067116,0.2141740173101425,0.009702051989734173,-0.05945338308811188,0.08470826596021652,-0.1504261195659637,-0.06914558261632919,0.04349670931696892,-0.0003976856241934001,-0.1272594034671783,0.0492095947265625,0.0007873682770878077,-0.1196947544813156,0.07324310392141342,-0.1172519996762276,-0.003247970947995782,0.1728836596012115,0.01823197677731514,0.07647383213043213,0.2072397619485855,0.0412115566432476,-0.1766659915447235,-0.1332480758428574,-0.2247330099344254,-0.07777401059865952,0.09747360646724701,0.1487713605165482,0.0340409055352211,-0.01604532264173031,0.1185915768146515,0.09786760061979294,0.0247820932418108,0.07233691960573196,-0.02210294827818871,-0.02600346878170967,-0.01637036539614201,0.06879099458456039,-0.06173853576183319,0.05815321207046509,-0.07639503479003906,-0.03888700529932976,-0.1155184358358383,0.1070082113146782,-0.04495447874069214,-0.04065996780991554,0.09747360646724701,0.0683182030916214,-0.1689437478780746,0.136872798204422,0.03922189772129059,0.09337609261274338,0.08715102076530457,0.06839700043201447,0.0495641864836216,-0.009756225161254406,-0.1134696751832962,-0.07489787042140961,0.09392768144607544,-0.02011328935623169,-0.2455357760190964,0.09440046548843384,-0.02779613062739372,0.03715344145894051,-0.1083477810025215,-0.1848610192537308,0.1079537943005562,-0.04755482822656631,0.04751542955636978,-0.1108693331480026,-0.05523766949772835,0.01849792152643204,0.04172374680638313,-0.02194535173475742,-0.0339227057993412,0.03155875578522682,0.1018863171339035,-0.01013544294983149,0.0796651691198349,0.007869988679885864,0.02159075811505318,0.004843638278543949,0.06311751157045364,0.003545927349478006,-0.02377741411328316,-0.1369515955448151,0.1093721613287926,0.06122634932398796,-0.1382123678922653,0.01784783415496349,-0.01308053266257048,-0.09952236711978912,0.0678454115986824,-0.1078749969601631,0.1101601496338844,0.02539278008043766,0.1016499176621437,0.09432166814804077,-0.05547406524419785,-0.09424287080764771,0.07757700979709625,0.05114015564322472,-0.0263383612036705,-0.03205124288797379,-0.05712883174419403,-0.1130756884813309,0.04042357206344604,-0.1119725108146667,0.02363951690495014,-0.01370107010006905,-0.08620543777942657,0.01069688145071268,0.1217435076832771,-0.06063535809516907,0.02088157273828983,-0.07304610311985016,0.1570451855659485,0.1333268731832504,0.02365921624004841,-0.03378481045365334,-0.01813347823917866,-0.01129771862179041,0.08305349946022034,0.02659445628523827,-0.005476487800478935]

我想这会帮助你找到两个向量之间的距离,

import UIKit
import Foundation

var sum = 0.0
var array1 = [first array elements]
var array2 = [second array elements]
var result = 0.0
        
for i in 0..<array1.count {
    let temp = (array1[i] - array2[i])*(array1[i] - array2[i])
    sum = sum + temp
}

result = Double(sqrt(sum))