num

语言模型

人盡茶涼 提交于 2020-02-15 12:41:07
1 读取数据集 with open ( '/home/kesci/input/jaychou_lyrics4703/jaychou_lyrics.txt' ) as f : corpus_chars = f . read ( ) print ( len ( corpus_chars ) ) print ( corpus_chars [ : 40 ] ) corpus_chars = corpus_chars . replace ( '\n' , ' ' ) . replace ( '\r' , ' ' ) corpus_chars = corpus_chars [ : 10000 ] 2 建立字符索引 idx_to_char = list ( set ( corpus_chars ) ) # 去重,得到索引到字符的映射 char_to_idx = { char : i for i , char in enumerate ( idx_to_char ) } # 字符到索引的映射 vocab_size = len ( char_to_idx ) print ( vocab_size ) corpus_indices = [ char_to_idx [ char ] for char in corpus_chars ] # 将每个字符转化为索引,得到一个索引的序列 sample =

ORM操作

柔情痞子 提交于 2020-02-15 12:33:17
必知必会13条 # 得到是queryset对象 all() # 查询所有结果 filter() # 过滤 exclude() # 排除 values() # 以字典的形式储存在对象列表里 values_list() # 以字典中的值的形式数据存放在元组里 order_by() # 排序 reverse() # 翻转 distinct() #从返回结果中剔除重复纪录 # 得到的是对象: get() # 返回与所给筛选条件相匹配的对象,返回结果有且只有一个,如果符合筛选条件的对象超过一个或者没有都会抛出错误。 first() # 返回第一条记录 last() # 返回最后一条记录 # 其他: count() # 返回数据库中匹配查询(QuerySet)的对象数量。 exists() # 返回布尔值 基于字段的查询 models.Tb1.objects.filter(id__lt=10, id__gt=1) # 获取id大于1 且 小于10的值 models.Tb1.objects.filter(id__in=[11, 22, 33]) # 获取id等于11、22、33的数据 models.Tb1.objects.exclude(id__in=[11, 22, 33]) # not in models.Tb1.objects.filter(name__contains="ven") #

计算机思维综合训练-java-14

蹲街弑〆低调 提交于 2020-02-15 11:23:01
java List 题目 题面描述: 有一个整数序列,序列中每个元素的质因数只有2、3、5,该序列的前几个元素为1、2、3、4、5、6、8、9、10…。按惯例,1也作为序列中的元素,且是序列中的第一个元素。现在感兴趣的是,给定一个位置n(1≤n≤10000)),该序列中第n个元素是多少? 输入: 输入有若干行,每行为一个整数n,为查询的元素位置。 输出: 对每行输入,在单独的行中输出序列中对应位置的元素。 示例输入: 1 2 3 7 示例输出: 1 2 3 8 代码 import java . util . Scanner ; public class Main { public static long min ( long a , long b , long c ) { long tem = ( a < b ? a : b ) ; return ( tem < c ? tem : c ) ; } public static long Find ( int n ) { long num [ ] = new long [ n ] ; num [ 0 ] = 1 ; int index2 = 0 ; int index3 = 0 ; int index5 = 0 ; int index = 1 ; while ( index < n ) { long val = min ( num [

Pytorch学习笔记 Task02

混江龙づ霸主 提交于 2020-02-15 11:19:28
第一部分:文本预处理 文本是一类序列数据,一篇文章可以看作是字符或单词的序列,本节将介绍文本数据的常见预处理步骤,预处理通常包括四个步骤: 读入文本 分词 建立字典,将每个词映射到一个唯一的索引(index) 将文本从词的序列转换为索引的序列,方便输入模型 #读入文本 import collections import re def read_time_machine ( ) : with open ( '/home/kesci/input/timemachine7163/timemachine.txt' , 'r' ) as f : lines = [ re . sub ( '[^a-z]+' , ' ' , line . strip ( ) . lower ( ) ) for line in f ] return lines lines = read_time_machine ( ) print ( '# sentences %d' % len ( lines ) ) #分词: def tokenize ( sentences , token = 'word' ) : """Split sentences into word or char tokens""" if token == 'word' : return [ sentence . split ( ' ' ) for

deeplearning_class1:线性回归&softmax与分类模型&多层感知机

时光总嘲笑我的痴心妄想 提交于 2020-02-15 11:18:36
1.线性回归 线性回归的基本公式是: Y = W X + b Y=WX+b Y = W X + b 在计算线性回归中,我们需要求解的就是误差最小时的线性回归方程,为此,需要了解 损失函数 。这里我们以差平方来表示误差,有 l ( i ) ( w , b ) = 1 2 ( y ^ − y ( i ) ) 2 l^{(i)}(w,b)=\frac{1}{2}(\hat{y}-y^{(i)})^2 l ( i ) ( w , b ) = 2 1 ​ ( y ^ ​ − y ( i ) ) 2 损失函数 以平均误差和来表示: L ( w , b ) = 1 n ∑ i = 1 n l ( i ) ( w , b ) L(w,b) = \frac{1}{n}\sum^{n}_{i=1}l^{(i)}(w,b) L ( w , b ) = n 1 ​ i = 1 ∑ n ​ l ( i ) ( w , b ) 所谓线性回归模型的训练,就是指训练以数据,得到误差最小的线性回归方程,通俗的讲,就是一次次的向线性回归模型中带入训练集,通过计算误差,来调整线性回归模型的 w w w 和 b b b 。 这里,我们采用 随机梯度下降 的方法来调整 w w w , b b b : ( w , b ) ← ( w , b ) − η β ∑ i ∈ β ∂ ( w , b ) l ( i ) ( w ,

softmax和分类模型

送分小仙女□ 提交于 2020-02-15 11:17:51
softmax和分类模型 内容包含: 如何获取Fashion-MNIST数据集和读取数据 softmax回归模型的从零开始实现,实现一个对Fashion-MNIST训练集中的图像数据进行分类的模型 使用pytorch重新实现softmax回归模型 softmax的简洁实现 In [63]: # 加载各种包或者模块 import torch from torch import nn from torch.nn import init import numpy as np import sys sys.path.append("/home/kesci/input") import d2lzh1981 as d2l print(torch.__version__) 初始化参数和获取数据 In [64]: batch_size = 256 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) 定义网络模型 In [65]: num_inputs = 784 num_outputs = 10 class LinearNet(nn.Module): def __init__(self, num_inputs, num_outputs): super(LinearNet, self).__init__() self

darawhale第一次学习打卡笔记

丶灬走出姿态 提交于 2020-02-15 10:34:54
1.线性回归 线性回归的公式 是一种线性关系 线性回归的损失函数 常用的一种损失函数是均方误差,公式如下 优化函数 这里用的是小批量随机梯度下降法,这种方法也是神经网络中常用的方法 需要注意的点 优化函数的代码 def sgd ( params , lr , batch_size ) : for param in params : param . data -= lr * param . grad / batch_size # ues .data to operate param without gradient track 这里除以了batch_size是因为在后续代码中传入的梯度是求和后标量的梯度 所以要除以batch_size 训练的代码 # super parameters init lr = 0.03 num_epochs = 5 net = linreg loss = squared_loss # training for epoch in range ( num_epochs ) : # training repeats num_epochs times # in each epoch, all the samples in dataset will be used once # X is the feature and y is the label of a

task2之循环神经网络基础

家住魔仙堡 提交于 2020-02-15 10:12:56
本次基于当前输入与过去的输入序列预测序列的下一个字符以展示使用循环神经网络实现语言模型。 循环神经网络引入一个隐藏变量*H*,用*Ht*表示*H*在时间步*t*的值。*Ht* 的计算基于 *Xt* 和 *Ht−1* ,可以认为 *Ht* 记录了到当前字符为止的序列信息,利用 *Ht* 对序列的下一个字符进行预测。 **循环神经网络的构造** 假设 *Xt*∈*Rn×d* 是时间步 *t* 的小批量输入, *Ht*∈*Rn×h* 是该时间步的隐藏变量,则: *Ht = ϕ(Xt Wxh + Ht−1 Whh + bh).* 其中,*Wxh* ∈ *Rd×h* , *Whh* ∈ *Rh×h* , *bh* ∈ *R1×h* , *ϕ* 函数是非线性激活函数。由于引入了 *Ht−1* *Whh* , *Ht* 能够捕捉截至当前时间步的序列的历史信息,就像是神经网络当前时间步的状态或记忆一样。由于 *Ht* 的计算基于 *Ht−1* ,上式的计算是循环的,使用循环计算的网络即循环神经网络(recurrent neural network)。 在时间步 t ,输出层的输出为: *Ot = Ht Whq + bq.* 其中 *Whq *∈* Rh×q , bq *∈* R1×q* 。 **定义模型** 使用Pytorch中的nn.RNN来构造循环神经网络。在本节中,我们主要关注nn

6.5 循环神经网络的简洁实现

心不动则不痛 提交于 2020-02-15 10:10:00
6.5.1 定义模型 Mxnet: num_hiddens = 256 rnn_layer = rnn . RNN ( num_hiddens ) rnn_layer . initialize ( ) batch_size = 2 state = rnn_layer . begin_state ( batch_size = batch_size ) state [ 0 ] . shape num_steps = 35 X = nd . random . uniform ( shape = ( num_steps , batch_size , vocab_size ) ) Y , state_new = rnn_layer ( X , state ) Y . shape , len ( state_new ) , state_new [ 0 ] . shape # 本类已保存在d2lzh包中方便以后使用 class RNNModel ( nn . Block ) : def __init__ ( self , rnn_layer , vocab_size , ** kwargs ) : super ( RNNModel , self ) . __init__ ( ** kwargs ) self . rnn = rnn_layer self . vocab_size = vocab

Datawhale:动手深度学习第一次打卡!

不羁的心 提交于 2020-02-15 09:46:34
Datawhale:动手深度学习第一次打卡! 这几天将视频中的代码重新敲了一边,感觉自己学到了不少东西: 编程一定要多练呀!!!(叫破喉咙) 要多看看一些写的好的代码,不然你就不知道自己代码写的有多辣眼睛 学习了一些python的语法点已经一些小技巧 又再次复习了以下知识点 Task01: 线性回归 import torch from torch import nn import numpy as np torch . manual_seed ( 1 ) print ( torch . __version__ ) torch . set_default_tensor_type ( 'torch.FloatTensor' ) # 生成数据 num_inputs = 2 num_examples = 1000 true_w = [ 2 , - 3.4 ] true_b = 4.2 features = torch . tensor ( np . random . normal ( 0 , 1 , ( num_examples , num_inputs ) ) , dtype = torch . float ) labels = true_w [ 0 ] * features [ : , 0 ] + true_w [ 1 ] * features [ : , 1 ] + true_b