沿轴的张量列表上的火炬总和

时间:2019-03-14 10:17:48

标签: pytorch

我有一个相同形状的张量列表。 我想总结沿轴的整个张量列表。 torch.cumsum是否在暗处执行此操作? 如果是这样,则需要将列表转换为单个张量并求和?

1 个答案:

答案 0 :(得分:2)

您不需要sumstack是您的朋友 是的,您首先应该根据需要使用catimport 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;
}