如何阅读从C火炬张量 [英] How to read Torch Tensor from C

查看:290
本文介绍了如何阅读从C火炬张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用火炬框架来训练卷积神经网络,然后用C编写相同的网络
要做到这一点,我必须从我的C程序以某种方式阅读网的了解到参数,但我不能找到一种方法来转换或写入文件火炬张量,使他们在阅读C.
理想情况下,我想张量转换为双阵列在C

有谁知道如何做到这一点?在此先感谢:)


解决方案

  

我无法找到一个方法来转换或写入文件火炬张量,以使他们在C.可读理想情况下,我想张量转换为双阵列中的温度。


最基本的(直接)的办法就是直接 FREAD 用C你有$ P $的数据pviously写入二进制文件。在这样的情况下,你将通常串联的重量和偏见(如有的话),用于每一层

在Lua的/手电筒端,您可以使用文件公用事业字面上 FWRITE 各张量数据。例如这里是一个基本的函数,它是:

 本地FWRITE =功能(张量,文件)
  如果不是张量则返回false结束
  当地N =张量:nElement()
  当地S =张量:存储()
  返回断言(文件:writeDouble(S)== N)
结束

例如,如果 M 手电筒/ NN 包含模块的重量,你会用它如下:

 本地文件= torch.DiskFile(net.bin,W):二进制()
FWRITE(m.weight,文件)
FWRITE(m.bias,文件)

当然,你需要编写自己的逻辑,以确保您 FWRITE 并连接您所有层的权重。在C面,除了 net.bin ,你还需要知道你的网络结构(特别注意:层,比如内核尺寸等参数)知道如何许多块双击 -s在 FREAD

由于(在Lua)的例子你可以看看 overfeat炬(非演示如何看这种纯二进制文件正式项目):请参阅<一个href=\"https://github.com/jhjin/overfeat-torch/blob/09e10a6818ee8e079d923c63a9afc55d86ac4515/run.lua#L83-L100\"相对=nofollow> ParamBank 工具。

请记住,一个强大的解决方案将包括使用适当的二进制序列化格式如 msgpack 或的 Protocol Buffers的,将使这个导出/导入过程的清洁,便于携带。

-

下面是一个玩具的例子:

   - 出口
需要'ン当地FWRITE =功能(张量,文件)
  如果不是张量则返回false结束
  当地N =张量:nElement()
  当地S =张量:存储()
  返回断言(文件:writeDouble(S)== N)
结束本地M = nn.Linear(2,2)打印(m.weight)
打印(m.bias)本地文件= torch.DiskFile(net.bin,W):二进制()
FWRITE(m.weight,文件)
FWRITE(m.bias,文件)

然后在C:

  / *导入* /
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&ASSERT.H GT;INT
主要(无效)
{
  const int的N = 2; / * NB。神经元* /  双* W =的malloc(N * N * sizeof的(*宽)); / * *重/
  双* B =的malloc(N * sizeof的(*宽)); / * *偏见/  FILE * F =的fopen(net.bin,RB);
  断言(FREAD(W,sizeof的(*宽),N * N,F)== N * N);
  断言(FREAD(B,的sizeof(*宽),N,F)== N);
  FCLOSE(F);  INT I,J;
  对于(i = 0; I&LT; N;我++)
    为(J = 0; J&LT; N; J ++)
      的printf(W [%D,%d个=%F \\ N,I,J,W [N * I + J]);  对于(i = 0; I&LT; N;我++)
      的printf(B [%d个=%F \\ N,I,B [I]);  自由(重量);
  免费(B);  返回0;
}

I have to train a convolutional neural network using the Torch framework and then write the same network in C. To do so, I have to read somehow the learned parameters of the net from my C program, but I can't find a way to convert or write to a file the Torch Tensors to make them readable in C. Ideally, I want to convert the Tensors into arrays of double in C.

Does anyone know how to do that? Thanks in advance :)

解决方案

I can't find a way to convert or write to a file the Torch Tensors to make them readable in C. Ideally, I want to convert the Tensors into arrays of double in C.

The most basic (and direct) way is to directly fread in C the data you have previously written into a binary file. In such a case you would typically concatenate the weights and biases (if any) for each layer.

On the Lua/Torch side you can use the File utilities to literally fwrite each tensor data. For example here is a basic function that does that:

local fwrite = function(tensor, file)
  if not tensor then return false end
  local n = tensor:nElement()
  local s = tensor:storage()
  return assert(file:writeDouble(s) == n)
end

For example if m refers to a torch/nn module containing weights you would use it as follow:

local file = torch.DiskFile("net.bin", "w"):binary()
fwrite(m.weight, file)
fwrite(m.bias, file)

Of course you need to write your own logic to make sure you fwrite and concatenate all the weights from all your layers. On the C side, in addition to net.bin, you also need to know the structure of your network (nb. layers, parameters like kernel size, etc) to know how many block of double-s to fread.

As an example (in Lua) you can have a look at overfeat-torch (non official project) that illustrates how to read such a plain binary file: see the ParamBank tool.

Keep in mind that a robust solution would consist in using a proper binary serialization format like msgpack or Protocol Buffers that would make this export/import process clean and portable.

--

Here is a toy example:

-- EXPORT
require 'nn'

local fwrite = function(tensor, file)
  if not tensor then return false end
  local n = tensor:nElement()
  local s = tensor:storage()
  return assert(file:writeDouble(s) == n)
end

local m = nn.Linear(2, 2)

print(m.weight)
print(m.bias)

local file = torch.DiskFile("net.bin", "w"):binary()
fwrite(m.weight, file)
fwrite(m.bias, file)

Then in C:

/* IMPORT */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int
main(void)
{
  const int N = 2; /* nb. neurons */

  double *w = malloc(N*N*sizeof(*w)); /* weights */
  double *b = malloc(N*sizeof(*w));   /* biases */

  FILE *f = fopen("net.bin", "rb");
  assert(fread(w, sizeof(*w), N*N, f) == N*N);
  assert(fread(b, sizeof(*w), N, f) == N);
  fclose(f);

  int i, j;
  for (i = 0; i < N; i++)
    for (j = 0; j < N; j++)
      printf("w[%d,%d] = %f\n", i, j, w[N*i+j]);

  for (i = 0; i < N; i++)
      printf("b[%d] = %f\n", i, b[i]);

  free(w);
  free(b);

  return 0;
}

这篇关于如何阅读从C火炬张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆