我有一个相同形状的张量列表。
我想总结沿轴的整个张量列表。
torch.cumsum
是否在暗处执行此操作?
如果是这样,则需要将列表转换为单个张量并求和?
答案 0 :(得分:2)
您不需要sum
,stack
是您的朋友
是的,您首先应该根据需要使用cat
或import torch
my_list = [torch.randn(3, 5), torch.randn(3, 5)]
result = torch.stack(my_list, dim=0).sum(dim=0).sum(dim=0)
print(result.shape) #torch.Size([5])
将它们转换为单个张量,如下所示:
$databaseValues = [
[
'no' => 0,
'field_a' => 'username',
'field_b' => 'brian'
],
[
'no' => 1,
'field_a' => 'email',
'field_b' => 'adam@fcg.com'
],
[
'no' => 1,
'field_a' => 'username',
'field_b' => 'adam'
],
[
'no' => 0,
'field_a' => 'email',
'field_b' => 'brian@fcg.com'
]
];
$table = [];
foreach ($databaseValues as $row) {
['no' => $userId, 'field_a' => $columnName, 'field_b' => $columnValue] = $row;
if (!isset($table[$userId])) {
$table[$userId] = ['id' => $userId];
}
$table[$userId][$columnName] = $columnValue;
}
/* you can sort it; e.g. */
uasort($table, function($left, $right) {
return $left['username'] <=> $right['username'];
});
/* output for demonstration */
foreach ($table as $user) {
echo $user['username'] . PHP_EOL;
echo $user['email'] . PHP_EOL;
}