// TensorFlowAPI contains most API functions defined in libtensorflow.so
import TensorFlowAPI
// This is the Swift version of TensorFlow classes and objects
import PerfectTensorFlow
// To keep the naming consistency with TensorFlow in other languages such as// Python or Java, making an alias of `TensorFlow` Class is a good idea:publictypealiasTF=TensorFlow
// this action will load all api functions defined// in /usr/local/lib/libtensorflow.sotryTF.Open()
另请注意,您可以激活带有特定路径的库,尤其是在需要不同版本或 CPU/GPU 库调整的情况下
// this action will load the library with the pathtryTF.Open("/path/to/DLL/of/libtensorflow.so")
"Hello, Perfect TensorFlow!"
这是 Swift 版本的 "Hello, TensorFlow!"
// define a string tensorlettensor=tryTF.Tensor.Scalar("Hello, Perfect TensorFlow! 🇨🇳🇨🇦")// declare a new graphletg=tryTF.Graph()// turn the tensor into an operationletop=try g.const(tensor: tensor, name:"hello")// run a sessionleto=try g.runner().fetch(op).addTarget(op).run()// decode the result letdecoded=tryTF.Decode(strings:o[0].data, count:1)// check the resultlets2=decoded[0].string
print(s2)
letg=tryTF.Graph()// the meta signature info defined in a saved modelletmetaBuf=tryTF.Buffer()// load the sessionletsession=try g.load(
exportDir:"/path/to/saved/model",
tags:["tag1","tag2",...],
metaGraphDef: metaBuf)