torch FloatTensor toString method? - lua

I have a torch.FloatTensor that is 2 rows and 200 columns. I want to print the lines to a text file. Is there a toString() method for the torch.FloatTensor? If not, how do you print the line to the file? Thanks.
I can convert the FloatTensor into a Lua table:
local line = a[1]
local table = {}
for i=1,line:size(1) do
table[i] = line[i]
end
Is there an easy way to convert the Lua table to a string, so I can write it to file? Thanks!

There is a built-in table conversion called torch.totable. If what you want to do is save and load your tensor, then it's even easier to use Torch native serialization like torch.save('file.txt', tensor, 'ascii').

Related

What does "table:insert()" do?

I understand what colon syntax does. I know what table.insert(list, value) does. I'm also aware that I cannot create my own table t={} and insert a value to it with t:insert(value). But when I do table:insert(value) it inserts the value to table which is supposed to a type, right? The worst thing is that I can read this value by calling table[1]. What did I just do? How did I insert a value into a type? Why regular tables don't support colon syntax? I tried to Google it up but I just get information about tables in general, not about this particular case.
What did I just do?
The syntax A:B(C) is nearly equivalent to A.B(A, C), you can check my another answer: link.
So table:insert(value) just means table.insert(table, value), it inserts value into the table table (have nothing to do with t).
How did I insert a value into a type?
table.insert(t, value)
Why regular tables don't support colon syntax?
Because t:insert(value) means t.insert(t, value), but this regular table doesn't have the key insert.
You can solve this by adding the function to the table
t.insert = table.insert
Or using a metatable
setmetatable(t, {__index = {insert = table.insert}})
In Lua the only datatypes where you can use the colon directly for assigned datatype specific methods are string and userdata
But its easy to set the table functions as methods on self created tables...
> myTable = setmetatable({}, {__index = table})
Than you can do...
> myTable:insert("Hi")
> myTable:insert("There")
> print(myTable:concat(" "))
Hi There
But chaining the table methods like the most string methods is not possible.
Because for chaining the method has to return what the next method can work with (as argument).
Like...
> print(("Hi there"):upper():rep(10, ", ")) -- Chaining methods on datatype string
HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE
But you can chain what myTable:concat() will return with string methods...
> print(myTable:concat(" "):rep(10, ", "):upper())
HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE
Because myTable:concat() returns a string.
PS: Thinking about datatype specific methods and deciding to add math functions for datatype number as methods
That will be done with...
> debug.setmetatable(1, {__index = math})
Than math and calculating with datatype number becomes some magic...
> print((0.0).pi) -- Any number has pi
3.1415926535898
> print((1.1).pi:deg()) -- Convert pi to degrees
180.0
> print((180):rad()) -- Convert 180 degrees to radians
3.1415926535898
> print(("%a"):format((1).pi)) -- Pi to HEX float (string)
0x1.921fb54442d18p+1
> print((0x1.921fb54442d18p+1):deg()) -- HEX float to degrees
180.0

Convert a .csv file into a 2D table in Lua

as the title suggests I'd like to know how to convert a .csv file in Lua into a 2D table.
So, for example, say I have a .csv file that looks like this:
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,-1,-1,-1,-1,-1,-1,-1,-1,0,0,-1,-1,-1,-1,-1,-1,-1,-1,0
0,-1,-1,-1,-1,-1,-1,-1,-1,0,0,-1,-1,-1,-1,-1,-1,-1,-1,0
0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0
0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0
0,0,-1,-1,-1,-1,-1,-1,0,0,0,0,-1,-1,-1,-1,-1,-1,0,0
0,0,0,-1,-1,-1,-1,0,0,0,0,0,0,-1,-1,-1,-1,0,0,0
0,0,0,0,-1,-1,0,0,0,0,0,0,0,0,-1,-1,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
How would I convert it into something like this?
local example_table = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,-1,-1,-1,-1,-1,-1,-1,-1,0,0,-1,-1,-1,-1,-1,-1,-1,-1,0},
{0,-1,-1,-1,-1,-1,-1,-1,-1,0,0,-1,-1,-1,-1,-1,-1,-1,-1,0},
{0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0},
{0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0},
{0,0,-1,-1,-1,-1,-1,-1,0,0,0,0,-1,-1,-1,-1,-1,-1,0,0},
{0,0,0,-1,-1,-1,-1,0,0,0,0,0,0,-1,-1,-1,-1,0,0,0},
{0,0,0,0,-1,-1,0,0,0,0,0,0,0,0,-1,-1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}
Your help will be greatly appreciated.
1. Don't underestimate CSV.
If you need it generic, get a proper CSV parsing library. If you do the parsing yourself, you will miss lots of special cases that could happen, so it's only suitable for cases where you know your data and would notice if something went wrong.
2. Changing the file
If you want the equivalent Lua code as output, assuming you're doing the parsing in Lua, you could do something like this:
local input = get_input_somehow() -- probably using io.open, etc.
local output =
"local example_table = {\n"
..
input:gmatch("[^\n]*", function(line)
return "{" .. line .. "};"
end)
..
"\n}"
save_output_somehow(output) -- Probably just write to a new file
3. Parsing CSV into a table
If you want to read the CSV file directly into a Lua table, you could instead do it like this:
local input = get_input_somehow() -- probably using io.open, etc.
local output = {}
input:gmatch("[^\n]", function(line)
local row = {}
table.insert(output, row)
line:gmatch("[^,]", function(item)
table.insert(row, tonumber(item))
end)
end)
do_something_with(output) -- Whatever you need your data for

How to create a tables with variable length with string-like keys in lua

I have a file database. Inside that file I have something like:
DB_A = ...
DB_B = ...
.
.
.
DB_N = ...
I would like to parse the data and group them in lua code like this:
data={}
-- the result after parsing a file
data={
["DB_A"] = {...},
["DB_B"] = {...},
.
.
.
["DB_N"] = {...}
}
In other words, is it possible to create a table inside a table dynamically and assign the key to each table without previously knowing what will be the names of the key (that is something I can figure out after parsing the data from a database).
(Just as a note, I am using Lua 5.3.5; also, I apologize that my code resembles C more than Lua!)
Iterating through your input file line-by-line--which can be done with the Lua FILE*'s lines method--you can use string.match to grab the information you are looking for from each line.
#!/usr/bin/lua
local PATTERN = "(%S+)%s?=%s?(%S+)"
local function eprintf(fmt, ...)
io.stderr:write(string.format(fmt, ...))
return
end
local function printf(fmt, ...)
io.stdout:write(string.format(fmt, ...))
return
end
local function make_table_from_file(filename)
local input = assert(io.open(filename, "r"))
local data = {}
for line in input:lines() do
local key, value = string.match(line, PATTERN)
data[key] = value
end
return data
end
local function main(argc, argv)
if (argc < 1) then
eprintf("Filename expected from command line\n")
os.exit(1)
end
local data = make_table_from_file(argv[1])
for k, v in pairs(data) do
printf("data[%s] = %s\n", k, data[k])
end
return 0
end
main(#arg, arg)
The variable declared at the top of the file, PATTERN, is your capture pattern to be used by string.match. If you are unfamiliar with how Lua's pattern matching works, this pattern looks for a series of non-space characters with zero or one spaces to its right, an equal sign, another space, and then another series of non-space characters. The two series of non-space characters are the two matches--key and value--returned by string.match in the function make_table_from_file.
The functions eprintf and printf are my Lua versions of C-style formatted output functions. The former writes to standard error, io.stderr in Lua; and the latter writes to standard output, io.stdout in Lua.
In your question, you give a sample of what your expected output is. Within your table data, you want it to contain keys that correspond to tables as values. Based on the sample input text you provided, I assume the data contained within these tables are whatever comes to the right of the equal signs in the input file--which you represent with .... As I do not know what exactly those ...s represent, I cannot give you a solid example for how to separate that right-hand data into a table. Depending on what you are looking to do, you could take the second variable returned by string.match, which I called value, and further separate it using Lua's string pattern matching. It could look something like this:
...
local function make_table_from_value(val)
// Split `val` into distinct elements to form a table with `some_pattern`
return {string.match(val, some_pattern)}
end
local function make_table_from_file(filename)
local input = assert(io.open(filename, "r"))
local data = {}
for line in input:lines() do
local key, value = string.match(line, PATTERN)
data[key] = make_table_from_value(value)
end
return data
end
...
In make_table_from_value, string.match will return some number of elements, based on whatever string pattern you provide as its second argument, which you can then use to create a table by enclosing the function call in curly braces. It will be a table that uses numerical indices as keys--rather than strings or some other data type--starting from 1.

Lua - get table hex identifier

I want to know how to get the table hex id. I know that doing:
local some_var = {}
print (some_var)
the result is (for instance):
table: 0x21581c0
I want the hex without the table: string. I know that maybe some of you suggest me to make a regular expression (or something similar) to remove those chars, but I want to avoid that, and just get the 0x21581c0
Thanks
This is simpler and works for all types that are associated with pointers:
local function getId(t)
return string.format("%p", t)
end
print("string:", getId("hi"))
print("table:", getId({}))
print("userdata:", getId(io.stdin))
print("function:", getId(print))
print("number:", getId(1))
print("boolean:", getId(false))
print("nil:", getId(nil))
Result:
string: 0x0109f04638
table: 0x0109f0a270
userdata: 0x01098076c8
function: 0x0109806018
number: NULL
boolean: NULL
nil: NULL
In the standard implementation, there is the global 'print' variable that refers to a standard function that calls, through the global variable 'tostring', a standard function described here. The stanard 'tostring' function is the only way to retrieve the hexadecimal number it shows for a table.
Unfortunately, there is no configuration for either of the functions to do anything differently for all tables.
Nonetheless, there are several points for modification. You can create you own function and call that every time instead, or point either of the the global variables print or tostring to you own functions. Or, set a __tostring metamethod on each table you need tostring to return a different answer for. The advantage to this is it gets you the format you want with only one setup step. The disadvantage is that you have to set up each table.
local function simplifyTableToString(t)
local answer = tostring(t):gsub("table: ", "", 1)
local mt = getmetatable(t)
if not mt then
mt = {}
setmetatable(t, mt)
end
mt.__tostring = function() return answer end
end
local a = {}
local b = {}
print(a, b)
simplifyTableToString(a)
print(a, b)
Without complex patterns, you can just search for the first space, and grab the substring of what follows.
function get_mem_addr (object)
local str = tostring(object)
return str:sub(str:find(' ') + 1)
end
print(get_mem_addr({})) -- 0x109638
print(get_mem_addr(function () end)) -- 0x108cf8
This function will work with tables and functions, but expect errors if you pass it anything else.
Or you can use a little type checking:
function get_mem_addr (o)
return tostring(o):sub(type(o):len() + 3)
end
The table id stated by the OP is invalid in the version of Lua I am using (5.1 in Roblox). A valid ID is length 8, not 9 as in your example. Either way, just use string.sub to get the sub-string you are after.
string.sub(tostring({}), 8)
The reason is, 'table: ' is 7 characters long, so we take from index 8 through the end of the string which returns the hex value.

How to convert torch Tensor/ Storage to a lua table?

If I have a tensor:
t1 = torch.Tensor(2, 2)
Is there any way get this data as a Lua table?
There is a dedicated constructor to create a tensor from a table but so far there is no method out-of-the box to convert the other way around.
Of course you can do that manually:
-- This assumes `t1` is a 2-dimensional tensor!
local t2 = {}
for i=1,t1:size(1) do
t2[i] = {}
for j=1,t1:size(2) do
t2[i][j] = t1[i][j]
end
end
--
Update: as of commit 10f3323 there is now a dedicated torch.totable(object) converter.

Resources