vertically stack tensors :param tensor_list: list of tensor to be stacked vertically :param pad: label to pad with :return: tensor with max shape
(tensor_list, pad=0)
| 158 | return im |
| 159 | |
| 160 | def tensor_vstack(tensor_list, pad=0): |
| 161 | """ |
| 162 | vertically stack tensors |
| 163 | :param tensor_list: list of tensor to be stacked vertically |
| 164 | :param pad: label to pad with |
| 165 | :return: tensor with max shape |
| 166 | """ |
| 167 | ndim = len(tensor_list[0].shape) |
| 168 | dtype = tensor_list[0].dtype |
| 169 | islice = tensor_list[0].shape[0] |
| 170 | dimensions = [] |
| 171 | first_dim = sum([tensor.shape[0] for tensor in tensor_list]) |
| 172 | dimensions.append(first_dim) |
| 173 | for dim in range(1, ndim): |
| 174 | dimensions.append(max([tensor.shape[dim] for tensor in tensor_list])) |
| 175 | if pad == 0: |
| 176 | all_tensor = np.zeros(tuple(dimensions), dtype=dtype) |
| 177 | elif pad == 1: |
| 178 | all_tensor = np.ones(tuple(dimensions), dtype=dtype) |
| 179 | else: |
| 180 | all_tensor = np.full(tuple(dimensions), pad, dtype=dtype) |
| 181 | if ndim == 1: |
| 182 | for ind, tensor in enumerate(tensor_list): |
| 183 | all_tensor[ind*islice:(ind+1)*islice] = tensor |
| 184 | elif ndim == 2: |
| 185 | for ind, tensor in enumerate(tensor_list): |
| 186 | all_tensor[ind*islice:(ind+1)*islice, :tensor.shape[1]] = tensor |
| 187 | elif ndim == 3: |
| 188 | for ind, tensor in enumerate(tensor_list): |
| 189 | all_tensor[ind*islice:(ind+1)*islice, :tensor.shape[1], :tensor.shape[2]] = tensor |
| 190 | elif ndim == 4: |
| 191 | for ind, tensor in enumerate(tensor_list): |
| 192 | all_tensor[ind*islice:(ind+1)*islice, :tensor.shape[1], :tensor.shape[2], :tensor.shape[3]] = tensor |
| 193 | else: |
| 194 | raise Exception('Sorry, unimplemented.') |
| 195 | return all_tensor |
no outgoing calls
no test coverage detected