def compile_test_fn(self): """ Build a testing routine for speech models. Returns: val_fn (theano.function): Function that takes in acoustic inputs, and calculates the loss. Returns network outputs and ctc cost """ logger.info("Building val_fn") f_inputs = [self.acoustic_input, self.ctc_in_lens] f_outputs = [] for branch in self.branch_outputs: labels, label_lens = self.branch_labels[branch.name] if K.backend() == 'tensorflow': network_output = branch.output ctc_cost = K.mean( K.ctc_batch_cost(labels, network_output, self.ctc_in_lens, label_lens)) else: network_output = branch.output.dimshuffle((1, 0, 2)) ctc_cost = ctc_th.gpu_ctc(network_output, labels, self.ctc_in_lens).mean() f_inputs.extend([labels, label_lens]) f_outputs.extend([network_output, ctc_cost]) f_inputs.append(K.learning_phase()) self.val_fn = K.function(f_inputs, f_outputs) return self.val_fn
def compile_train_fn(self, learning_rate=2e-4): """ Build the CTC training routine for speech models. Args: learning_rate (float) Returns: train_fn (theano.function): Function that takes in acoustic inputs, and updates the model. Returns network outputs and ctc cost """ logger.info("Building train_fn") f_inputs = [self.acoustic_input, self.ctc_in_lens] f_outputs = [] f_updates = [] for branch in self.branch_outputs: labels, label_lens = self.branch_labels[branch.name] f_inputs.append(labels) f_inputs.append(label_lens) if K.backend() == 'tensorflow': network_output = branch.output ctc_cost = K.mean( K.ctc_batch_cost(labels, network_output, self.ctc_in_lens, label_lens)) else: network_output = branch.output.dimshuffle((1, 0, 2)) ctc_cost = ctc_th.gpu_ctc(network_output, labels, self.ctc_in_lens).mean() f_outputs.extend([network_output, ctc_cost]) trainable_vars = self.branch_vars[branch.name] optmz = Adam(lr=learning_rate, clipnorm=100) f_updates.extend(optmz.get_updates(trainable_vars, [], ctc_cost)) f_inputs.append(K.learning_phase()) self.train_fn = K.function(f_inputs, f_outputs, f_updates) return self.train_fn
def setup_gpu_op(self, activations, labels, input_length, compute_grad=True): gpu_ctc_cost = gpu_ctc(activations, labels, input_length) outputs = [gpu_ctc_cost] if compute_grad: # Symbolic gradient of CTC cost gpu_ctc_grad = T.grad(T.mean(gpu_ctc_cost), activations) outputs += [gpu_ctc_grad] return theano.function([], outputs, mode=mode_with_gpu)
def setup_gpu_op(self, activations, labels, input_length, compute_grad=True): gpu_ctc_cost = gpu_ctc(activations, labels, input_length) outputs = [gpu_ctc_cost] if compute_grad: # Symbolic gradient of CTC cost gpu_ctc_grad = tt.grad(tt.mean(gpu_ctc_cost), activations) outputs += [gpu_ctc_grad] return theano.function([], outputs, mode=mode_with_gpu)
def check_grads_disabled(self, activations, labels, input_length): """ Check if optimization to disable gradients is working """ gpu_ctc_cost = gpu_ctc(activations, labels, input_length) gpu_ctc_function = theano.function([], [gpu_ctc_cost]) for node in gpu_ctc_function.maker.fgraph.apply_nodes: if isinstance(node.op, GpuConnectionistTemporalClassification): assert (node.op.compute_grad is False)
def check_grads_disabled(self, activations, labels, input_length): """ Check if optimization to disable gradients is working """ gpu_ctc_cost = gpu_ctc(activations, labels, input_length) gpu_ctc_function = theano.function([], [gpu_ctc_cost]) for node in gpu_ctc_function.maker.fgraph.apply_nodes: if isinstance(node.op, GpuConnectionistTemporalClassification): assert node.op.compute_grad is False
def wrapper(acts): # Create auxiliary symbolic variables t_activation_times = theano.shared(in_lengths, name="activation_times") t_labels = theano.shared(labels, name="labels") return gpu_ctc(acts, t_labels, t_activation_times)