def __init__(self, output_dim, hidden_dim, output_length, depth=1,bidirectional=True, dropout=0.25, **kwargs): if bidirectional and hidden_dim % 2 != 0: raise Exception ("hidden_dim for AttentionSeq2seq should be even (Because of bidirectional RNN).") super(AttentionSeq2seq, self).__init__() if type(depth) not in [list, tuple]: depth = (depth, depth) if bidirectional: encoder = Bidirectional(LSTMEncoder(output_dim=hidden_dim / 2, state_input=False, return_sequences=True, **kwargs)) else: encoder = LSTMEncoder(output_dim=hidden_dim, state_input=False, return_sequences=True, **kwargs) decoder = AttentionDecoder(hidden_dim=hidden_dim, output_length=output_length, state_input=False, **kwargs) lstms = [] for i in range(1, depth[0]): if bidirectional: layer = Bidirectional(LSTMEncoder(output_dim=hidden_dim / 2, state_input=False, return_sequences=True, **kwargs)) else: layer = LSTMEncoder(output_dim=hidden_dim, state_input=False, return_sequences=True, **kwargs) self.add(layer) lstms.append(layer) self.add(Dropout(dropout)) self.add(encoder) self.add(Dropout(dropout)) self.add(TimeDistributedDense(hidden_dim if depth[1] > 1 else output_dim)) lstms.append(encoder) self.add(decoder) lstms = [decoder] for i in range(1, depth[1]): layer = LSTMEncoder(output_dim=hidden_dim, state_input=False, return_sequences=True, **kwargs) self.add(layer) lstms.append(layer) self.add(Dropout(dropout)) if depth[1] > 1: self.add(TimeDistributedDense(output_dim)) self.encoder = encoder self.decoder = decoder
def __init__(self, output_dim, hidden_dim, output_length, depth=1, broadcast_state=True, inner_broadcast_state=True, peek=False, dropout=0.1, additional_layer=None, **kwargs): super(Seq2seq, self).__init__() if type(depth) not in [list, tuple]: depth = (depth, depth) if 'batch_input_shape' in kwargs: shape = kwargs['batch_input_shape'] del kwargs['batch_input_shape'] elif 'input_shape' in kwargs: shape = (None,) + tuple(kwargs['input_shape']) del kwargs['input_shape'] elif 'input_dim' in kwargs: shape = (None, None, kwargs['input_dim']) del kwargs['input_dim'] lstms = [] if additional_layer is not None: self.add(additional_layer) layer = LSTMEncoder(batch_input_shape=shape, output_dim=hidden_dim, state_input=False, return_sequences=depth[0] > 1, **kwargs) self.add(layer) lstms += [layer] for i in range(depth[0] - 1): self.add(Dropout(dropout)) layer = LSTMEncoder(output_dim=hidden_dim, state_input=inner_broadcast_state, return_sequences=i < depth[0] - 2, **kwargs) self.add(layer) lstms += [layer] if inner_broadcast_state: for i in range(len(lstms) - 1): lstms[i].broadcast_state(lstms[i + 1]) encoder = self.layers[-1] self.add(Dropout(dropout)) decoder = LSTMDecoder(hidden_dim=hidden_dim, output_length=output_length, state_input=broadcast_state, **kwargs) self.add(decoder) lstms = [decoder] for i in range(depth[1] - 1): self.add(Dropout(dropout)) layer = LSTMEncoder( output_dim=hidden_dim, state_input=inner_broadcast_state, return_sequences=True, **kwargs) self.add(layer) lstms += [layer] self.add(Dropout(dropout)) if inner_broadcast_state: for i in range(len(lstms) - 1): lstms[i].broadcast_state(lstms[i + 1]) if broadcast_state: encoder.broadcast_state(decoder) self.add(Dropout(dropout)) self.add(TimeDistributed(Dense(output_dim, activation='softmax'))) self.encoder = encoder self.decoder = decoder
def __init__(self, output_dim, hidden_dim, output_length, depth=1, broadcast_state=True, inner_broadcast_state=True, peek=False, dropout=0.25, **kwargs): super(Seq2seq, self).__init__() layers = [] if type(depth) not in [list, tuple]: depth = (depth, depth) broadcast = (depth[0] > 1 and inner_broadcast_state) or broadcast_state encoder = LSTMEncoder(output_dim=hidden_dim, state_input=broadcast, **kwargs) if peek: decoder = LSTMDecoder2(hidden_dim=hidden_dim, output_length=output_length, state_input=encoder if broadcast else False, **kwargs) else: decoder = LSTMDecoder(hidden_dim=hidden_dim, output_length=output_length, state_input=encoder if broadcast else False, **kwargs) lstms = [] for i in range(1, depth[0]): layer = LSTMEncoder(output_dim=hidden_dim, state_input=inner_broadcast_state and (i != 1), return_sequences=True, **kwargs) layers.append(layer) lstms.append(layer) layers.append(Dropout(dropout)) layers.append(encoder) layers.append(Dropout(dropout)) layers.append(Dense(hidden_dim if depth[1] > 1 else output_dim)) lstms.append(encoder) if inner_broadcast_state: for i in range(len(lstms) - 1): lstms[i].broadcast_state(lstms[i + 1]) layers.append(decoder) if broadcast_state: encoder.broadcast_state(decoder) lstms = [decoder] for i in range(1, depth[1]): layer = LSTMEncoder(output_dim=hidden_dim, state_input=inner_broadcast_state and (i != 1), return_sequences=True, **kwargs) layers.append(layer) lstms.append(layer) layers.append(Dropout(dropout)) if inner_broadcast_state: for i in range(len(lstms) - 1): lstms[i].broadcast_state(lstms[i + 1]) if depth[1] > 1: layers.append(TimeDistributedDense(output_dim)) self.encoder = encoder self.decoder = decoder for l in layers: self.add(l) if depth[0] > 1: self.layers[0].build()
def __init__(self, output_dim, hidden_dim, output_length, depth=1,bidirectional=True, dropout=0.1, **kwargs): if bidirectional and hidden_dim % 2 != 0: raise Exception ("hidden_dim for AttentionSeq2seq should be even (Because of bidirectional RNN).") super(AttentionSeq2seq, self).__init__() if type(depth) not in [list, tuple]: depth = (depth, depth) if 'batch_input_shape' in kwargs: shape = kwargs['batch_input_shape'] del kwargs['batch_input_shape'] elif 'input_shape' in kwargs: shape = (None,) + tuple(kwargs['input_shape']) del kwargs['input_shape'] elif 'input_dim' in kwargs: if 'input_length' in kwargs: input_length = kwargs['input_length'] else: input_length = None shape = (None, input_length, kwargs['input_dim']) del kwargs['input_dim'] self.add(Layer(batch_input_shape=shape)) if bidirectional: self.add(Bidirectional(LSTMEncoder(output_dim=int(hidden_dim / 2), state_input=False, return_sequences=True, **kwargs))) else: self.add(LSTMEncoder(output_dim=hidden_dim, state_input=False, return_sequences=True, **kwargs)) for i in range(0, depth[0] - 1): self.add(Dropout(dropout)) if bidirectional: self.add(Bidirectional(LSTMEncoder(output_dim=int(hidden_dim / 2), state_input=False, return_sequences=True, **kwargs))) else: self.add(LSTMEncoder(output_dim=hidden_dim, state_input=False, return_sequences=True, **kwargs)) encoder = self.layers[-1] self.add(Dropout(dropout)) self.add(TimeDistributed(Dense(hidden_dim if depth[1] > 1 else output_dim))) decoder = AttentionDecoder(hidden_dim=hidden_dim, output_length=output_length, state_input=False, **kwargs) self.add(Dropout(dropout)) self.add(decoder) for i in range(0, depth[1] - 1): self.add(Dropout(dropout)) self.add(LSTMEncoder(output_dim=hidden_dim, state_input=False, return_sequences=True, **kwargs)) self.add(Dropout(dropout)) self.add(TimeDistributed(Dense(output_dim))) self.encoder = encoder self.decoder = decoder
def __init__(self, output_dim, hidden_dim, output_length, depth=1, broadcast_state=True, inner_broadcast_state=True, peek=False, dropout=0.25, **kwargs): super(Seq2seq, self).__init__() layers= [] if type(depth) not in [list, tuple]: depth = (depth, depth) broadcast = (depth[0] > 1 and inner_broadcast_state) or broadcast_state encoder = LSTMEncoder(output_dim=hidden_dim, state_input=broadcast, **kwargs) if peek: decoder = LSTMDecoder2(hidden_dim=hidden_dim, output_length=output_length, state_input=encoder if broadcast else False, **kwargs) else: decoder = LSTMDecoder(hidden_dim=hidden_dim, output_length=output_length, state_input=encoder if broadcast else False, **kwargs) lstms = [] for i in range(1, depth[0]): layer = LSTMEncoder(output_dim=hidden_dim, state_input=inner_broadcast_state and (i != 1), return_sequences=True, **kwargs) layers.append(layer) lstms.append(layer) layers.append(Dropout(dropout)) layers.append(encoder) layers.append(Dropout(dropout)) layers.append(Dense(hidden_dim if depth[1] > 1 else output_dim)) lstms.append(encoder) if inner_broadcast_state: for i in range(len(lstms) - 1): lstms[i].broadcast_state(lstms[i + 1]) layers.append(decoder) if broadcast_state: encoder.broadcast_state(decoder) lstms = [decoder] for i in range(1, depth[1]): layer = LSTMEncoder(output_dim=hidden_dim, state_input=inner_broadcast_state and (i != 1), return_sequences=True, **kwargs) layers.append(layer) lstms.append(layer) layers.append(Dropout(dropout)) if inner_broadcast_state: for i in range(len(lstms) - 1): lstms[i].broadcast_state(lstms[i + 1]) if depth[1] > 1: layers.append(TimeDistributedDense(output_dim)) self.encoder = encoder self.decoder = decoder for l in layers: self.add(l) if depth[0] > 1: self.layers[0].build()