// Get the output tensor to process the inference results.
outputTensor = try interpreter.output(at: 0)
回想一下,这个模型是在5种不同类型的花上训练的,所以模型的输出将是5个值,每
个值都是图像包含特定花的概率。顺序是按字母顺序排列的,所以我们识别的花是雏
菊、蒲公英、玫瑰、向日葵和郁金香,这5 个值将与它们对应。因此,例如,第一个输
出值是图像包含雏菊的可能性,以此类推。
这些值是概率,因此它们在0和1 之间,表示为浮点数。你可以读取输出张量并像这样
将其转换为数组:
let resultsArray =
outputTensor.data.toArray(type: Float32.self)
现在,如果你想确定图像中最有可能包含的花,你可以回到纯 Swift 中,获取最大值,
找到该值的索引,并查找与该索引对应的标签!
// Pick the biggest value in the array
let maxVal = resultsArray.max()
// Get the index of the biggest value
let resultsIndex = ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.