From fa3035668299d77f1047ba2024d44bf5205be8e3 Mon Sep 17 00:00:00 2001 From: Xittong Yu Date: Mon, 9 Jan 2017 17:22:47 +0800 Subject: [PATCH] fix eval_sick error --- eval_sick.py | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/eval_sick.py b/eval_sick.py index 9825710..b8387d6 100644 --- a/eval_sick.py +++ b/eval_sick.py @@ -20,12 +20,13 @@ def evaluate(model, seed=1234, evaltest=False): """ print 'Preparing data...' train, dev, test, scores = load_data() - train[0], train[1], scores[0] = shuffle(train[0], train[1], scores[0], random_state=seed) - + train[0], train[1], scores[0] = shuffle( + train[0], train[1], scores[0], random_state=seed) + print 'Computing training skipthoughts...' trainA = skipthoughts.encode(model, train[0], verbose=False, use_eos=True) trainB = skipthoughts.encode(model, train[1], verbose=False, use_eos=True) - + print 'Computing development skipthoughts...' devA = skipthoughts.encode(model, dev[0], verbose=False, use_eos=True) devB = skipthoughts.encode(model, dev[1], verbose=False, use_eos=True) @@ -46,14 +47,16 @@ def evaluate(model, seed=1234, evaltest=False): if evaltest: print 'Computing test skipthoughts...' - testA = skipthoughts.encode(model, test[0], verbose=False, use_eos=True) - testB = skipthoughts.encode(model, test[1], verbose=False, use_eos=True) + testA = skipthoughts.encode( + model, test[0], verbose=False, use_eos=True) + testB = skipthoughts.encode( + model, test[1], verbose=False, use_eos=True) print 'Computing feature combinations...' testF = np.c_[np.abs(testA - testB), testA * testB] print 'Evaluating...' - r = np.arange(1,6) + r = np.arange(1, 6) yhat = np.dot(bestlrmodel.predict_proba(testF, verbose=2), r) pr = pearsonr(yhat, scores[2])[0] sr = spearmanr(yhat, scores[2])[0] @@ -70,7 +73,7 @@ def prepare_model(ninputs=9600, nclass=5): Set up and compile the model architecture (Logistic regression) """ lrmodel = Sequential() - lrmodel.add(Dense(ninputs, nclass)) + lrmodel.add(Dense(nclass, input_dim=ninputs)) lrmodel.add(Activation('softmax')) lrmodel.compile(loss='categorical_crossentropy', optimizer='adam') return lrmodel @@ -82,11 +85,12 @@ def train_model(lrmodel, X, Y, devX, devY, devscores): """ done = False best = -1.0 - r = np.arange(1,6) - + r = np.arange(1, 6) + while not done: # Every 100 epochs, check Pearson on development set - lrmodel.fit(X, Y, verbose=2, shuffle=False, validation_data=(devX, devY)) + lrmodel.fit(X, Y, verbose=2, shuffle=False, + validation_data=(devX, devY)) yhat = np.dot(lrmodel.predict_proba(devX, verbose=2), r) score = pearsonr(yhat, devscores)[0] if score > best: @@ -100,7 +104,7 @@ def train_model(lrmodel, X, Y, devX, devY, devscores): score = pearsonr(yhat, devscores)[0] print 'Dev Pearson: ' + str(score) return bestlrmodel - + def encode_labels(labels, nclass=5): """ @@ -109,10 +113,10 @@ def encode_labels(labels, nclass=5): Y = np.zeros((len(labels), nclass)).astype('float32') for j, y in enumerate(labels): for i in range(nclass): - if i+1 == np.floor(y) + 1: - Y[j,i] = y - np.floor(y) - if i+1 == np.floor(y): - Y[j,i] = np.floor(y) - y + 1 + if i + 1 == np.floor(y) + 1: + Y[j, i] = y - np.floor(y) + if i + 1 == np.floor(y): + Y[j, i] = np.floor(y) - y + 1 return Y @@ -120,8 +124,8 @@ def load_data(loc='./data/'): """ Load the SICK semantic-relatedness dataset """ - trainA, trainB, devA, devB, testA, testB = [],[],[],[],[],[] - trainS, devS, testS = [],[],[] + trainA, trainB, devA, devB, testA, testB = [], [], [], [], [], [] + trainS, devS, testS = [], [], [] with open(loc + 'SICK_train.txt', 'rb') as f: for line in f: @@ -147,5 +151,3 @@ def load_data(loc='./data/'): testS = [float(s) for s in testS[1:]] return [trainA[1:], trainB[1:]], [devA[1:], devB[1:]], [testA[1:], testB[1:]], [trainS, devS, testS] - -