Ho una lista che contiene 3 elementi in questa forma: tensor([[-0.1077, 0.1150, -0.1903,…
- User
Ho una lista che contiene 3 elementi in questa forma: tensor([[-0.1077, 0.1150, -0.1903, -0.2614, 0.1581, -0.3483, -0.2628, -0.2432, -0.1794, -0.1706, -0.3371, -0.2628, 0.0694, 0.3391, -0.1869, -0.1965, 0.2015, 0.2690, 0.1642, -0.1530, -0.1278, 0.2458, -0.3796, 0.3329, -0.0705, -0.0850, -0.0831, 0.2919, -0.3507, 0.1319, -0.2226, -0.1719, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [ 0.2720, 0.2756, -0.3262, 0.0220, -0.2522, -0.0503, -0.1751, 0.1430, 0.2648, -0.1255, 0.2972, -0.0547, 0.0511, 0.0769, 0.1366, -0.0577, -0.1939, 0.0429, -0.2158, 0.2011, -0.3197, 0.3130, -0.1897, 0.2176, -0.2984, 0.0098, -0.0970, 0.0437, 0.0829, -0.3190, 0.1023, 0.1874, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [ 0.2009, 0.2926, -0.0102, -0.0098, 0.1502, -0.0709, -0.2425, 0.2604, 0.3496, 0.2842, 0.2733, -0.0933, -0.1722, -0.0928, -0.2229, -0.1754, 0.0925, -0.3586, -0.1553, 0.1438, -0.2397, -0.1465, 0.2568, -0.2173, -0.1346, 0.0490, 0.3730, -0.1755, -0.0737, 0.3677, -0.0788, 0.3122, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [ 0.3647, 0.1531, -0.2991, 0.0021, 0.2795, 0.0178, -0.3591, 0.2967, 0.3025, 0.3235, -0.1361, -0.3557, -0.1767, 0.1784, 0.1265, 0.1758, 0.2146, -0.0591, -0.0087, -0.2784, 0.0203, -0.3091, -0.1163, 0.0439, 0.0360, 0.2007, 0.0333, -0.0996, 0.2421, -0.1108, -0.0248, 0.1773, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [ 0.0591, 0.1301, 0.0815, 0.0942, 0.3419, -0.3318, -0.3382, -0.2996, 0.0844, -0.2636, 0.0854, -0.3648, -0.2242, 0.2955, -0.2425, 0.2532, 0.1743, 0.0493, -0.2145, 0.1774, -0.0345, 0.0184, 0.2510, -0.0797, -0.2577, 0.1882, 0.3562, 0.1520, 0.0384, 0.0590, 0.0852, 0.1217, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [-0.2551, 0.2950, -0.0098, 0.1635, 0.1687, -0.1506, 0.1317, 0.1810, -0.0521, 0.1032, 0.2537, 0.1829, 0.1143, -0.2057, 0.0591, 0.3292, 0.2082, -0.0610, 0.2120, -0.3115, -0.3559, -0.2292, 0.3380, 0.2257, -0.2386, 0.2973, 0.2280, -0.2086, 0.0115, 0.1369, -0.0850, -0.0524, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [ 0.2648, 0.2977, 0.2827, 0.0522, -0.2934, 0.2213, 0.0060, 0.0292, -0.1239, 0.2256, 0.2987, -0.1243, -0.1582, -0.2665, -0.1884, 0.2786, 0.3060, -0.0119, -0.2420, 0.3112, 0.1790, 0.0231, 0.2671, -0.0325, 0.0086, 0.2818, 0.0026, 0.0175, -0.2057, -0.3169, -0.1018, 0.2280, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [ 0.0205, 0.3674, -0.2902, 0.0384, 0.0324, 0.0026, -0.0490, -0.2897, 0.3203, -0.2608, 0.2058, -0.3325, 0.2889, 0.2777, -0.0491, 0.2109, 0.2925, 0.1642, -0.1847, -0.0593, -0.0641, -0.3065, 0.0877, -0.0979, 0.1752, -0.0908, -0.0516, 0.2670, 0.0043, -0.2088, -0.0434, 0.2043, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [-0.0296, 0.0222, -0.1277, 0.3490, 0.3511, 0.0545, 0.3844, 0.1583, 0.2803, -0.2507, 0.2117, 0.0901, 0.1436, -0.0298, -0.0413, 0.2248, -0.2839, -0.3369, 0.0577, 0.1191, -0.0629, 0.2563, 0.2539, -0.2040, 0.1366, -0.0152, 0.1081, 0.1386, 0.1795, 0.1900, -0.0114, 0.2868, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],
- Assistant
To sum corresponding tensors from multiple elements in your list, you can use PyTorch's stacking and summing operations. Here's how you can do it:
import torch # Suppose 'lst' is your list containing the three tensors lst = [tensor1, tensor2, tensor3] # Stack the tensors along a new dimension (e.g., dimension 0) stacked_tensors = torch.stack(lst, dim=0) # Sum along the new dimension to get the element-wise sum summed_tensor = stacked_tensors.sum(dim=0)Alternatively, since the tensors in your list are of the same shape, you can use the built-in
sum()function directly:summed_tensor = sum(lst)This will add the tensors element-wise, summing the first tensor of the first element with the first tensor of the second and third elements, the second tensor with the second tensors, and so on.
Explanation:
torch.stack(lst, dim=0): This stacks your list of tensors along a new dimension (dimension 0 in this case), resulting in a new tensor of shape if your original tensors are of shape .