Exemple #1
0
def predict_all(model_file, input_file):
    w = dict()
    for line in open(model_file):
        name, value = line.strip('\n').split('\t')
        w[name] = float(value)
    with open('my_answer.txt', 'w') as fp:
        for x in open(input_file):
            phi = create_features(x)
            y_d = predict_one(w, phi)
            print(y_d, file=fp)
Exemple #2
0
def predict_all(model_file, input_file):
    w = dict()
    for line in open(model_file):
        name, value = line.strip('\n').split('\t')
        w[name] = float(value)
    with open('my_answer.txt', 'w') as fp:
        for x in open(input_file):
            phi = create_features(x)
            y_d = predict_one(w, phi)
            print(y_d, file=fp)
def train_nn(train_file, model_file, num_nodes, num_layers, num_iter):
  """Train a neural network"""
  import random
  import pickle
  import sys

  NUM_NODES  = num_nodes
  NUM_LAYERS = num_layers
  NUM_ITER   = num_iter
  # Too large learning rate gives bizarre results
  #LEARNING_RATE = 0.5
  LEARNING_RATE = 0.1

  # See page 10
  network = []
  for i in range(NUM_LAYERS):
    for j in range(NUM_NODES):
      weight = {}
      node = (i + 1, weight) # note that + 1
      network.append(node)
  # the last node
  network.append((NUM_LAYERS + 1, {}))
  
  for i in range(NUM_ITER):
    print >>sys.stderr, "Iteration #%s..." % (i)
    for line in open(train_file, 'r'):
      line = line.rstrip()
      (y, x) = line.split("\t")
      y = int(y)
      phi = create_features(x)
      update_nn(network, phi, y, LEARNING_RATE)

  # debugging
  for line in open(train_file, 'r'):
    line = line.rstrip()
    (y, x) = line.split("\t")
    y = int(y)
    phi = create_features(x)
    y_ = predict_nn(network, phi)
    print y, y_[-1].values()[0]

  with open(model_file, 'w') as f:
    pickle.dump(network, f)
Exemple #4
0
def predict_all(model_file,input_file):
    with open(model_file,'r') as m_f,open(input_file,'r') as i_f:
        w = defaultdict(lambda :0)
        for line in m_f:
            word,value = line.split('\t')
            w[word] = float(value)
        for x in i_f:
            phi = defaultdict(lambda :0)
            for k,v in create_features(x).items():
                phi[k] = float(v)
            y_ = predict_one(w,phi)
            yield('{}\t{}'.format(y_,x))
def test_nn(test_file, model_file):
    """Test using a neural network"""
    import pickle

    with open(model_file, 'r') as f:
        network = pickle.load(f)

    for line in open(test_file, 'r'):
        line = line.rstrip()
        phi = create_features(line)
        y = predict_nn(network, phi)
        #print y[-1].values()[0]
        if y[-1].values()[0] >= 0:
            print 1
        else:
            print -1
def test_nn(test_file, model_file):
    """Test using a neural network"""
    import pickle

    with open(model_file, "r") as f:
        network = pickle.load(f)

    for line in open(test_file, "r"):
        line = line.rstrip()
        phi = create_features(line)
        y = predict_nn(network, phi)
        # print y[-1].values()[0]
        if y[-1].values()[0] >= 0:
            print 1
        else:
            print -1
def predict_all(model_file, input_file):
  """Reads model file and predict instance one by one"""
  # Reading a model file
  w = {}
  for line in open(model_file):
    line = line.strip()
    (name, value) = line.split("\t")
    value = float(value)
    w[name] = value

  # Evaluation and print results
  for line in open(input_file):
    line = line.strip()
    phi = create_features(line)
    y_ = predict_one(w, phi)

    print y_
def predict_all(model_file, input_file):
    """Reads model file and predict instance one by one"""
    # Reading a model file
    w = {}
    for line in open(model_file):
        line = line.strip()
        (name, value) = line.split("\t")
        value = float(value)
        w[name] = value

    # Evaluation and print results
    for line in open(input_file):
        line = line.strip()
        phi = create_features(line)
        y_ = predict_one(w, phi)

        print y_
Exemple #9
0
def pred_all(model_file, input_file=inputfile):

    ansfile = model_file + '_ans'

    phi = defaultdict(lambda : 0.)
    with open(model_file) as modelf:
        w = defaultdict(lambda: 0.)
        for model_line in modelf:
            name, weight = model_line.split('\t')
            # print("{}\t{}".format(name,weight))
            w[name] = float(weight)

    with open(input_file) as inputf, open(ansfile, 'w') as ansout:
        for in_line in inputf:
            l = in_line
            # print(l)
            phi = create_features(l.lower())
            y_pred = int(predict_one(w, phi))
            print('{}\t{}'.format(y_pred, l), end='', file=ansout)

    import subprocess
    subprocess.call('../../script/grade-prediction.py ../../data/titles-en-test.labeled ' + ansfile, shell=True)
Exemple #10
0
from train_svm import sign, create_features, product
from collections import defaultdict

W = defaultdict(float)
for line in open('train_result.txt', 'r'):
    key, value = line.split('\t')
    W[key] = float(value)

for x in open('../../data/titles-en-test.word', 'r'):
    phi = create_features(x)
    y_prime = product(W, phi)
    print(sign(y_prime))