def policy(self,v): """returns an optimal policy assuming the next value function is v v is a list of values for each state returns a list of the indexes of optimal actions for each state """ return [argmax(enumerate([self.reward[s][a]+self.discount*product(self.trans[s][a],v) for a in range(len(self.actions))])) for s in range(len(self.states))]
def select_action(self, state): """returns an action to carry out for the current agent given the state, and the q-function """ if flip(self.explore): return random.choice(self.actions) else: return argmax((next_act, self.q.get((state, next_act), self.qinit)) for next_act in self.actions)
def processTestFile(dwsd, filename, destination, window=5, softScoring=False): tobreak = False prevword = "" with open(destination, 'w') as d: f = open(filename) d.write("Id,Prediction\n") acc = 0.0 i = 0 start_time = time.time() for line in f: lst = line.split('|') word_pos = lst[0].strip().split('.') word = word_pos[0] pos = word_pos[1] gloss = lst[2].strip().split('%%') context = gloss[0] + gloss[1] + gloss[2] # pick up a number of pre- and post- words defined by int window pre_words = gloss[0].strip().split(' ') pre_words = pre_words[-window:] post_words = gloss[2].strip().split(' ') post_words = post_words[:window] if prevword != word: prevword = word print "Target: ", word, "at line", i, "of 3918 for test", time.time() - start_time #print "Context: ", context #print "POS: ", pos # algorithm bottleneck is HERE scores, alpha = dwsd.Lesk(word, pos, pre_words, post_words,softScoring) sense = utilities.argmax(zip(scores.keys(),scores.values())) if max(scores.values()) == alpha: # we got no overlap! print sense, "original score but no overlap." print zip(scores.keys(),scores.values()) sense = 1 # we should guess this by default. #tobreak = True trueSense = int(lst[1].strip()) if softScoring: #print scores if trueSense in scores: #print scores[trueSense] acc += scores[trueSense] else: if sense == trueSense: acc += 1 i += 1 d.write("" + str(i) + "," + str(sense) + "\n") if tobreak: break # REMOVE AFTER DONE WITH d.close() return acc / float(i)
def select_action(self, state): """returns an action to carry out for the current agent given the state, and the q-function. This implements an epsilon-greedy approach where self.explore is the probability of exploring. """ if flip(self.explore): return random.choice(self.actions) else: return argmax((next_act, dot_product(self.weights, self.get_features(state,next_act))) for next_act in self.actions)
def classify(self,testSet,alpha=1,softscore=False,kaggle=False,biasTowardsCommon=True): predictions = [] actual = [] correct = 0 for example in testSet: word = example[0] actual.append(example[1]) features = example[2] probs = [] if word in self.wordCounts: wc = self.wordCounts[word] for sense in self.senseCounts[word]: sc = self.senseCounts[word][sense] prob = 0.0 if biasTowardsCommon: prob = math.log(sc/float(wc)) # Only go through the features we have from training, ignore features that arise in test example # that don't appear in any training example as they will all have the same effect (sort of) on the probability if word in self.featureLists: for key in self.featureLists[word]: #feature is non-null/0 in this example fp = 0.0 if key in features: value = features[key] #Does this sense of the word have this feature as non-null/0 if key in self.featureCounts[(word,sense)]: #Are there any of the value in this test example in our training examples if value in self.featureCounts[(word,sense)][key]: fp = math.log((self.featureCounts[(word,sense)][key][value] + alpha) / float(sc+alpha*len(self.featureLists[word]))) #if not, use add one smoothing to avoid zero probability else: fp = math.log(alpha/float(sc+alpha*len(self.featureLists[word]))) #If it doesn't then using add 1 smoothing compute probability else: fp = math.log(alpha/float(sc+alpha*len(self.featureLists[word]))) # feature is 0/null in example else: #if the feature has non-null/0 value in any of our test example if key in self.featureCounts[(word,sense)]: fp = math.log((sc-sum(self.featureCounts[(word,sense)][key].values())+alpha) / float(sc+alpha*len(self.featureLists[word]))) #The feature is null for all train examples else: fp = math.log((sc + alpha) / float(sc+alpha*len(self.featureLists[word]))) #print key + ": " + str(fp) prob += fp probs.append((sense,prob)) res = None if softscore: # separate out the senses and the probabilities into 2 lists senses = list(zip(*probs)[0]) prob_numbers = list(zip(*probs)[1]) #Adjust probabilities by maximum log to avoid underflow if possible when normalizing. m_log = max(prob_numbers) prob_numbers = [math.e**(x-m_log) for x in prob_numbers] # normalize probabilities prob_sum = sum(prob_numbers) prob_numbers = [x/prob_sum for x in prob_numbers] probs = zip(senses, prob_numbers) res = utilities.argmax(probs) probs = dict(probs) if example[1] in probs: correct += probs[example[1]] else: res = utilities.argmax(probs) correct = correct if res != example[1] else correct+1 predictions.append(res) else: predictions.append(-1) accuracy = correct/float(len(predictions)) if kaggle: with open('kaggle_results','w') as f: i = 1 f.write('Id,Prediction\n') for prediction in predictions: f.write(str(i) + "," + str(prediction) + "\n") i+=1 return (accuracy,zip(actual,predictions))