File size: 1,597 Bytes
626eca0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
from typing import List
import numpy as np
import torch
def flatten(lsts: List[list]) -> list:
acc_lst = list()
for lst in lsts:
acc_lst.extend(lst)
return acc_lst
def batchify(tensors: List[torch.Tensor], padding_value: int = 0) -> torch.Tensor:
return torch.nn.utils.rnn.pad_sequence(
tensors, batch_first=True, padding_value=padding_value
)
def batchify_matrices(tensors: List[torch.Tensor], padding_value: int) -> torch.Tensor:
x = max([t.shape[0] for t in tensors])
y = max([t.shape[1] for t in tensors])
out_matrix = torch.zeros((len(tensors), x, y))
out_matrix += padding_value
for i, tensor in enumerate(tensors):
out_matrix[i][0 : tensor.shape[0], 0 : tensor.shape[1]] = tensor
return out_matrix
def batchify_tensor(tensors: List[torch.Tensor], padding_value: int) -> torch.Tensor:
x = max([t.shape[0] for t in tensors])
y = max([t.shape[1] for t in tensors])
rest = tensors[0].shape[2]
out_matrix = torch.zeros((len(tensors), x, y, rest))
out_matrix += padding_value
for i, tensor in enumerate(tensors):
out_matrix[i][0 : tensor.shape[0], 0 : tensor.shape[1], :] = tensor
return out_matrix
def chunks(lst: list, chunk_size: int) -> List[list]:
chunks_acc = list()
for i in range(0, len(lst), chunk_size):
chunks_acc.append(lst[i : i + chunk_size])
return chunks_acc
def add_noise_to_value(value: int, noise_param: float):
noise_value = value * noise_param
noise = np.random.uniform(-noise_value, noise_value)
return max(1, value + noise)
|