CS224n - Assignment1 - Sentiment Analysis

代码链接,在原有代码做了些小的修改,适用于python3.6

4 Sentiment Analysis (20')

对于Stanford Sentiment Treebank数据集中的每个句子,我们使用该句子中所有单词向量的平均值作为特征,从而预测情绪水平。

我们将训练softmax分类器,并执行train / dev验证以改进分类器的泛化能力。

(a) 句子的特征表示:取句子中单词向量的平均值

具体参考q4 sentiment.py:

def getSentenceFeatures(tokens,wordVectors,sentence):
    """
    obtain the sentence feature for sentiment analysis by averaging its word vectors.
    :param tokens: a dictionary that maps words to their indices in the word vector list
    :param wordVectors: word vectors (each row) for all tokens
    :param sentence: a list of words in the sentence of interest
    :return: sentVector: feature vector for the sentence
    """
    sentVector=np.zeros((wordVectors.shape[1],))

    ### YOUR CODE HERE
    for s in sentence:
        sentVector+=wordVectors[tokens[s],:]
    sentVector*=1.0/len(sentence)
    ### END YOUR CODE

    assert sentVector.shape==(wordVectors.shape[1],)
    return sentVector

(b) 正则化的原因:

避免过拟合,增强对未知样例的泛华能力

(c) 具体参考q4 sentiment.py:

搜索“最佳”正则化参数:

def getRegularizationValues():
    """Try different regularizations
    :return: sorted: a sorted list of values to try
    """
    values=None # Assign a list of floats in the block below
    ### YOUR CODE HERE
    values=np.logspace(-4,2,num=100,base=10)
    ### END YOUR CODE
    return sorted(values)

(1)numpy.arange 函数用于创建数值范围并返回 ndarray 对象:

numpy.arange(start, stop, step, dtype)
参数描述
start起始值,默认为0
stop终止值(不包含)
step步长,默认为1
dtype返回ndarray的数据类型,如果没有提供,则会使用输入数据的类型。

(2) numpy.linspace 函数用于创建一个一维数组,数组是一个等差数列构成:

np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
参数描述
start序列的起始值
stop序列的终止值,如果endpointtrue,该值包含于数列中
num要生成的等步长的样本数量,默认为50
endpoint该值为 ture 时,数列中中包含stop值,反之不包含,默认是True。
retstep如果为 True 时,生成的数组中会显示间距,反之不显示。
dtypendarray 的数据类型

(3)numpy.logspace 函数用于创建一个于等比数列

logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)

base 参数意思是取对数的时候 log 的下标。

参数描述
start序列的起始值为:base ** start
stop序列的终止值为:base ** stop。如果endpointtrue,该值包含于数列中
num要生成的等步长的样本数量,默认为50
endpoint该值为 ture 时,数列中中包含stop值,反之不包含,默认是True。
base对数 log 的底数。
dtypendarray 的数据类型

(d) 模型跑起来

  • 将python的位置加入环境变量中系统变量path里面
  • 打开命令提示行,进入q4_sentiment.py所在的文件夹
  • 输入python q4_sentiment.py --yourvectors:利用你自己的词向量训练模型
  • 输入python q4_sentiment.py--pretrained:利用GloVe的词向量训练模型

我们认为预训练的向量训练效果更好:

  1. 更高维的词向量可以编码更多信息
  2. GloVe向量是在更大的语料库上训练的结果
  3. GloVe vs Word2Vec

(e) 针对训练集和测试集,绘制关于预训练GloVe向量的正则化值的分类精度,利用q4_reg_acc.png保存。

  1. 随着正则化参数不断增长,模型经历了过拟合->最优拟合->欠拟合的变化过程,正则化参数取10^(1)时实现最优拟合
  2. 过拟合->最优拟合:正则化参数从10^(-4)到10^(1),训练集精确率稍有下降,验证集精确率上升
  3. 最优拟合->欠拟合:正则化参数从10^(1)到10^(2),训练集和测试集的精确率均下降

(f) 运行python q4_sentiment.py --pretrained,还会生成一个q4_dev_conf.png图像:

  1. 图中用蓝色笔画出对的斜对角线上的为正确预测的情况
  2. 距离蓝色勾选框越远说明预测结果越差

(g) 选择3个示例包括分类器出错以及正确的情况,并简要说明错误原因以及正确分类所需的功能

正确示例:4 4 a warm , funny , engaging film .

错误示例: 4 1 it 's refreshing to see a girl-power movie that does n't feel it has to prove anything .

分析:词向量的平均会破坏词顺序且不能处理否定does n't

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值