Example #1
0
def create_network(args, text_reader, feature_tables, md=None):
    """Creates and returns the neural network according to the task at hand."""
    logger = logging.getLogger("Logger")
    
    if args.task.startswith('srl') and args.task != 'srl_predicates':
        num_tags = len(text_reader.tag_dict)
        distance_tables = utils.set_distance_features(md, args.max_dist, args.target_features,
                                                      args.pred_features)
        nn = ConvolutionalNetwork.create_new(feature_tables, distance_tables[0], 
                                             distance_tables[1], args.window, 
                                             args.convolution, args.hidden, num_tags)
        padding_left = text_reader.converter.get_padding_left(False)
        padding_right = text_reader.converter.get_padding_right(False)
        if args.identify:
            logger.info("Loading initial transition scores table for argument identification")
            transitions = srl.train_srl.init_transitions_simplified(text_reader.tag_dict)
            nn.transitions = transitions
            nn.learning_rate_trans = args.learning_rate_transitions
            
        elif not args.classify:
            logger.info("Loading initial IOB transition scores table")
            transitions = srl.train_srl.init_transitions(text_reader.tag_dict, 'iob')
            nn.transitions = transitions
            nn.learning_rate_trans = args.learning_rate_transitions
    
    elif args.task == 'lm':
        nn = LanguageModel.create_new(feature_tables, args.window, args.hidden)
        padding_left = text_reader.converter.get_padding_left(tokens_as_string=True)
        padding_right = text_reader.converter.get_padding_right(tokens_as_string=True)
        
    else:
        num_tags = len(text_reader.tag_dict)
        nn = Network.create_new(feature_tables, args.window, args.hidden, num_tags)
        if args.learning_rate_transitions > 0:
            transitions = np.zeros((num_tags + 1, num_tags), np.float)
            nn.transitions = transitions
            nn.learning_rate_trans = args.learning_rate_transitions

        padding_left = text_reader.converter.get_padding_left(args.task == 'pos')
        padding_right = text_reader.converter.get_padding_right(args.task == 'pos')
    
    nn.padding_left = np.array(padding_left)
    nn.padding_right = np.array(padding_right)
    nn.learning_rate = args.learning_rate
    nn.learning_rate_features = args.learning_rate_features
    
    if args.task == 'lm':
        layer_sizes = (nn.input_size, nn.hidden_size, 1)
    elif args.convolution > 0 and args.hidden > 0:
        layer_sizes = (nn.input_size, nn.hidden_size, nn.hidden2_size, nn.output_size)
    else:
        layer_sizes = (nn.input_size, nn.hidden_size, nn.output_size)
    
    logger.info("Created new network with the following layer sizes: %s"
                % ', '.join(str(x) for x in layer_sizes))
    
    return nn
Example #2
0
def create_network(args, text_reader, feature_tables, md):
    """Creates and returns the neural network according to the task at hand."""
    logger = logging.getLogger("Logger")
    
    is_dependency = 'dependency' in args.task
    is_convolution_srl =  args.task.startswith('srl') and args.task != 'srl_predicates'
    is_convolution = is_convolution_srl or is_dependency
    
    if is_convolution:
        # get some data structures used both by dep parsing and SRL
        distance_tables = utils.set_distance_features(args.max_dist, args.target_features,
                                                      args.pred_features)
        padding_left = text_reader.converter.get_padding_left(False)
        padding_right = text_reader.converter.get_padding_right(False)
    
        if is_dependency:
            output_size = 1 if not args.labeled else len(text_reader.tag_dict)
            nn = ConvolutionalDependencyNetwork.create_new(feature_tables, distance_tables[0], 
                                                           distance_tables[1], args.window, 
                                                           args.convolution, args.hidden, output_size)
        
        else:
            # not dependency (SRL)
            num_tags = len(text_reader.tag_dict)
            nn = ConvolutionalNetwork.create_new(feature_tables, distance_tables[0], 
                                                 distance_tables[1], args.window, 
                                                 args.convolution, args.hidden, num_tags)
            
            if args.identify:
                logger.info("Loading initial transition scores table for argument identification")
                transitions = srl.init_transitions_simplified(text_reader.tag_dict)
                nn.transitions = transitions
                
            elif not args.classify:
                logger.info("Loading initial IOB transition scores table")
                transitions = srl.init_transitions(text_reader.tag_dict, 'iob')
                nn.transitions = transitions
                
    else:
        # not convolution
        num_tags = len(text_reader.tag_dict)
        nn = Network.create_new(feature_tables, args.window, args.hidden, num_tags)
        transitions = np.zeros((num_tags + 1, num_tags), np.float)
        nn.transitions = transitions

        padding_left = text_reader.converter.get_padding_left(args.task == 'pos')
        padding_right = text_reader.converter.get_padding_right(args.task == 'pos')
    
    nn.padding_left = np.array(padding_left)
    nn.padding_right = np.array(padding_right)
    nn.learning_rate = args.learning_rate
    nn.l2_factor = args.l2
    nn.dropout = args.dropout
    nn.max_norm = args.max_norm
    
    if 'convolution' in args and args.convolution > 0 and args.hidden > 0:
        layer_sizes = (nn.input_size, nn.hidden_size, nn.hidden2_size, nn.output_size)
    else:
        layer_sizes = (nn.input_size, nn.hidden_size, nn.output_size)
    
    logger.info("Created new network with the following layer sizes: %s\n"
                % ', '.join(str(x) for x in layer_sizes))
    
    nn.network_filename = config.FILES[md.network]
    return nn
Example #3
0
def create_network(args, text_reader, feature_tables, md=None):
    """Creates and returns the neural network according to the task at hand."""
    logger = logging.getLogger("Logger")

    if args.task.startswith('srl') and args.task != 'srl_predicates':
        num_tags = len(text_reader.tag_dict)
        distance_tables = utils.set_distance_features(args.max_dist,
                                                      args.target_features,
                                                      args.pred_features)
        nn = ConvolutionalNetwork.create_new(feature_tables,
                                             distance_tables[0],
                                             distance_tables[1], args.window,
                                             args.convolution, args.hidden,
                                             num_tags)
        padding_left = text_reader.converter.get_padding_left(False)
        padding_right = text_reader.converter.get_padding_right(False)
        if args.identify:
            logger.info(
                "Loading initial transition scores table for argument identification"
            )
            transitions = srl.train_srl.init_transitions_simplified(
                text_reader.tag_dict)
            nn.transitions = transitions
            nn.learning_rate_trans = args.learning_rate_transitions

        elif not args.classify:
            logger.info("Loading initial IOB transition scores table")
            transitions = srl.train_srl.init_transitions(
                text_reader.tag_dict, 'iob')
            nn.transitions = transitions
            nn.learning_rate_trans = args.learning_rate_transitions

    else:
        num_tags = len(text_reader.tag_dict)
        nn = Network.create_new(feature_tables, args.window, args.hidden,
                                num_tags)
        if args.learning_rate_transitions > 0:
            transitions = np.zeros((num_tags + 1, num_tags), np.float)
            nn.transitions = transitions
            nn.learning_rate_trans = args.learning_rate_transitions

        padding_left = text_reader.converter.get_padding_left(
            args.task == 'pos')
        padding_right = text_reader.converter.get_padding_right(
            args.task == 'pos')

    nn.padding_left = np.array(padding_left)
    nn.padding_right = np.array(padding_right)
    nn.learning_rate = args.learning_rate
    nn.learning_rate_features = args.learning_rate_features

    if 'convolution' in args and args.convolution > 0 and args.hidden > 0:
        layer_sizes = (nn.input_size, nn.hidden_size, nn.hidden2_size,
                       nn.output_size)
    else:
        layer_sizes = (nn.input_size, nn.hidden_size, nn.output_size)

    logger.info("Created new network with the following layer sizes: %s" %
                ', '.join(str(x) for x in layer_sizes))

    return nn
Example #4
0
def create_network(args, text_reader, feature_tables, md):
    """Creates and returns the neural network according to the task at hand."""
    logger = logging.getLogger("Logger")

    is_dependency = 'dependency' in args.task
    is_convolution_srl = args.task.startswith(
        'srl') and args.task != 'srl_predicates'
    is_convolution = is_convolution_srl or is_dependency

    if is_convolution:
        # get some data structures used both by dep parsing and SRL
        distance_tables = utils.set_distance_features(args.max_dist,
                                                      args.target_features,
                                                      args.pred_features)
        padding_left = text_reader.converter.get_padding_left(False)
        padding_right = text_reader.converter.get_padding_right(False)

        if is_dependency:
            output_size = 1 if not args.labeled else len(text_reader.tag_dict)
            nn = ConvolutionalDependencyNetwork.create_new(
                feature_tables, distance_tables[0], distance_tables[1],
                args.window, args.convolution, args.hidden, output_size)

        else:
            # not dependency (SRL)
            num_tags = len(text_reader.tag_dict)
            nn = ConvolutionalNetwork.create_new(feature_tables,
                                                 distance_tables[0],
                                                 distance_tables[1],
                                                 args.window, args.convolution,
                                                 args.hidden, num_tags)

            if args.identify:
                logger.info(
                    "Loading initial transition scores table for argument identification"
                )
                transitions = srl.init_transitions_simplified(
                    text_reader.tag_dict)
                nn.transitions = transitions

            elif not args.classify:
                logger.info("Loading initial IOB transition scores table")
                transitions = srl.init_transitions(text_reader.tag_dict, 'iob')
                nn.transitions = transitions

    else:
        # not convolution
        num_tags = len(text_reader.tag_dict)
        nn = Network.create_new(feature_tables, args.window, args.hidden,
                                num_tags)
        transitions = np.zeros((num_tags + 1, num_tags), np.float)
        nn.transitions = transitions

        padding_left = text_reader.converter.get_padding_left(
            args.task == 'pos')
        padding_right = text_reader.converter.get_padding_right(
            args.task == 'pos')

    nn.padding_left = np.array(padding_left)
    nn.padding_right = np.array(padding_right)
    nn.learning_rate = args.learning_rate
    nn.l2_factor = args.l2
    nn.dropout = args.dropout
    nn.max_norm = args.max_norm

    if 'convolution' in args and args.convolution > 0 and args.hidden > 0:
        layer_sizes = (nn.input_size, nn.hidden_size, nn.hidden2_size,
                       nn.output_size)
    else:
        layer_sizes = (nn.input_size, nn.hidden_size, nn.output_size)

    logger.info("Created new network with the following layer sizes: %s\n" %
                ', '.join(str(x) for x in layer_sizes))

    nn.network_filename = config.FILES[md.network]
    return nn