【发布时间】:2021-03-07 11:38:41
【问题描述】:
我有两种方法:一种将 4D 矩阵(张量)转换为矩阵,另一种将 2D 矩阵转换为 4D。
从 4D 重塑到 2D 效果很好,但是当我再次尝试在张量中重新转换时,我没有实现相同的元素顺序。方法有:
# Method to convert the tensor in a matrix
def tensor2matrix(tensor):
# rows, columns, channels and filters
r, c, ch, f = tensor[0].shape
new_dim = [r*c*ch, f] # Inferer the new matrix dims
# Transpose is necesary because the columns are the channels weights
# flattened in columns
return np.reshape(np.transpose(tensor[0], [2,0,1,3]), new_dim)
# Method to convert the matrix in a tensor
def matrix2tensor(matrix, fs):
return np.reshape(matrix, fs, order="F")
我认为问题出在np.transpose,因为只有当矩阵是我才能按行排列列...有没有办法在没有循环的情况下从矩阵中支持张量?
【问题讨论】:
标签: python matrix slice reshape tensor