jupyter操作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 迭代的次数,每次计算损失函数,反向回归,优化参数,得出新的预测值,再计算损失函数

python 复制代码
input_dim = 1
hidden_dim =72
num_layers = 3
output_dim = 1
num_epochs = 100
# LSTM 模型定义
class LSTM(nn.Module):
    def __init__(self, input_dim, hidden_dim,num_layers,output_dim):
        super(LSTM, self).__init__()
        self.hidden_dim = hidden_dim
        self.num_layers =num_layers
        self.lstm = nn.LSTM(input_dim, hidden_dim,num_layers,batch_first=True)
        # 全连接层
        self.fc = nn.Linear(hidden_dim, output_dim)

    def forward(self, x):
        h0=torch.zeros(self.num_layers,x.size(0),self.hidden_dim).requires_grad_()
        c0=torch.zeros(self.num_layers,x.size(0),self.hidden_dim).requires_grad_()
        out,(hn,cn)=self.lstm(x,(h0.detach(),c0.detach()))
        out = self.fc(out[:,-1,:])
        return out
python 复制代码
model = LSTM(input_dim=input_dim,hidden_dim=hidden_dim,output_dim=output_dim,num_layers=num_layers)
criterion = torch.nn.MSELoss()
optimiser = torch.optim.Adam(model.parameters(),lr=0.01)
hist = np.zeros(num_epochs)
python 复制代码
import time
hist = np.zeros(num_epochs)
start_time = time.time()
lstm=[]
for t in range(num_epochs):
    y_train_pred = model(x_train)
    loss = criterion(y_train_pred,y_train_lstm)
    print('EPOCH',t,'MSE',loss.item())
    hist[t]=loss.item()
    optimiser.zero_grad()
    loss.backward()
    optimiser.step()
    
training_time = time.time()-start_time
print(training_time)

4.词向量模型理解

相关推荐
奈斯。zs16 分钟前
yjs08——矩阵、数组的运算
人工智能·python·线性代数·矩阵·numpy
Melody205016 分钟前
tensorflow-dataset 内网下载 指定目录
人工智能·python·tensorflow
学步_技术17 分钟前
Python编码系列—Python抽象工厂模式:构建复杂对象家族的蓝图
开发语言·python·抽象工厂模式
Narutolxy1 小时前
Python 单元测试:深入理解与实战应用20240919
python·单元测试·log4j
Amo Xiang1 小时前
2024 Python3.10 系统入门+进阶(十五):文件及目录操作
开发语言·python
liangbm31 小时前
数学建模笔记——动态规划
笔记·python·算法·数学建模·动态规划·背包问题·优化问题
B站计算机毕业设计超人2 小时前
计算机毕业设计Python+Flask微博情感分析 微博舆情预测 微博爬虫 微博大数据 舆情分析系统 大数据毕业设计 NLP文本分类 机器学习 深度学习 AI
爬虫·python·深度学习·算法·机器学习·自然语言处理·数据可视化
羊小猪~~2 小时前
深度学习基础案例5--VGG16人脸识别(体验学习的痛苦与乐趣)
人工智能·python·深度学习·学习·算法·机器学习·cnn
waterHBO4 小时前
python 爬虫 selenium 笔记
爬虫·python·selenium
编程零零七5 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql