zoukankan      html  css  js  c++  java
  • 动手学深度学习PyTorch版-task02

    task0201.文本预处理


    • 代码解读
    import collections
    import re
    with open('timemachine.txt', 'r', encoding='UTF-8') as f:
            lines = [re.sub('[^a-z]+', ' ', line.strip().lower()) for line in f]
            print(lines)
    

    In[8]: lines[0]
    Out[8]: 'the time machine'
    In[9]: lines[1]
    Out[9]: ''
    In[10]: lines[0].split()
    Out[10]: ['the', 'time', 'machine']
    In[11]: lines[0].split(' ')
    Out[11]: ['the', 'time', 'machine']
    




    文本预处理步骤:

    1.读取文本  2.分词  3.构建字典 建立索引  
    

    Vocab字典构建步骤:

    1.统计词频,去重筛选掉低频词  
    2.根据需求添加特殊的token  
    3.建立字典,将每个token建立映射到唯一的索引
    4.建立索引到token的映射
    

    部分知识汇总:

    string = ' AbcDe'  # 主要是说明string是一个字符串,下面是视频中涉及到了两个操作
    string.strip()  # 用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列,在这里是用来删除单词之间的空格和换行符
    string.lower()  # 将字符串中的所有大写字母都转化成小写
    
    #两者还可以拼起来用:string.strip().lower() 具体的意思也就是上面两个意思拼起来
    
    
    
    lines = [re.sub('[^a-z]+', ' ', line.strip().lower()) for line in f]
    
    # 上面这行代码的正则部分为
    re.sub('[^a-z]+', ' ', str)
    # re.sub()函数是用来字符串替换的函数
    # '[^a-z]+' 注意这里的^是非的意思,就是说非a-z字符串
    # 上面句子的含义是:将字符串str中的非小写字母开头的字符串以空格代替
    
    

    课后习题

    task0202.语言模型与数据集




    课后习题


    task0203.循环神经网络基础







    课后习题




  • 相关阅读:
    Go的Web之旅【部署】
    module declares its path as: github.com/xxx/yyy【Go报错】
    android studio快捷键集合
    Web App 概述
    WIN10 kafka搭建完后命令测试步骤自用
    Echarts tab 切换解决图不能显示的问题
    如何为不定高度(height:auto)的元素添加CSS3 transition-property:height 动画
    手持设备开发项目实例
    SQL Server 查找表问题
    Tomcat 10 升级注意事项
  • 原文地址:https://www.cnblogs.com/HongjianChen/p/12309595.html
Copyright © 2011-2022 走看看