1.Tensor中选择元素时索引必须用number类型,tensor与table转换
index
5
3
[torch.FloatTensor of size 2]
p
0.5162 0.4855 0.5177 0.5186 0.5007
0.4797 0.4709 0.4959 0.4905 0.4959
[torch.FloatTensor of size 2x5]
想要根据tensor index取tensor p中的元素,组成一个新的Tensor NEW。
但是从index取出的数仍为一个tensor。
解决方法:将tensor->table,再从table中提取number数据。
做法如下:
index=torch.totable(index)
NEW=torch.tensor(2,1)
for i=1,2 do
NEW[{{i},{1}}]=p[{{i},{index[i]}}]
end
结果:
NEW
0.5007
0.4959
[torch.FloatTensor of size 2x1]
[table] torch.totable(object)
Converts a Tensor or a Storage to a lua table. Also available as methods: tensor:totable()
and storage:totable()
. Multidimensional Tensors are converted to a set of nested tables, matching the shape of the source Tensor.
> print(torch.totable(torch.Tensor({1, 2, 3})))
{
1 : 1
2 : 2
3 : 3
}
2.分类问题,criterion的forward
error 1
error 2
/Users/zh/torch/install/bin/luajit: /Users/zh/torch/install/share/lua/5.1/nn/THNN.lua:110: Assertion `cur_target >= 0 && cur_target < n_classes' failed. at /tmp/luarocks_nn-scm-1-2066/nn/lib/THNN/generic/ClassNLLCriterion.c:62
stack traceback:[C]: in function 'v'
/Users/zh/torch/install/share/lua/5.1/nn/THNN.lua:110: in function 'ClassNLLCriterion_updateOutput'
.../zh/torch/install/share/lua/5.1/nn/ClassNLLCriterion.lua:41: in function 'updateOutput'
.../zh/torch/install/share/lua/5.1/nn/ParallelCriterion.lua:23: in function 'forward'
该问题是指output的种类多于target。
解决方法:
以上两个error都是ClassNLLCriterion的input和target的设置(size)问题。
如下面的ClassNLLCriterion损失函数使用描述中写道:
假设有n个类别input
需是一个1维的size为n的张量,target是一个类别的索引,上文1中的p和index可以分别作为input
和target。
ClassNLLCriterion
criterion = nn.ClassNLLCriterion([weights])
The negative log likelihood criterion. It is useful to train a classication problem with n
classes. If provided, the optional argument weights
should be a 1D Tensor
assigning weight to each of the classes. This is particularly useful when you have an unbalanced training set.
The input
given through a forward()
is expected to contain log-probabilities of each class: input
has to be a 1D Tensor
of size n
. Obtaining log-probabilities in a neural network is easily achieved by adding a LogSoftMax
layer in the last layer of your neural network. You may use CrossEntropyCriterion
instead, if you prefer not to add an extra layer to your network. This criterion expects a class index (1 to the number of class) as target
when calling forward(input, target
) and backward(input, target)
.