|
import torch |
|
import numpy as np |
|
import string |
|
import shutil |
|
import os |
|
|
|
|
|
def kwargify(**kwargs): |
|
return kwargs |
|
|
|
|
|
def show_lr(optimizer): |
|
lr = [] |
|
for param_group in optimizer.param_groups: |
|
lr += [param_group['lr']] |
|
|
|
return np.array(lr).mean() |
|
|
|
|
|
def contains_nan_or_inf(tensor): |
|
return torch.isnan(tensor).any() or torch.isinf(tensor).any() |
|
|
|
|
|
def map_phonemes(phonemes): |
|
new_phonemes = [] |
|
charset = string.printable.strip() |
|
|
|
for phoneme in phonemes: |
|
if phoneme == ' ': |
|
new_phonemes.append(phoneme) |
|
else: |
|
index = phonemes.index(phoneme) - 1 |
|
new_phonemes.append(charset[index]) |
|
|
|
return new_phonemes |
|
|
|
|
|
def empty_dir(directory): |
|
for filename in os.listdir(directory): |
|
file_path = os.path.join(directory, filename) |
|
|
|
try: |
|
if os.path.isfile(file_path): |
|
os.remove(file_path) |
|
elif os.path.islink(file_path): |
|
os.unlink(file_path) |
|
elif os.path.isdir(file_path): |
|
shutil.rmtree(file_path) |
|
except Exception as e: |
|
print('Failed to delete %s. Reason: %s' % (file_path, e)) |