def test_new_bidirectional4(self): model = Sequential(self.s, model_table='new_table4') model.add(InputLayer()) model.add(Recurrent(n=10, name='rec1')) model.add(Bidirectional(n=20, src_layers=['rec1'])) model.add(OutputLayer()) model.print_summary()
def test_bidirectional_block3(self): try: Bidirectional(n=[10, 20, 30], n_blocks=2).compile() except DLPyError: pass except Exception as e: self.fail('Unexpected exception raised:', e) else: self.fail('ExpectedException not raised')
def SequenceLabeling(conn, model_table='sequence_labeling_model', neurons=10, n_blocks=3, rnn_type='gru'): ''' Generates a sequence labeling model. Parameters ---------- conn : CAS Specifies the CAS connection object. model_table : string, optional Specifies the name of CAS table to store the model. neurons : int, optional Specifies the number of neurons to be in each layer. Default: 10 n_blocks : int, optional Specifies the number of bidirectional blocks to be added to the model. Default: 3 rnn_type : string, optional Specifies the type of the rnn layer. Default: GRU Valid Values: RNN, LSTM, GRU Returns ------- :class:`Sequential` ''' conn.retrieve('loadactionset', _messagelevel='error', actionset='deeplearn') if n_blocks >= 1: model = Sequential(conn=conn, model_table=model_table) model.add( Bidirectional(n=neurons, n_blocks=n_blocks, rnn_type=rnn_type, name='bi_' + rnn_type + '_layer_')) model.add(OutputLayer()) else: raise DLPyError( 'The number of blocks for a sequence labeling model should be at least 1.' ) return model
def test_mix_cnn_rnn_network(self): from dlpy.applications import ResNet50_Caffe from dlpy import Sequential from dlpy.blocks import Bidirectional # the case is to test if CNN and RNN model can be connect using functional api # the model_type is expected to be RNN in 19w47. # CNN model = ResNet50_Caffe(self.s) cnn_head = model.to_functional_model(stop_layers = model.layers[-1]) # RNN model_rnn = Sequential(conn = self.s, model_table = 'rnn') model_rnn.add(Bidirectional(n = 100, n_blocks = 2)) model_rnn.add(OutputLayer('fixed')) f_rnn = model_rnn.to_functional_model() # connecting inp = Input(**cnn_head.layers[0].config) x = cnn_head(inp) y = f_rnn(x) cnn_rnn = Model(self.s, inp, y) cnn_rnn.compile() # check type self.assertTrue(cnn_rnn.model_type == 'RNN') self.assertTrue(cnn_rnn.layers[-1].name == 'fixed') self.assertTrue(x[0].shape == (1, 1, 2048)) f_rnn = model_rnn.to_functional_model() # connecting inp = Input(**cnn_head.layers[0].config) x = cnn_head(inp) y = f_rnn(x) cnn_rnn = Model(self.s, inp, y) cnn_rnn.compile() # it should be fixed if I create f_rnn again. self.assertTrue(cnn_rnn.layers[-1].name == 'fixed') inp = Input(**cnn_head.layers[0].config) x = cnn_head(inp) y = f_rnn(x) cnn_rnn = Model(self.s, inp, y) cnn_rnn.compile() # it should be fixed if I create f_rnn again. self.assertTrue(cnn_rnn.layers[-1].name == 'fixed_2')
def test_6(self): model1 = Sequential(self.s, model_table='table6') model1.add(Bidirectional(n=10, n_blocks=3)) model1.add(OutputLayer())
def test_new_bidirectional3(self): model = Sequential(self.s, model_table='new_table3') model.add(Bidirectional(n=[10, 20, 30], n_blocks=3)) model.add(OutputLayer()) model.print_summary()
def test_new_bidirectional1(self): model = Sequential(self.s, model_table='new_table1') model.add(Bidirectional(n=10)) model.add(OutputLayer()) model.print_summary()
def test_bidirectional_block4(self): list1 = Bidirectional(n=[10, 20, 30], n_blocks=3).compile() self.assertTrue(self.sample_syntax['bidirectional3'] == list1)
def test_bidirectional_block1(self): list1 = Bidirectional(n=30).compile() self.assertTrue(self.sample_syntax['bidirectional1'] == list1)
def TextGeneration(conn, model_table='text_generator', neurons=10, max_output_length=15, n_blocks=3, rnn_type='gru'): ''' Generates a text generation model. Parameters ---------- conn : CAS Specifies the CAS connection object. model_table : string, optional Specifies the name of CAS table to store the model. neurons : int, optional Specifies the number of neurons to be in each layer. Default: 10 n_blocks : int, optional Specifies the number of bidirectional blocks to be added to the model. Default: 3 max_output_length : int, optional Specifies the maximum number of tokens to generate Default: 15 rnn_type : string, optional Specifies the type of the rnn layer. Default: GRU Valid Values: RNN, LSTM, GRU Returns ------- :class:`Sequential` ''' conn.retrieve('loadactionset', _messagelevel='error', actionset='deeplearn') if n_blocks >= 3: model = Sequential(conn=conn, model_table=model_table) b = Bidirectional(n=neurons, name='bi_' + rnn_type + '_layer_', n_blocks=n_blocks - 2, rnn_type=rnn_type) model.add(b) b2 = Bidirectional(n=neurons, output_type='encoding', src_layers=b.get_last_layers(), rnn_type=rnn_type, name='bi_' + rnn_type + '_lastlayer') model.add(b2) model.add( Recurrent(n=neurons, output_type='arbitrarylength', src_layers=b2.get_last_layers(), rnn_type=rnn_type, max_output_length=max_output_length)) model.add(OutputLayer()) elif n_blocks >= 2: model = Sequential(conn=conn, model_table=model_table) b2 = Bidirectional(n=neurons, output_type='encoding', rnn_type=rnn_type, name='bi_' + rnn_type + '_layer_') model.add(b2) model.add( Recurrent(n=neurons, output_type='arbitrarylength', src_layers=b2.get_last_layers(), rnn_type=rnn_type, max_output_length=max_output_length)) model.add(OutputLayer()) else: raise DLPyError( 'The number of blocks for a text generation model should be at least 2.' ) return model
def TextClassification(conn, model_table='text_classifier', neurons=10, n_blocks=3, rnn_type='gru'): ''' Generates a text classification model Parameters ---------- conn : CAS Specifies the CAS connection object. model_table : string, optional Specifies the name of CAS table to store the model. neurons : int, optional Specifies the number of neurons to be in each layer. Default: 10 n_blocks : int, optional Specifies the number of bidirectional blocks to be added to the model. Default: 3 rnn_type : string, optional Specifies the type of the rnn layer. Default: GRU Valid Values: RNN, LSTM, GRU Returns ------- :class:`Sequential` ''' conn.retrieve('loadactionset', _messagelevel='error', actionset='deeplearn') if n_blocks >= 2: model = Sequential(conn=conn, model_table=model_table) b = Bidirectional(n=neurons, name='bi_' + rnn_type + '_layer_', n_blocks=n_blocks - 1, rnn_type=rnn_type) model.add(b) model.add( Bidirectional( n=neurons, output_type='encoding', src_layers=b.get_last_layers(), rnn_type=rnn_type, name='bi_' + rnn_type + '_lastlayer_', )) model.add(OutputLayer()) elif n_blocks == 1: model = Sequential(conn=conn, model_table=model_table) model.add( Bidirectional(n=neurons, output_type='encoding', rnn_type=rnn_type)) model.add(OutputLayer()) else: raise DLPyError( 'The number of blocks for a text classification model should be at least 1.' ) return model