delete LCAE, adjust training
This commit is contained in:
64
models/ae.py
64
models/ae.py
@ -3,7 +3,7 @@ import torch.nn as nn
|
||||
import torch.functional as F
|
||||
|
||||
class AE(nn.Module):
|
||||
def __init__(self, in_dim=320):
|
||||
def __init__(self, in_dim=400):
|
||||
super(AE, self).__init__()
|
||||
self.net = nn.Sequential(
|
||||
nn.Linear(in_dim, 64),
|
||||
@ -16,69 +16,13 @@ class AE(nn.Module):
|
||||
nn.ReLU(),
|
||||
nn.Linear(64, 64),
|
||||
nn.ReLU(),
|
||||
nn.Linear(64, 320),
|
||||
nn.Linear(64, in_dim),
|
||||
nn.ReLU(),
|
||||
)
|
||||
|
||||
def forward(self, data):
|
||||
return self.net(data)
|
||||
|
||||
def init_weights(self):
|
||||
def _weight_init(m):
|
||||
if hasattr(m, 'weight'):
|
||||
if isinstance(m.weight, torch.Tensor):
|
||||
torch.nn.init.xavier_uniform_(m.weight,
|
||||
gain=nn.init.calculate_gain('relu'))
|
||||
if hasattr(m, 'bias'):
|
||||
if isinstance(m.bias, torch.Tensor):
|
||||
m.bias.data.fill_(0.01)
|
||||
|
||||
self.apply(_weight_init)
|
||||
|
||||
|
||||
|
||||
class LCAE(nn.Module):
|
||||
def __init__(self, in_dim=320):
|
||||
super(LCAE, self).__init__()
|
||||
num_mem = 10
|
||||
mem_size= 8
|
||||
self.num_mem = num_mem
|
||||
self.encode = nn.Sequential(
|
||||
nn.Linear(in_dim, 64),
|
||||
nn.ReLU(),
|
||||
nn.Linear(64, 64),
|
||||
nn.ReLU(),
|
||||
nn.Linear(64, num_mem),
|
||||
nn.Softmax(-1)
|
||||
)
|
||||
|
||||
self.decode = nn.Sequential(
|
||||
nn.Linear(mem_size, 64),
|
||||
nn.ReLU(),
|
||||
nn.Linear(64, 64),
|
||||
nn.ReLU(),
|
||||
nn.Linear(64, 320),
|
||||
nn.ReLU(),
|
||||
)
|
||||
|
||||
self.M = nn.Parameter(
|
||||
torch.randn(num_mem, mem_size)
|
||||
)
|
||||
|
||||
def forward(self, data):
|
||||
alphas = self.encode(data).unsqueeze(-1)
|
||||
entropy_alphas = (alphas * -alphas.log()).sum(1)
|
||||
M = self.M.expand(data.shape[0], *self.M.shape)
|
||||
#print(M.shape, alphas.shape) # torch.Size([128, 4, 8]) torch.Size([128, 4, 1])
|
||||
elu = nn.ELU()
|
||||
weighted = alphas * (1+elu(M+1e-13))
|
||||
#print(weighted.shape)
|
||||
summed = weighted.sum(1)
|
||||
#print(summed.shape)
|
||||
decoded = self.decode(summed)
|
||||
diversity = (alphas.sum(dim=0)/data.shape[0]).max()
|
||||
#print(alphas[0])
|
||||
return decoded, entropy_alphas, diversity
|
||||
x = data.view(data.shape[0], -1)
|
||||
return self.net(x), x
|
||||
|
||||
def init_weights(self):
|
||||
def _weight_init(m):
|
||||
|
@ -21,8 +21,8 @@ class Subspectrogram(object):
|
||||
|
||||
if __name__ == '__main__':
|
||||
import numpy as np
|
||||
sub_spec_tnfm = Subspectrogram(20, 10)
|
||||
X = np.random.rand(1, 60, 40)
|
||||
sub_spec_tnfm = Subspectrogram(20, 20)
|
||||
X = np.random.rand(1, 80, 40)
|
||||
Y = sub_spec_tnfm(X)
|
||||
print(f'\t Sub-Spectrogram transformation from shape {X.shape} to {Y.shape}')
|
||||
print('Done ...')
|
||||
print(f'\tSub-Spectrogram transformation from shape {X.shape} to {Y.shape}')
|
||||
print('\tDone ...')
|
Reference in New Issue
Block a user