def test_srl_neural_model(train_file_path, valid_file_path):

    train_corpora = Conll05Corpora()
    train_corpora.load(train_file_path)
    train_problem = SRLProblem(train_corpora)

    valid_corpora = Conll05Corpora()
    valid_corpora.load(valid_file_path)
    valid_problem = SRLProblem(valid_corpora)

    init_rng()

    nn_architecture = SRLNetowrkArchitecture()

    nn_architecture.word_feature_dim = 50
    nn_architecture.pos_feature_dim = 50
    nn_architecture.dist_feature_dim = 50

    nn_architecture.conv_window_height = 3
    nn_architecture.conv_output_dim = 500

    nn_architecture.hidden_layer_output_dims = [500,500]


    hyper_param = NeuralModelHyperParameter()

    hyper_param.n_epochs = 1000
    hyper_param.learning_rate = 0.001 #1
    hyper_param.learning_rate_decay_ratio = 0.8
    hyper_param.learning_rate_lowerbound = 0.0000
    hyper_param.l1_reg = 0
    hyper_param.l2_reg = 0

    problem_character = train_problem.get_problem_property()

    m = SRLNetwork(problem_character, nn_architecture)

    optimizer = CGDOptimizer()

    trained_batch_num = 0
    valid_freq = 10

    for iter in range(1000):
        for sentence in train_problem.sentences():

            for X, y in train_problem.get_dataset_for_sentence(sentence):

                if trained_batch_num % valid_freq == 0:
                    evaluate(m, valid_problem, trained_batch_num/valid_freq)


                optimizer.batch_size = X.shape[0]
                optimizer.update_chunk(X, y)

                param = optimizer.optimize(m, m.get_parameter())

                m.set_parameter(param)

                trained_batch_num += 1
Пример #2
0
def test_cdg_optimizer():


    layer_setting = [{"type": "perception", "activator_type": "sigmoid", "input_dim":50, "output_dim": 100},
                     {"type": "perception", "activator_type": "sigmoid", "input_dim":100, "output_dim": 100},
                     {"type": "perception", "activator_type": "sigmoid", "input_dim":100, "output_dim": 100},
                     {"type": "perception", "activator_type": "sigmoid", "input_dim":100, "output_dim": 1}]

    cost = {"type":"mse"}

    m = MultiLayerPerception(layer_setting, cost)

    optimizer = CGDOptimizer(max_epoches=3, batch_size=500)

    X = numpy.random.random((1000, 50))
    y = numpy.random.random((1000, 1))

    optimizer.update_chunk(X,y)

    param = optimizer.optimize(m, m.get_parameter())

    m.set_parameter(param)