def test(self): t = np.linspace(-10, 10, num=100) target = np.vectorize(self.func1)(t) wn = Wavenet(Morlet, 30, t, target) wn.smart_train(t, target, maxiter=500) plb.plot(t, target, label='Сигнал') plb.plot(t, wn.sim(t), label='Аппроксимация') plb.show()
class WaveVAE(nn.Module): def __init__(self): super(WaveVAE, self).__init__() self.encoder = Wavenet(out_channels=2, num_blocks=2, num_layers=10, residual_channels=128, gate_channels=256, skip_channels=128, kernel_size=2, cin_channels=80, upsample_scales=[16, 16]) self.decoder = Wavenet_Student(num_blocks_student=[1, 1, 1, 1, 1, 1], num_layers=10) self.log_eps = nn.Parameter(torch.zeros(1)) def forward(self, x, c): # Encode mu_logs = self.encoder(x, c) mu = mu_logs[:, 0:1, :-1] logs = mu_logs[:, 1:, :-1] q_0 = Normal(mu.new_zeros(mu.size()), mu.new_ones(mu.size())) mean_q = (x[:, :, 1:] - mu) * torch.exp(-logs) # Reparameterization, Sampling from prior z = q_0.sample() * torch.exp(self.log_eps) + mean_q z_prior = q_0.sample() z = F.pad(z, pad=(1, 0), mode='constant', value=0) z_prior = F.pad(z_prior, pad=(1, 0), mode='constant', value=0) c_up = self.encoder.upsample(c) # Decode # x_rec : [B, 1, T] (first time step zero-padded) # mu_tot, logs_tot : [B, 1, T-1] x_rec, mu_p, log_p = self.decoder(z, c_up) x_prior = self.decoder.generate(z_prior, c_up) loss_recon = -0.5 * (-log(2.0 * pi) - 2. * log_p - torch.pow(x[:, :, 1:] - mu_p, 2) * torch.exp( (-2.0 * log_p))) loss_kl = 0.5 * (mean_q**2 + torch.exp(self.log_eps)**2 - 1) - self.log_eps return x_rec, x_prior, loss_recon.mean(), loss_kl.mean() def generate(self, z, c): c_up = self.encoder.upsample(c) x_sample = self.decoder.generate(z, c_up) return x_sample
def build_model(): model = Wavenet(out_channels=2, num_blocks=args.num_blocks, num_layers=args.num_layers, residual_channels=args.residual_channels, gate_channels=args.gate_channels, skip_channels=args.skip_channels, kernel_size=args.kernel_size, cin_channels=args.cin_channels, upsample_scales=[16, 16]) print(model) n_params = sum(p.numel() for p in model.parameters() if p.requires_grad) print('number of teacher parameters:', n_params) return model
def __init__(self): super(WaveVAE, self).__init__() self.encoder = Wavenet(out_channels=2, num_blocks=2, num_layers=10, residual_channels=128, gate_channels=256, skip_channels=128, kernel_size=2, cin_channels=80, upsample_scales=[16, 16]) self.decoder = Wavenet_Student(num_blocks_student=[1, 1, 1, 1, 1, 1], num_layers=10) self.log_eps = nn.Parameter(torch.zeros(1))
def __init__(self, q_channels, dil_width, res_width, skip_width, kernel_width, n_dilations, n_blocks, learning_rate, log): ''' Pre-processes audio, calls Wavenet to create the networks, creates the targets, prepares the loss calculation, optimizer and weights saver Args: q_channels: number of quantization levels dil_width: width of dilation convolutional filters res_width: width of residual convolutional filters skip_width: width of skip channels for the softmax output kernel_width: filter with, first dimension of of kernel and gate dilation component n_dilations: Number of dilations to be used in every block n_blocks: Number of blocks in the network learning_rate: Learning rate to be used log: callback to log method ''' self.q_channels = q_channels self.dilations = n_blocks * [2**x for x in range(n_dilations)] self.receptive_field = (kernel_width - 1) * sum( self.dilations) + kernel_width net = Wavenet(dilations=self.dilations, kernel_width=kernel_width, dilation_width=dil_width, residual_width=res_width, skip_width=skip_width, q_channels=self.q_channels, receptive_field=self.receptive_field, log=log) input_batch = tf.placeholder(dtype=tf.float32, shape=None) one_hot = one_hot_mu_law_encode(input_batch, self.q_channels) log('Constructed wavenet.') raw_output = net.construct_network(one_hot) target_output = construct_target_output(one_hot, self.q_channels, self.receptive_field) log('Constructed targets.') loss = tf.nn.softmax_cross_entropy_with_logits(logits=tf.reshape( raw_output, [-1, self.q_channels]), labels=target_output) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) log('Created optimizer.') trainable = tf.trainable_variables() self.loss = tf.reduce_mean(loss) self.optim = optimizer.minimize(self.loss, var_list=trainable) self.saver = tf.train.Saver(var_list=trainable) self.input_batch = input_batch self.net = net
def build_model(): model_t = Wavenet(out_channels=2, num_blocks=args.num_blocks_t, num_layers=args.num_layers_t, residual_channels=args.residual_channels, gate_channels=args.gate_channels, skip_channels=args.skip_channels, kernel_size=args.kernel_size, cin_channels=args.cin_channels, upsample_scales=[16, 16]) return model_t
def build_model(): #build wavenet object with arguments set in parser model = Wavenet(out_channels=2, num_blocks=args.num_blocks, num_layers=args.num_layers, residual_channels=args.residual_channels, gate_channels=args.gate_channels, skip_channels=args.skip_channels, kernel_size=args.kernel_size, cin_channels=args.cin_channels, upsample_scales=[16, 16]) return model
def init_models(self): """ instantiate the requested models and sent them to the device """ # traditionals if self.nn_arch == 'conv-trad': self.models = {'cnn': ConvNetTrad(self.n_classes, self.data_size)} elif self.nn_arch == 'conv-fstride': self.models = { 'cnn': ConvNetFstride4(self.n_classes, self.data_size) } elif self.nn_arch == 'conv-experimental': self.models = { 'cnn': ConvNetExperimental(self.n_classes, self.data_size) } # adversarials elif self.nn_arch == 'adv-experimental': self.models = { 'g': G_experimental(self.n_classes, self.data_size), 'd': D_experimental(self.n_classes, self.data_size) } elif self.nn_arch == 'adv-experimental3': self.models = { 'g': G_experimental(self.n_classes, self.data_size), 'd': D_experimental(self.n_classes, self.data_size, out_dim=1) } elif self.nn_arch == 'adv-collected-encoder': self.models = { 'g': G_experimental(self.n_classes, self.data_size, net_class='label-collect-encoder'), 'd': D_experimental(self.n_classes, self.data_size, net_class='label-collect-encoder') } # cnns elif self.nn_arch == 'conv-encoder': self.models = { 'cnn': ConvEncoderClassifierNet(self.n_classes, self.data_size, net_class='label-collect-encoder', fc_layer_type='fc1') } elif self.nn_arch == 'conv-encoder-stacked': self.models = { 'cnn': ConvStackedEncodersNet(self.n_classes, self.data_size, self.encoder_model) } elif self.nn_arch == 'conv-latent': self.models = { 'cnn': ConvLatentClassifier(self.n_classes, self.data_size) } # selected fc elif self.nn_arch == 'conv-encoder-fc1': self.models = { 'cnn': ConvEncoderClassifierNet(self.n_classes, self.data_size, net_class='lim-encoder-6', fc_layer_type='fc1') } elif self.nn_arch == 'conv-encoder-fc3': self.models = { 'cnn': ConvEncoderClassifierNet(self.n_classes, self.data_size, net_class='lim-encoder-6', fc_layer_type='fc3') } # limited encoders adv elif self.nn_arch == 'adv-lim-encoder': self.models = { 'g': G_experimental(self.n_classes, self.data_size, net_class='lim-encoder'), 'd': D_experimental(self.n_classes, self.data_size, net_class='lim-encoder') } elif self.nn_arch == 'adv-lim-encoder-6': self.models = { 'g': G_experimental(self.n_classes, self.data_size, net_class='lim-encoder-6'), 'd': D_experimental(self.n_classes, self.data_size, net_class='lim-encoder-6') } # limited encoder conv elif self.nn_arch == 'conv-lim-encoder': self.models = { 'cnn': ConvEncoderClassifierNet(self.n_classes, self.data_size, net_class='lim-encoder') } # wavenet elif self.nn_arch == 'wavenet': self.models = {'wav': Wavenet(self.n_classes)} # not found else: print("***Network Architecture not found!") # send models to device self.models = dict( (k, model.to(self.device)) for k, model in self.models.items())
transform=transform) validation_set = Testset(np.array([2]), 'ccmixter3/', pad=field, dilations1=dilations1, device=device) loadtr = data.DataLoader(training_set, batch_size=1, shuffle=True, num_workers=0, worker_init_fn=np.random.seed) loadval = data.DataLoader(validation_set, batch_size=1, num_workers=0) # In[6]: #model = Unet(skipDim, quantization_channels, residualDim,device) model = Wavenet(field, skipDim, residualDim, dilations0, dilations1) #model = nn.DataParallel(model) model = model.cuda() criterion = nn.CrossEntropyLoss() # in wavenet paper, they said crossentropyloss is far better than MSELoss optimizer = optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-5) # use adam to train maxloss = np.zeros(50) + 100 # In[7]: start_epoch = 0 if continueTrain: # if continueTrain, the program will find the checkpoints if os.path.isfile(resumefile): print("=> loading checkpoint '{}'".format(resumefile)) checkpoint = torch.load(resumefile)
batch_loss = 0 print('--Training finished') if __name__ == '__main__': """ main """ # input [num x batch x channel x time] x_train = torch.randn(1, 1, 1, 16000) y_train = torch.randint(0, 2, (1, 1, 16000)) class_dict = {'a':0, 'b':1} # norm x_train = x_train / torch.max(torch.abs(x_train)) # wavenet wavenet = Wavenet() print("wavenet: ", wavenet) # wavenet training wavenet_training(wavenet, x_train, y_train, class_dict, num_epochs=10, lr=0.001) # count params layer_params = wavenet.count_params() print("layer: {} sum: {}".format(layer_params, sum(layer_params)))
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {} # In[5]: params = {'batch_size': 1, 'shuffle': True, 'num_workers': 1} training_set = Dataset(np.arange(0, songnum), np.arange(0, songnum), 'ccmixter2/x/', 'ccmixter2/y/') validation_set = Dataset(np.arange(0, songnum), np.arange(0, songnum), 'ccmixter2/x/', 'ccmixter2/y/') loadtr = data.DataLoader(training_set, **params) # pytorch dataloader, more faster than mine loadval = data.DataLoader(validation_set, **params) # In[6]: model = Wavenet(pad, skipDim, quantization_channels, residualDim, dilations).cuda() criterion = nn.CrossEntropyLoss() # in wavenet paper, they said crossentropyloss is far better than MSELoss # optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum) optimizer = optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-5) # use adam to train # optimizer = optim.SGD(model.parameters(), lr = 0.1, momentum=0.9, weight_decay=1e-5) # scheduler = StepLR(optimizer, step_size=30, gamma=0.1) # scheduler = MultiStepLR(optimizer, milestones=[20,40], gamma=0.1) # In[7]: if continueTrain: # if continueTrain, the program will find the checkpoints if os.path.isfile(resumefile): print("=> loading checkpoint '{}'".format(resumefile)) checkpoint = torch.load(resumefile)