This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_target(words, idx, window_size=5): | |
| ''' Get a list of words in a window around an index. | |
| words -- list of words in text | |
| idx -- index of the center word | |
| window_size -- the window size to define context | |
| ''' | |
| # implement this function | |
| R = np.random.randint(1, window_size+1) | |
| start = idx - R if (idx - R) > 0 else 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # we create a list of the indices of words in vocabulary as the input to network | |
| training_reviews = [] | |
| for review in training_reviews_raw: | |
| indices = set() | |
| for word in review.split(" "): | |
| if word in word2index.keys(): | |
| indices.add(self.word2index[word]) | |
| training_reviews.append(list(indices)) | |
| # forward pass for each data point in the batch(index i) | |
| self.layer_1 *= 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for word in review.split(' '): | |
| if word in self.word2index.keys(): | |
| self.layer_0[0,self.word2index[word]] = 1 # changed "+= 1" to "= 1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [('.', 27), # noise with a large weight | |
| ('', 18), # noise... | |
| ('the', 9), | |
| ('to', 6), | |
| ('high', 5), | |
| ('i', 5), | |
| ('bromwell', 4), | |
| ('is', 4), | |
| ('a', 4), | |
| ('teachers', 4), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # forward-pass | |
| h_1 = np.dot(self.layer_0,self.weights_0_1) | |
| output = self.sigmoid(np.dot(h_1,self.weights_1_2)) | |
| # back propagation | |
| output_error = label - output | |
| output_error_term = output_error*output*(1-output) | |
| hidden_error = np.dot(output_error_term,(self.weights_1_2).T) | |
| hidden_error_term = hidden_error | |
| #weight update |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # sentences (list of list of str): A list of sentences to be parsed | |
| # model (ParserModel): The model that makes parsing decisions. It is assumed to have a function | |
| # model.predict(partial_parses) that takes in a list of PartialParses as input and | |
| # returns a list of transitions predicted for each parse. | |
| # batch_size (int): The number of PartialParses to include in each minibatch | |
| # dependencies (list of dependency lists): A list where each element is the dependencies | |
| # list for a parsed sentence. | |
| partial_parsers = [] | |
| for sentence in sentences: | |
| partial_parsers.append(PartialParse(sentence)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # sentence :The sentence to be parsed as a list of words. | |
| self.stack = ["ROOT"] # corresponds to first column in above image | |
| self.buffer = (self.sentence).copy() # corresponds to second column in above image | |
| self.dependencies = [] # corresponds to the third column in above image | |
| # transition (str): A string that equals "S", "LA", or "RA" representing the shift, | |
| # left-arc, and right-arc transitions. You can assume the provided | |
| # transition is a legal transition. | |
| # corresponds to the last column in above image | |
| if transition == "S": |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_target_for_label(label): | |
| """Convert a label to `0` or `1`. | |
| Args: | |
| label(string) - Either "POSITIVE" or "NEGATIVE". | |
| Returns: | |
| `0` or `1`. | |
| """ | |
| if label == "POSITIVE": | |
| return 1 | |
| else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def update_input_layer(review): | |
| """ Modify the global layer_0 to represent the vector form of review. | |
| The element at a given index of layer_0 should represent | |
| how many times the given word occurs in the review. | |
| Args: | |
| review(string) - the string of the review | |
| Returns: | |
| None | |
| """ | |
| global layer_0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Create a dictionary of words in the vocabulary mapped to index positions | |
| # (to be used in layer_0) | |
| word2index = {} | |
| for i,word in enumerate(vocab): | |
| word2index[word] = i |