コード例 #1
0
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
コード例 #2
0
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