How to read the torch tensor from C

I need to train a convolutional neural network using a framework, and then write the same network in C. To do this, I need to somehow read the received network parameters from my C program, but I can’t find a way to convert or write torch tensors to a file, to make them readable in C. Ideally, I want to convert tensors to double arrays in C.

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

+1
source share
1 answer

I cannot find a way to convert or write torch tensors to a file to make them readable in C. Ideally, I want to convert tensors to double arrays in C.

The easiest (and most direct) way is to directly fread in C the data that you previously wrote to the binary. In this case, you usually combine weights and offsets (if any) for each layer.

On the Lua / Torch side, you can use the File utilities literally fwrite for each tensor data. For example, here is the main function that does this:

 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 scales, you should use it as follows:

 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 that you are fwrite and combine 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. Levels, parameters such as kernel size, etc.), in order to find out how many double -s blocks are before fread .

As an example (in Lua), you can look at the overfeat-torch (unofficial project), which illustrates how to read such a simple binary: see the ParamBank tool.

Keep in mind that a reliable solution would be to use the right binary serialization format, such as msgpack or Protocol Buffers , which will make this export / import process clean and portable.

-

Here is an example of a toy:

 -- 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; } 
+4
source

All Articles