def getSentiment(): dataSet = StanfordSentiment() tokens = dataSet.tokens() # Load the word vectors we trained earlier _, wordVectors, _ = load_saved_params() dimVectors = wordVectors.shape[1] # Load the test sentences sentences = dataSet.getTestSentences() print sentences nSentences = len(sentences) sentenceFeatures = np.zeros((nSentences, dimVectors)) sentenceLabels = np.zeros((nSentences, ), dtype=np.int32) for i in xrange(nSentences): words, sentenceLabels[i] = sentences[i] #print sentences[i] #print words #print sentenceLabels[i] sentenceFeatures[i, :] = getSentenceFeature(tokens, wordVectors, words) #train weights random.seed(3141) np.random.seed(59265) weights = np.random.randn(dimVectors, nSentences) regularization = 0.00001 weights = sgd(lambda weights: softmax_wrapper( sentenceFeatures, sentenceLabels, weights, regularization), weights, 3.0, 10, PRINT_EVERY=10) #pred = np.sum((weights * sentenceFeatures.T).T, axis = 1) #pred = softmax(pred) #testAccuracy = accuracy(sentenceLabels, pred) prob = softmax(sentenceFeatures.dot(weights)) #prob = normalizeRows(prob) _, _, pred = softmaxRegression(sentenceFeatures, sentenceLabels, weights) pred = 1 / (1 + np.exp(-pred)) #for polarity in pred: #print categorify(polarity) #_, _, pred = softmaxRegression(sentenceFeatures, sentenceLabels, weights) for label in pred: print categorify(label) #pred = categorify(pred) print prob print pred
devFeatures = np.zeros((nDev, dimVectors)) devLabels = np.zeros((nDev, ), dtype=np.int32) for i in xrange(nDev): words, devLabels[i] = devset[i] devFeatures[i, :] = getSentenceFeature(tokens, wordVectors, words) # Try our regularization parameters results = [] for regularization in REGULARIZATION: random.seed(3141) np.random.seed(59265) weights = np.random.randn(dimVectors, 5) print "Training for reg=%f" % regularization # We will do batch optimization weights = sgd(lambda weights: softmax_wrapper(trainFeatures, trainLabels, weights, regularization), weights, 3.0, 10000, PRINT_EVERY=100) # Test on train set _, _, pred = softmaxRegression(trainFeatures, trainLabels, weights) trainAccuracy = accuracy(trainLabels, pred) print "Train accuracy (%%): %f" % trainAccuracy # Test on dev set _, _, pred = softmaxRegression(devFeatures, devLabels, weights) devAccuracy = accuracy(devLabels, pred) print "Dev accuracy (%%): %f" % devAccuracy
devFeatures = np.zeros((nDev, dimVectors)) devLabels = np.zeros((nDev,), dtype=np.int32) for i in range(nDev): words, devLabels[i] = devset[i] devFeatures[i, :] = getSentenceFeature(tokens, wordVectors, words) # Try our regularization parameters results = [] for regularization in REGULARIZATION: random.seed(3141) np.random.seed(59265) weights = np.random.randn(dimVectors, 5) print("Training for reg=%f" % regularization) # We will do batch optimization weights = sgd(lambda weights: softmax_wrapper(trainFeatures, trainLabels, weights, regularization), weights, 3.0, 10000, PRINT_EVERY=100) # Test on train set _, _, pred = softmaxRegression(trainFeatures, trainLabels, weights) trainAccuracy = accuracy(trainLabels, pred) print("Train accuracy (%%): %f" % trainAccuracy) # Test on dev set _, _, pred = softmaxRegression(devFeatures, devLabels, weights) devAccuracy = accuracy(devLabels, pred) print("Dev accuracy (%%): %f" % devAccuracy) # Save the results and weights results.append({ "reg" : regularization, "weights" : weights,
devFeatures = np.zeros((nDev, dimVectors)) devLabels = np.zeros((nDev,), dtype=np.int32) for i in xrange(nDev): words, devLabels[i] = devset[i] devFeatures[i, :] = getSentenceFeature(tokens, wordVectors, words) # Try our regularization parameters results = [] for regularization in REGULARIZATION: random.seed(3141) np.random.seed(59265) weights = np.random.randn(dimVectors, 5) print "Training for reg=%f" % regularization # We will do batch optimization weights = sgd(lambda weights: softmax_wrapper(trainFeatures, trainLabels, weights, regularization), weights, 3.0, 10000, PRINT_EVERY=100) # Test on train set _, _, pred = softmaxRegression(trainFeatures, trainLabels, weights) trainAccuracy = accuracy(trainLabels, pred) print "Train accuracy (%%): %f" % trainAccuracy # Test on dev set _, _, pred = softmaxRegression(devFeatures, devLabels, weights) devAccuracy = accuracy(devLabels, pred) print "Dev accuracy (%%): %f" % devAccuracy # Save the results and weights results.append({ "reg" : regularization, "weights" : weights,