def forward(self, batch): # pylint:disable=arguments-differ ''' A batch of inputs and targets ''' encoded, encoder_attn_weights_tensor = self.encode(batch['inputs']) decoded = self.decode( encoded, right_shift(right_shift(batch['targets']), shift=self.span - 1, fill=self.sos_idx), ) logits = decoded['logits'] dims = list(range(1, logits.dim())) targets = left_shift(batch['targets']) nll = self.cross_entropy(logits, targets).sum(dims[:-1]) smoothed_nll = self.label_smoothing(logits, targets).sum(dims) return { 'smoothed_nll': smoothed_nll, 'nll': nll, 'encoder_attn_weights_tensor': encoder_attn_weights_tensor, 'decoder_attn_weights_tensor': decoded['decoder_attn_weights_tensor'], 'enc_dec_attn_weights_tensor': decoded['enc_dec_attn_weights_tensor'] }
def forward(self, batch): # pylint:disable=arguments-differ ''' A batch of inputs and targets ''' decoded = self.decode(self.encode(batch['inputs']), right_shift(batch['targets']), input_lens=batch['input_lens']) logits = decoded['logits'] dims = list(range(1, logits.dim())) targets = left_shift(batch['targets']) nll = self.cross_entropy(logits, targets).sum(dims[:-1]) smoothed_nll = self.label_smoothing(logits, targets).sum(dims) return smoothed_nll, nll
def forward(self, batch, global_mask=None): # pylint:disable=arguments-differ ''' batch: length x bsz''' batch = batch.transpose(1, 0) targets = left_shift(batch) decoded = self.decode(right_shift(batch), global_mask=global_mask) state = decoded['state'] if not self.adaptive: logits = self.embedding(state, reverse=True).transpose(2, 1) dims = list(range(1, logits.dim())) nll = self.cross_entropy(logits, targets).view(-1) smoothed_nll = self.label_smoothing(logits, targets).sum(dims) if not self.config.return_rank: return smoothed_nll, nll else: logits = logits.transpose(2, 1) assert targets.shape[0] == 1 targets = targets.squeeze(0) target_logits = logits[:, range(targets.shape[0]), targets] rank = (logits > target_logits.unsqueeze(-1)).sum(dim=-1) return rank, nll else: if self.config.batch_length < state.size(1): state = state[:, -self.config.batch_length:].contiguous() targets = targets[:, -self.config.batch_length:].contiguous() state = state.view(-1, state.shape[-1]) # (bsz*L, embed_dim) targets = targets.contiguous().view(-1) # (bsz*L, ) if not self.config.return_rank: nll = self.adaptive_softmax(state, targets, keep_order=True) smoothed_nll = nll return smoothed_nll, nll else: nll, rank = self.adaptive_softmax(state, targets, keep_order=True, return_rank=True) return rank, nll
def forward(self, batch): # pylint:disable=arguments-differ ''' A batch of inputs and targets ''' encoded = self.encode(batch['inputs']) decoded_annotation = self.decode_annotation( encoded, right_shift(batch['target_annotations'])) logits = decoded_annotation['logits'] loss = nll = self.annotation_cross_entropy( logits, left_shift(batch['target_annotations'])).sum( list(range(1, logits.dim() - 1))) if self.decoders is not None: decoded = self.decode(encoded, batch['masked_targets']) logits = decoded['logits'] nll += self.cross_entropy(logits, batch['targets']).sum( list(range(1, logits.dim() - 1))) loss += self.label_smoothing(logits, batch['targets']).sum( list(range(1, logits.dim()))) return loss, nll
def start(): # plain text with open('input.txt', 'rb') as f: plain = f.read() plain_splitted = utils.string_to_array(plain, 8) plain_binary_splitted = [] for p in plain_splitted: plain_binary_splitted.append(utils.string_to_binary(p)) print '' utils.debug('plain text', [plain]) utils.debug('splitted', plain_splitted) utils.debug('plain binary', plain_binary_splitted) # key key = '12345678' key_binary = utils.string_to_binary(key) print '' utils.debug('key', key) utils.debug('key binary', key_binary, 8) # generate C, D cd0 = permute(key_binary, tables.PC1) c = [cd0[:len(tables.PC1) / 2]] d = [cd0[len(tables.PC1) / 2:]] print '' utils.debug('CD0', cd0, 7) for i in range(16): c.append(utils.left_shift(c[i], tables.LEFT_SHIFT[i])) d.append(utils.left_shift(d[i], tables.LEFT_SHIFT[i])) utils.debug('CD' + str(i + 1), c[i + 1] + d[i + 1], 7) # generate K print '' k = [''] for i in range(16): k.append(permute(c[i + 1] + d[i + 1], tables.PC2)) utils.debug('K' + str(i + 1), k[i + 1], 6) final_cipher = '' for i in range(len(plain_splitted)): # generate L, R lr0 = permute(plain_binary_splitted[i], tables.IP) l = [lr0[:len(tables.IP) / 2]] r = [lr0[len(tables.IP) / 2:]] print '' utils.debug('L0', l[0], 8) utils.debug('R0', r[0], 8) # --- er = [] a = [''] b = [''] pb = [''] for i in range(16): er.append(permute(r[i], tables.EXPANSION)) a.append(utils.xor(er[i], k[i + 1])) b.append(sbox(a[i + 1])) pb.append(permute(b[i + 1], tables.PBOX)) r.append(utils.xor(l[i], pb[i + 1])) l.append(r[i]) print '' utils.debug('ER' + str(i), er[i], 6) utils.debug('A' + str(i + 1), a[i], 6) utils.debug('B' + str(i + 1), b[i], 4) utils.debug('PB' + str(i + 1), pb[i], 8) utils.debug('R' + str(i + 1), r[i + 1], 8) utils.debug('L' + str(i + 1), l[i + 1], 8) # cipher cipher_binary = permute(r[16] + l[16], tables.IP_INV) cipher_binary_splitted = utils.string_to_array(cipher_binary, 8) cipher = '' for i in range(len(cipher_binary_splitted)): cipher += utils.binary_to_hex(cipher_binary_splitted[i]) print '' utils.debug('cipher binary', cipher_binary, 8) utils.debug('cipher', cipher) final_cipher += cipher utils.debug('final chiper', final_cipher) with open('output.txt', 'wb') as f: f.write(final_cipher)