简体   繁体   English

TensorFlow张量到TensorFlow.js张量

[英]TensorFlow Tensor to a TensorFlow.js tensor

I was trying to take tensor from a ConvNet output and use it in another model in TF.js. 我试图从ConvNet输出中获取张量,并在TF.js中的另一个模型中使用它。 Is there any way to save a TF tensor to a file ans load that file in TF.js and get the tensor back? 有什么方法可以将TF张量保存到文件并在TF.js中加载该文件并恢复张量?

A tensor can be saved to a file. 张量可以保存到文件中。 However,to load it back a server is required as the browser does not access the file system directly unless using an input type file . 但是,将其加载回服务器是必需的,因为除非使用input type file否则浏览器不会直接访问文件系统。

To save a tensor to a file, the tensor values have to be downloaded first using data or dataSync . 要将张量保存到文件中,必须先使用datadataSync下载张量值。

const tensor = tf.tensor([1, 2])
// download values
const values = tensor.dataSync()

To see how you can save values to a file, you can use the following answer 若要查看如何将values保存到文件,可以使用以下答案

To load back the tensor saved, the file content needs to be served by a server with an http request. 要加载已保存的张量,文件内容需要由带有http请求的服务器提供。 Upon receiving the response of the request, 收到请求的回复后,

const tensorRetrieved = tf.tensor(contentHttpResponse) // if needed parse the string response using JSON.parse

The other option would be to use tensorflow.js on node. 另一个选择是在节点上使用tensorflow.js。 An http request and response would not be needed as node can access the file system. 由于节点可以访问文件系统,因此不需要http请求和响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM