- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一个 RNN 模型(在 Pytorch 中),它需要几个句子,然后将其分类为 Class 0 或 Class 1。 p>
对于这个问题,我们假设句子的 max_len 为 4,max_amount of time steps 为 5。因此,每个数据点都在表单上(0 是用于填充填充值的值):
x[1] = [
# Input features at timestep 1
[1, 48, 91, 0],
# Input features at timestep 2
[20, 5, 17, 32],
# Input features at timestep 3
[12, 18, 0, 0],
# Input features at timestep 4
[0, 0, 0, 0],
# Input features at timestep 5
[0, 0, 0, 0]
]
y[1] = [1]
当我每个目标只有一个句子时:我只是将每个单词传递到嵌入层,然后传递到 LSTM 或 GRU,但我有点卡住了当我每个目标有一系列句子时该怎么办?
如何构建可以处理句子的嵌入?
最佳答案
xi = [
# Input features at timestep 1
[1, 48, 91, 0],
# Input features at timestep 2
[20, 5, 17, 32],
# Input features at timestep 3
[12, 18, 0, 0],
# Input features at timestep 4
[0, 0, 0, 0],
# Input features at timestep 5
[0, 0, 0, 0]
]
yi = 1
x = torch.tensor([xi, xi])
y = torch.tensor([yi, yi])
print(x.shape)
# torch.Size([2, 5, 4])
print(y.shape)
# torch.Size([2])
然后,x
是输入的批处理。这里batch_size
= 2。
vocab_size = 1000
embed_size = 100
hidden_size = 200
embed = nn.Embedding(vocab_size, embed_size)
# shape [2, 5, 4, 100]
x = embed(x)
# convert x into a batch of sequences
# Reshape into [2, 20, 100]
x = x.view(bs * 5, 4, 100)
wlstm = nn.LSTM(embed_size, hidden_size, batch_first=True)
# get the only final hidden state of each sequence
_, (hn, _) = wlstm(x)
# hn shape [1, 10, 200]
# get the output of final layer
hn = hn[0] # [10, 200]
# Reshape hn into [bs, num_seq, hidden_size]
hn = hn.view(2, 5, 200)
# Pass to another LSTM and get the final state hn
slstm = nn.LSTM(hidden_size, hidden_size, batch_first=True)
_, (hn, _) = slstm(hn) # [1, 2, 200]
# Similarly, get the hidden state of the last layer
hn = hn[0] # [2, 200]
pred_linear = nn.Linear(hidden_size, 1)
# [2, 1]
output = torch.sigmoid(pred_linear(hn))
关于python - 如何在 RNN 中嵌入句子序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60186944/
我使用 tensorflow 实现了一个简单的 RNN 模型来学习时间序列数据的可能趋势并预测 future 值。然而,该模型总是在训练后产生相同的值。实际上,它得到的最佳模型是: y = b. RN
我正在关注this tutorial关于循环神经网络。 这是导入: import tensorflow as tf from tensorflow.examples.tutorials.mnist i
一段时间以来,我一直在苦思冥想,无法弄清楚我在实现这些 RNN 时做错了什么(如果有的话)。为了让你们省去前向阶段,我可以告诉你们这两个实现计算相同的输出,所以前向阶段是正确的。问题出在倒退阶段。 这
我正在用 RNN 练习。我随机创建 5 个整数。如果第一个整数是奇数,则 y 值为 1,否则 y 为 0(因此,只有第一个 x 有效)。问题是,当我运行这个模型时,它不会“学习”:val_loss 和
我正在使用 bidirectional_rnn与 GRUCell但这是一个关于 Tensorflow 中 RNN 的普遍问题。 我找不到如何初始化权重矩阵(输入到隐藏,隐藏到隐藏)。它们是随机初始化的
我正在尝试找出适应开放命名实体识别问题的最佳模型(生物学/化学,因此不存在实体字典,但必须通过上下文来识别它们)。 目前我最好的猜测是调整 Syntaxnet,这样它就不会将单词标记为 N、V、ADJ
我正在通过以下方式训练 RNN: def create_rnn_model(stateful,length): model = Sequential() model.add(Simpl
我对 PyTorch 非常陌生,而且对一般神经网络也相当陌生。 我试图构建一个可以猜测性别名字的神经网络,并且基于判断国籍的 PyTorch RNN 教程。 我的代码运行没有错误,但损失几乎没有变化,
我正在尝试训练一个模型,返回单词序列(RNN)的类。 我为我的模型提供一系列嵌入: [ batchSize, sequence_length, word_embedding ] as float[]
我正在尝试预测输入向量中每个数字的类别。有3个类(class)。如果输入值从 0 变为 1,则为 1 类。如果从 1 变为 0,则为 2 类。否则为 0 类。 在第二个纪元之后,精度停留在 0.882
我正在尝试为我的日志分析项目开发一个顺序 RNN。 输入是一个日志序列,例如 [1,2,3,4,5,6,1,5,2,7,8,2,1] 目前我正在使用 keras 库中的 to_categorical
为了加深我对 RNN 和 LSTM 的理解,我正在尝试实现一个简单的 LSTM 来估计正弦波的频率和相位。事实证明,这出奇地难以收敛。 MSE 相当高(以千为单位)唯一似乎有点工作的是,如果我生成所有
请帮助我编写以下代码,当我尝试直接在数据上拟合模型时,该代码运行良好,但在网格搜索上失败(我已注释掉直接 model.fit()我的 grid.fit() 语句末尾的部分,这给了我满意的结果。还请告诉
前提1: 关于 RNN 层中的神经元 - 我的理解是,在“每个时间步,每个神经元都接收输入向量 x (t) 和前一个时间步的输出向量 y (t –1)”[1]: 前提2: 据我了解,在 Pytorch
我想训练一个 RNN 来解决一个简单的回归问题。我有一个形状为 (35584,) 的数组 X_train,它表示几年来每小时的测量值。我还有相应的 Y_train 形状为 (35584,) 作为预期值
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 4 年前。 Improve this qu
pytorch实现变长输入的rnn分类 输入数据是长度不固定的序列数据,主要讲解两个部分 1、Data.DataLoader的collate_fn用法,以及按batch进行padding数据
基于循环神经网络(RNN)的古诗生成器,具体内容如下 之前在手机百度上看到有个“为你写诗”功能,能够随机生成古诗,当时感觉很酷炫= = 在学习了深度学习后,了解了一下原理,打算自己做个实现练练手
使用循环神经网络(RNN)实现影评情感分类 作为对循环神经网络的实践,我用循环神经网络做了个影评情感的分类,即判断影评的感情色彩是正面的,还是负面的。 选择使用RNN来做情感分类,主要是因为影评
我的目标是在 Keras/TensorFlow 中构建一个 RNN,它由循环单元层(GRU、LSTM 等)以及从网络底部到顶部的循环组成,以添加注意力机制或特殊的内存类型。我不熟悉符号循环,所以首先我
我是一名优秀的程序员,十分优秀!