python 环境
包 | 版本 |
---|---|
tensorflow | 2.6.0 |
tensorflow-js | 3.18.0 |
uniapp(vue)
完整引入包
{"dependencies": {"@tensorflow/tfjs-core": "3.5.0","@tensorflow/tfjs-converter":"3.5.0","@tensorflow/tfjs-backend-webgl":"3.5.0","@tensorflow/tfjs-backend-cpu":"3.5.0","fetch-wechat": "0.0.3"}
}
在package.json下配置
{"dependencies": {"@tensorflow/tfjs-core": "3.5.0","@tensorflow/tfjs-converter":"3.5.0",}
}
PS: 许多文章都说只需要引入tensorflow.js 子包,但是子包所蕴涵的功能并不全面。如果你只是一个新手,最好是直接加载@tensorflow/tfjs。后续慢慢了解自己需要用到那几个模块再进行删减.
具体各个包的功能如下
包 | 功能说明 |
---|---|
tfjs-core | 基础包(数据基本结构) |
tfjs-converter | 模型导入 |
tfjs-layers | 自己构建模型 |
tfjs-data | 数据流 |
tfjs-backend-webgl | 作为前端使用时需要导入 |
tfjs-backend-cpu | 作为前端使用时需要导入 |
后续在目标项目的路径下打开命令行, 输入指令
npm install
当然,你有更好的方法也可以指明或留言
单纯只是在web环境运行你的配置只需要
{"@tensorflow/tfjs-backend-webgl":"3.5.0"
}
如果你的代码需要在微信小程序上运行则需要同时引入两个包
{"@tensorflow/tfjs-backend-webgl":"3.5.0","@tensorflow/tfjs-backend-cpu":"3.5.0",
}
同时你所引入的这个包,需要在你对应的页面或者公共地方引入
require('@tensorflow/tfjs-backend-webgl');require('@tensorflow/tfjs-backend-cpu')
如果你已经和引入fetchWetch , 并且在对应的页面已经引入。那么你估计是没有调用插件代码。
你需要在公共文件的onLaunch 内写下这段代码
var fetchWechat = require('fetch-wechat');var tf = require('@tensorflow/tfjs-core');var webgl = require('@tensorflow/tfjs-backend-webgl');var plugin = requirePlugin('tfjsPlugin');plugin.configPlugin({// polyfill fetch functionfetchFunc: fetchWechat.fetchFunc(),// inject tfjs runtimetf,// inject webgl backendwebgl,// provide webgl canvascanvas: wx.createOffscreenCanvas()});
但是这时候一般都会有问题,由于requirePlugin ,似乎是只有在微信小程序才会永远的功能。如果我们在现在调用会直接报错。
面对这种情况,我们只有两种方法
// 获取平台基本信息let systemInfo = uni.getSystemInfoSync()// 获取当前的平台let uniPlatform = systemInfo.uniPlatformif (uniPlatform == "mp-weixin") {var fetchWechat = require('fetch-wechat');var tf = require('@tensorflow/tfjs-core');var webgl = require('@tensorflow/tfjs-backend-webgl');var plugin = requirePlugin('tfjsPlugin');plugin.configPlugin({// polyfill fetch functionfetchFunc: fetchWechat.fetchFunc(),// inject tfjs runtimetf,// inject webgl backendwebgl,// provide webgl canvascanvas: wx.createOffscreenCanvas()});}},
上一篇:第十四届蓝桥杯模拟赛 第二期题解
下一篇:贪心算法-单源最短路径