Lua + Torch 踩坑小结

2 篇文章 0 订阅
2 篇文章 0 订阅

1.Tensor中选择元素时索引必须用number类型,tensor与table转换

index53[torch.FloatTensor of size 2]p0.5162  0.4855  0.5177  0.5186  0.50070.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结果:NEW0.50070.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中的pindex可以分别作为inputtarget。

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).



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值