Lua 命令,它们是做什么的?
Lua commands , what do they do?
我不熟悉lua。
但是文章的作者使用了lua.
你能帮我理解这两行的作用吗:
做什么
replicate(x,batch_size)
干什么?
x = x:resize(x:size(1), 1):expand(x:size(1), batch_size)
是做什么的?
原始源代码可以在这里找到
https://github.com/wojzaremba/lstm/blob/master/data.lua
这基本上可以归结为简单的数学运算并在 torch 手册中查找一些函数。
好吧我很无聊所以...
replicate(x,batch_size)
定义在 https://github.com/wojzaremba/lstm/blob/master/data.lua
-- Stacks replicated, shifted versions of x_inp
-- into a single matrix of size x_inp:size(1) x batch_size.
local function replicate(x_inp, batch_size)
local s = x_inp:size(1)
local x = torch.zeros(torch.floor(s / batch_size), batch_size)
for i = 1, batch_size do
local start = torch.round((i - 1) * s / batch_size) + 1
local finish = start + x:size(1) - 1
x:sub(1, x:size(1), i, i):copy(x_inp:sub(start, finish))
end
return x
end
此代码使用 Torch 框架。
x_inp:size(1)
returns Torch tensor(潜在的多维矩阵)的第 1 维大小 x_inp
.
见https://cornebise.com/torch-doc-template/tensor.html#toc_18
所以 x_inp:size(1)
给出了 x_inp
中的行数。 x_inp:size(2)
,会给你列数...
local x = torch.zeros(torch.floor(s / batch_size), batch_size)
创建一个新的用零填充的二维张量并创建一个本地引用,命名为 x
行数是根据 s
、x_inp
的行数和 batch_size
计算得出的。因此,对于您的示例输入,结果是 floor(11/2) = floor(5.5) = 5
.
您的示例中的列数是 2,因为 batch_size
是 2。
手电筒。
所以简单地说x
就是5x2矩阵
0 0
0 0
0 0
0 0
0 0
以下行将 x_inp
的内容复制到 x
。
for i = 1, batch_size do
local start = torch.round((i - 1) * s / batch_size) + 1
local finish = start + x:size(1) - 1
x:sub(1, x:size(1), i, i):copy(x_inp:sub(start, finish))
end
在第一个运行中,start
计算为1,finish
计算为5
,因为x:size(1)
当然是[的行数=27=] 即 5
。 1+5-1=5
在第二个 运行 中,start
计算为 6,finish
计算为 10
因此 x_inp
的前 5 行(您的第一批)被复制到 x
的第一列,第二批被复制到 x
的第二列
x:sub(1, x:size(1), i, i)
是 x
的子张量,第 1 到 5 行,第 1 到 1 列和第二个 运行 的第 1 到 5 行,第 2 到 2 列(在你的例子中)。所以无非就是x
的第一列和第二列
见https://cornebise.com/torch-doc-template/tensor.html#toc_42
:copy(x_inp:sub(start, finish))
将 x_inp
中的元素复制到 x
的列中。
总而言之,您获取一个输入张量并将其分成多个批次,这些批次存储在一个张量中,每个批次有一列。
所以 x_inp
0
1
2
3
4
5
6
7
8
9
10
和batch_size = 2
x
是
0 5
1 6
2 7
3 8
4 9
进一步:
local function testdataset(batch_size)
local x = load_data(ptb_path .. "ptb.test.txt")
x = x:resize(x:size(1), 1):expand(x:size(1), batch_size)
return x
end
是另一个从文件中加载一些数据的函数。这个 x
与上面的 x
无关,只是两者都是张量。
让我们用一个简单的例子:
x
正在
1
2
3
4
和batch_size = 4
x = x:resize(x:size(1), 1):expand(x:size(1), batch_size)
首先x
将被调整为4x1,阅读https://cornebise.com/torch-doc-template/tensor.html#toc_36
然后通过复制第一行 3 次将其扩展为 4x4。
导致 x
成为张量
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
阅读https://cornebise.com/torch-doc-template/tensor.html#toc_49
我不熟悉lua。 但是文章的作者使用了lua.
你能帮我理解这两行的作用吗:
做什么
replicate(x,batch_size)
干什么?
x = x:resize(x:size(1), 1):expand(x:size(1), batch_size)
是做什么的?
原始源代码可以在这里找到 https://github.com/wojzaremba/lstm/blob/master/data.lua
这基本上可以归结为简单的数学运算并在 torch 手册中查找一些函数。
好吧我很无聊所以...
replicate(x,batch_size)
定义在 https://github.com/wojzaremba/lstm/blob/master/data.lua
-- Stacks replicated, shifted versions of x_inp
-- into a single matrix of size x_inp:size(1) x batch_size.
local function replicate(x_inp, batch_size)
local s = x_inp:size(1)
local x = torch.zeros(torch.floor(s / batch_size), batch_size)
for i = 1, batch_size do
local start = torch.round((i - 1) * s / batch_size) + 1
local finish = start + x:size(1) - 1
x:sub(1, x:size(1), i, i):copy(x_inp:sub(start, finish))
end
return x
end
此代码使用 Torch 框架。
x_inp:size(1)
returns Torch tensor(潜在的多维矩阵)的第 1 维大小 x_inp
.
见https://cornebise.com/torch-doc-template/tensor.html#toc_18
所以 x_inp:size(1)
给出了 x_inp
中的行数。 x_inp:size(2)
,会给你列数...
local x = torch.zeros(torch.floor(s / batch_size), batch_size)
创建一个新的用零填充的二维张量并创建一个本地引用,命名为 x
行数是根据 s
、x_inp
的行数和 batch_size
计算得出的。因此,对于您的示例输入,结果是 floor(11/2) = floor(5.5) = 5
.
您的示例中的列数是 2,因为 batch_size
是 2。
手电筒。
所以简单地说x
就是5x2矩阵
0 0
0 0
0 0
0 0
0 0
以下行将 x_inp
的内容复制到 x
。
for i = 1, batch_size do
local start = torch.round((i - 1) * s / batch_size) + 1
local finish = start + x:size(1) - 1
x:sub(1, x:size(1), i, i):copy(x_inp:sub(start, finish))
end
在第一个运行中,start
计算为1,finish
计算为5
,因为x:size(1)
当然是[的行数=27=] 即 5
。 1+5-1=5
在第二个 运行 中,start
计算为 6,finish
计算为 10
因此 x_inp
的前 5 行(您的第一批)被复制到 x
的第一列,第二批被复制到 x
的第二列
x:sub(1, x:size(1), i, i)
是 x
的子张量,第 1 到 5 行,第 1 到 1 列和第二个 运行 的第 1 到 5 行,第 2 到 2 列(在你的例子中)。所以无非就是x
见https://cornebise.com/torch-doc-template/tensor.html#toc_42
:copy(x_inp:sub(start, finish))
将 x_inp
中的元素复制到 x
的列中。
总而言之,您获取一个输入张量并将其分成多个批次,这些批次存储在一个张量中,每个批次有一列。
所以 x_inp
0
1
2
3
4
5
6
7
8
9
10
和batch_size = 2
x
是
0 5
1 6
2 7
3 8
4 9
进一步:
local function testdataset(batch_size)
local x = load_data(ptb_path .. "ptb.test.txt")
x = x:resize(x:size(1), 1):expand(x:size(1), batch_size)
return x
end
是另一个从文件中加载一些数据的函数。这个 x
与上面的 x
无关,只是两者都是张量。
让我们用一个简单的例子:
x
正在
1
2
3
4
和batch_size = 4
x = x:resize(x:size(1), 1):expand(x:size(1), batch_size)
首先x
将被调整为4x1,阅读https://cornebise.com/torch-doc-template/tensor.html#toc_36
然后通过复制第一行 3 次将其扩展为 4x4。
导致 x
成为张量
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
阅读https://cornebise.com/torch-doc-template/tensor.html#toc_49