当前位置:   article > 正文

jupyter操作LSTM模型,词向量模型理解_lstm模型用啥打开

lstm模型用啥打开

1.jupyter没有torch模块,参考下面链接的解决办法

【jupyter notebook安装配置教程,导入pytorch解决No module named torch-哔哩哔哩】 https://b23.tv/jYGvyVR

2.jupyter中没有某一模块怎么办,可以用pycharm打开一个项目,在该项目中下载所需要的模块,然后jupyter notebook打开这个项目,在同路径下打开ipython文件。

3.LSTM模型的输入,输出与与注意事项。

 模型构建,最基本的是五个参数 input_dim 输入维度,即输入的特征的个数 hidden_dim 隐藏层特征的维度 num_layers lstm的连结个数 output_dim 输出层的维度,预测即为1,分类则为分类的个数 num_epochs 迭代的次数,每次计算损失函数,反向回归,优化参数,得出新的预测值,再计算损失函数

  1. input_dim = 1
  2. hidden_dim =72
  3. num_layers = 3
  4. output_dim = 1
  5. num_epochs = 100
  6. # LSTM 模型定义
  7. class LSTM(nn.Module):
  8. def __init__(self, input_dim, hidden_dim,num_layers,output_dim):
  9. super(LSTM, self).__init__()
  10. self.hidden_dim = hidden_dim
  11. self.num_layers =num_layers
  12. self.lstm = nn.LSTM(input_dim, hidden_dim,num_layers,batch_first=True)
  13. # 全连接层
  14. self.fc = nn.Linear(hidden_dim, output_dim)
  15. def forward(self, x):
  16. h0=torch.zeros(self.num_layers,x.size(0),self.hidden_dim).requires_grad_()
  17. c0=torch.zeros(self.num_layers,x.size(0),self.hidden_dim).requires_grad_()
  18. out,(hn,cn)=self.lstm(x,(h0.detach(),c0.detach()))
  19. out = self.fc(out[:,-1,:])
  20. return out
  1. model = LSTM(input_dim=input_dim,hidden_dim=hidden_dim,output_dim=output_dim,num_layers=num_layers)
  2. criterion = torch.nn.MSELoss()
  3. optimiser = torch.optim.Adam(model.parameters(),lr=0.01)
  4. hist = np.zeros(num_epochs)
  1. import time
  2. hist = np.zeros(num_epochs)
  3. start_time = time.time()
  4. lstm=[]
  5. for t in range(num_epochs):
  6. y_train_pred = model(x_train)
  7. loss = criterion(y_train_pred,y_train_lstm)
  8. print('EPOCH',t,'MSE',loss.item())
  9. hist[t]=loss.item()
  10. optimiser.zero_grad()
  11. loss.backward()
  12. optimiser.step()
  13. training_time = time.time()-start_time
  14. print(training_time)

4.词向量模型理解

词向量模型介绍

  1. one-hot缺点
  2. 维度灾难
  3. 无法度量词语之间的相似性
  4. 词向量(用一组向量表示词语)
  5. 重要假设:文本中离得越近的词语相似度越高
  6. 两种计算方法:CBOW skip-gram
  7. 前者用上下文词来预测中心词,通过上下文此计算中心词的词向量与实际值做对比,根据让损失下降来更新权重参数
  8. 后者用中心词来预测上下文此,用的最多
  9. 问题1:需要指定窗口大小,即中心词周围选几个词语来计算
  10. 问题2:优化问题中需要综合考虑靠近中心词的词和非靠近中心词的词,语料库大,非上下文词太大,因此只采样部分非靠近中文词的词,即负样本(negative)
  11. 问题3:设置迭代的次数,以及最小参与计算的词语的频数的阈值
  12. 问题4:指定词向量的维度
  13. 如何评估词向量
  14. 1.输出与特定词语的相关度比较高的词语
  15. 2.可视化
  16. 3.类比实验:国王-王后=男人-女人
  17. 词向量用途
  18. 1.主题聚类
  19. 2.情感分析
  20. 3.信息检索
  21. 词向量缺点
  22. 1.没有考虑多义词
  23. 2.窗口长度有限
  24. 3.没有考虑全局的文本信息
  25. 4.不是严格意义的语序
  26. python处理步骤
  27. 1.读取数据,作分词,得到双列表数据,一个大列表套一个小列表,小列表中为很多个词语
  28. 2.模型训练,自行设定词向量维度vector_size,窗口大小window,最小过滤词频min_count,迭代次数epochs,负采样个数negative
  29. 3.模型评估

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/794566
推荐阅读
相关标签
  

闽ICP备14008679号