| 22 | #----------------------------------------------------------------------------- |
| 23 | |
| 24 | class SequencePattern(object): |
| 25 | |
| 26 | INPUT_SEQUENCE_LENGTH = 10 |
| 27 | OUTPUT_SEQUENCE_LENGTH = 10 |
| 28 | INPUT_MAX_INT = 9 |
| 29 | OUTPUT_MAX_INT = 9 |
| 30 | PATTERN_NAME = "sorted" |
| 31 | |
| 32 | def __init__(self, name=None, in_seq_len=None, out_seq_len=None): |
| 33 | if name is not None: |
| 34 | assert hasattr(self, "%s_sequence" % name) |
| 35 | self.PATTERN_NAME = name |
| 36 | if in_seq_len: |
| 37 | self.INPUT_SEQUENCE_LENGTH = in_seq_len |
| 38 | if out_seq_len: |
| 39 | self.OUTPUT_SEQUENCE_LENGTH = out_seq_len |
| 40 | |
| 41 | def generate_output_sequence(self, x): |
| 42 | ''' |
| 43 | For a given input sequence, generate the output sequence. x is a 1D numpy array |
| 44 | of integers, with length INPUT_SEQUENCE_LENGTH. |
| 45 | |
| 46 | Returns a 1D numpy array of length OUTPUT_SEQUENCE_LENGTH |
| 47 | |
| 48 | This procedure defines the pattern which the seq2seq RNN will be trained to find. |
| 49 | ''' |
| 50 | return getattr(self, "%s_sequence" % self.PATTERN_NAME)(x) |
| 51 | |
| 52 | def maxmin_dup_sequence(self, x): |
| 53 | ''' |
| 54 | Generate sequence with [max, min, rest of original entries] |
| 55 | ''' |
| 56 | x = np.array(x) |
| 57 | y = [ x.max(), x.min()] + list(x[2:]) |
| 58 | return np.array(y)[:self.OUTPUT_SEQUENCE_LENGTH] # truncate at out seq len |
| 59 | |
| 60 | def sorted_sequence(self, x): |
| 61 | ''' |
| 62 | Generate sorted version of original sequence |
| 63 | ''' |
| 64 | ret = np.array( sorted(x) )[:self.OUTPUT_SEQUENCE_LENGTH] |
| 65 | return ret |
| 66 | |
| 67 | def reversed_sequence(self, x): |
| 68 | ''' |
| 69 | Generate reversed version of original sequence |
| 70 | ''' |
| 71 | return np.array( x[::-1] )[:self.OUTPUT_SEQUENCE_LENGTH] |
| 72 | |
| 73 | #----------------------------------------------------------------------------- |
| 74 | |