Я предполагаю, что тензоры A
и C
имеют разное количество строк, но одинаковое количество столбцов.
A = torch.LongTensor([[0,1,2],[2,3,4]]) # torch.Size([2, 3])
C = torch.LongTensor([[5,6,7],[8,9,10],[11,12,13]]) # torch.Size([3, 3])
assert A.dim() == C.dim() == 2
assert A.size(1) == C.size(1)
shape_E = (A.size(0) + C.size(0), A.size(1))
E = torch.zeros(*shape_E).long()
# Two possible cases
if A.size(0) > C.size(0): # C has fewer rows
n_rows = C.size(0)
E[np.arange(0, 2*n_rows, 2), :] = A[:n_rows, :]
E[np.arange(1, 2*n_rows, 2), :] = C[:n_rows, :]
E[2*n_rows:, :] = A[n_rows:, :]
else: # A has fewer rows
n_rows = A.size(0)
E[np.arange(0, 2*n_rows, 2), :] = A[:n_rows, :]
E[np.arange(1, 2*n_rows, 2), :] = C[:n_rows, :]
E[2*n_rows:, :] = C[n_rows:, :]
print(E)
Вывод
tensor([[ 0, 1, 2],
[ 5, 6, 7],
[ 2, 3, 4],
[ 8, 9, 10],
[11, 12, 13]])