Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix eval_sick error #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions eval_sick.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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):
"""
Expand All @@ -109,19 +113,19 @@ 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


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:
Expand All @@ -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]