def test(teat_path, n): ids = pickle.load(open('ids.pkl', 'rb')) ids_len = len(ids) feat_lab = [] for line in open(teat_path, 'r'): sentence = line.strip('\n').lower() phi = defaultdict(int) phi = create_features(sentence, n, phi, ids) feat_lab.append(phi) feat_list = [] for feat in feat_lab: feat_list.append(create_phi_list(feat, ids_len)) out = Output() hid = pickle.load(open('hidden_model_9', 'rb')) inp = pickle.load(open('input_model_9', 'rb')) with open('my_answer', 'w') as f: for i, phi in enumerate(feat_list): forward_nn(phi, out, hid, inp) result = out.result() print(result, file=f)
def main(): with open('net', 'rb') as net_f,\ open('ids', 'rb') as ids_f: net = dill.loads(net_f.read()) ids = dill.loads(ids_f.read()) text = [] with open('../../data/titles-en-test.word', 'r') as input_file: for line in input_file: words = line.strip().split(' ') test_phi0 = [[0]] * len(ids) phi0_dict = Counter(words) for word, count in phi0_dict.items(): if f'UNI:{word}' in ids: test_phi0[ids[f'UNI:{word}']] = [count] test_phi = forward_nn(net, test_phi0) print(test_phi[len(net)][0][0]) if test_phi[len(net)][0][0] >= 0: text.append('1') else: text.append('-1') with open('test_answer.txt', 'w') as out_file: out_file.write('\n'.join(text))
import pickle from train_nn import create_features, forward_nn if __name__ == '__main__': with open('weight_file.txt', 'rb') as w, open('id_file.txt', 'rb') as id_: net = pickle.load(w) ids = pickle.load(id_) with open('../../data/titles-en-test.word') as test, open( 'my_answer.labeled', 'w') as answer: for line in test: phi0 = create_features(line, ids) phi = forward_nn(net, phi0) y = (1 if phi[len(net) - 1][0] >= 0 else -1) answer.write('{}\n'.format(y))
import pickle from train_nn import forward_nn def create_features(x): phi = [0 for i in range(len(ids))] words = x.strip().split() for word in words: if 'UNI:' + word in ids: phi[ids['UNI:' + word]] += 1 return phi if __name__ == '__main__': test_f = '../../data/titles-en-test.word' answer = 'my_answer.nn' with open('network5_s_l.dump', 'rb') as net_f: net = pickle.load(net_f) with open('ids5_s_l.dump', 'rb') as ids_f: ids = pickle.load(ids_f) with open(answer, 'w') as ans_f, open(test_f, 'r') as t_f: for x in t_f: x = x.strip() x_l = x.lower() phi_0 = create_features(x_l) phiS = forward_nn(net, phi_0) y_ = (1 if phiS[len(net)][0] >= 0 else -1) ans_f.write('{}\t{}\n'.format(y_, x))
from train_nn import train,forward_nn import sys import numpy as np network,d=train(open(sys.argv[1])) for line in open(sys.argv[2]): phi0=np.zeros(len(d)) for item in line.strip("\n").split(" "): if item in d: phi0[d[item]]+=1 phi=forward_nn(network,phi0) if phi[-1]<0: y="-1" else: y="+1" print(y+"\t"+line.strip("\n"))
return 1 else: return -1 def create_features(x): phi0 = [0] * len(ids) words = x.split() for word in words: if word in ids: phi0[ids[word]] += 1 return phi0 if __name__ == '__main__': with open('id_file.txt') as id_f: ids = {} for iddayo in id_f: x, y = iddayo.split() ids[x] = int(y) with open('weight_file.txt', 'rb') as w_f: weight = pickle.load(w_f) with open('../../data/titles-en-test.word') as text: with open('my_answer.txt', 'w') as answer: for line in text: phi0 = create_features(line) phi = train_nn.forward_nn(weight, phi0) ydash = sign(phi[-1][0]) print(ydash, file=answer)
from train_nn import forward_nn import numpy as np import pickle with open('weight_file.dump', 'rb') as f: network = pickle.load(f) with open('id_file.dump', 'rb') as g: ids = pickle.load(g) for line in open('../../data/titles-en-test.word', 'r'): features = np.zeros(len(ids)) for word in line.strip('\n').split(): if word in ids: features[ids[word]] += 1 polarity = 1 if forward_nn(network, features)[2][0] > 0 else -1 print(str(polarity) + '\t' + line.strip('\n'))