def calPredictions(thisTree, Wcat, Wv, indicies, sStr, params): wsz = params.wordSize # vecPath - contains the vectors along the node path # nodePath - contains the nodes along the node path thisTree.nodePath = findPath(thisTree, indicies) # Get the internal features thisTree.features = getInternalFeatures(thisTree.nodePath, sStr, indicies, params.features_mean, params.features_std) # Add only the top node thisTree.nodePath = np.max(thisTree.nodePath) thisTree.pooledVecPath = thisTree.nodeAct_a[:,thisTree.nodePath].reshape(wsz,1) # Add the elements thisTree.pooledVecPath = np.hstack((thisTree.pooledVecPath, thisTree.nodeAct_a[:,[indicies[0]]], thisTree.nodeAct_a[:,[indicies[1]]])) thisTree.nodePath = np.array([thisTree.nodePath, indicies[0], indicies[1]], dtype='int32') thisTree.poolMatrix = np.eye(3*wsz) # add outer context words thisTree = addOuterContext(thisTree, indicies, len(sStr), params); # add inner context words thisTree = addInnerContext(thisTree,indicies, Wv, len(sStr), params); # add in nearest neighbors # thisTree = addNN(thisTree, Wv, sNN, params); #bhanu catInput = np.concatenate((thisTree.pooledVecPath.flatten(), thisTree.features))#, thisTree.NN_vecs.flatten())) #bhanu thisTree.score = np.dot(Wcat, np.transpose(catInput)) #score for each label/tag num = np.exp(thisTree.score) thisTree.y = np.divide(num, np.sum(num)) #conditional probs for each tag/label
def _setInternalFeatureStats(self, data): # features [path length, path depth1, path depth2, sentence length, length between elements] numFeatures = 5; self.features_mean = 0; self.features_std = np.ones(numFeatures); totaltrees = 0 for s in range(len(data.allSStr)): nverbs = len(data.verbIndices[s].flatten()) sentLength = len(data.allSStr[s].flatten()) totaltrees += nverbs*sentLength allFeatures = np.zeros((totaltrees,numFeatures)); thisTree = Tree() for s in range(len(data.allSStr)): thisTree.pp = data.allSTree[s].flatten() verbIndexesList = data.verbIndices[s].flatten() for vid in verbIndexesList: for wid in range(len(data.allSStr[s])): indices = [vid, wid] fullPath = findPath(thisTree, indices) allFeatures[s,:] = getInternalFeatures(fullPath, data.allSNum[s], indices, self.features_mean, self.features_std); # Find the mean self.features_mean = np.mean(allFeatures,axis=0); # check this allFeatures = allFeatures - self.features_mean # Find the standard deviation self.features_std = np.std(allFeatures, axis=0)
def calPredictions(thisTree, Wcat, Wv, indicies, sStr, params): wsz = params.wordSize # vecPath - contains the vectors along the node path # nodePath - contains the nodes along the node path thisTree.nodePath = findPath(thisTree, indicies) # Get the internal features thisTree.features = getInternalFeatures(thisTree.nodePath, sStr, indicies, params.features_mean, params.features_std) # Add only the top node thisTree.nodePath = np.max(thisTree.nodePath) thisTree.pooledVecPath = thisTree.nodeAct_a[:, thisTree.nodePath].reshape( wsz, 1) # Add the elements thisTree.pooledVecPath = np.hstack( (thisTree.pooledVecPath, thisTree.nodeAct_a[:, [indicies[0]]], thisTree.nodeAct_a[:, [indicies[1]]])) thisTree.nodePath = np.array([thisTree.nodePath, indicies[0], indicies[1]], dtype='int32') thisTree.poolMatrix = np.eye(3 * wsz) # add outer context words thisTree = addOuterContext(thisTree, indicies, len(sStr), params) # add inner context words thisTree = addInnerContext(thisTree, indicies, Wv, len(sStr), params) # add in nearest neighbors # thisTree = addNN(thisTree, Wv, sNN, params); #bhanu catInput = np.concatenate( (thisTree.pooledVecPath.flatten(), thisTree.features)) #, thisTree.NN_vecs.flatten())) #bhanu thisTree.score = np.dot(Wcat, np.transpose(catInput)) #score for each label/tag num = np.exp(thisTree.score) thisTree.y = np.divide(num, np.sum(num)) #conditional probs for each tag/label