在Pytorch中实现TensorFlow LSTM模型

发布于 2025-02-06 03:21:16 字数 3091 浏览 2 评论 0原文

我正在尝试在Pytorch中实现以下模型(来自Coursera)。该模型用于时间序列数据,其滑动窗口为30。输出是一个单个值,代表序列中下一个中的内容。 Coursera在Pytorch中实现的TensorFlow模型

from tensorflow.keras.layers import Conv1D, LSTM, Dense, Lambda
from tensorflow.keras.models import Sequential

(window_size, batch_size, shuffle_buffer_size) = (30,32,1000)

model =  Sequential([
    Conv1D(filters = 64, kernel_size = 3, strides = 1, activation = "relu", padding = "causal", input_shape = [window_size, 1]),
    LSTM(64, return_sequences = True),
    LSTM(64),
    Dense(30, activation = "relu"),
    Dense(30, activation = "relu"),
    Dense(1),
    Lambda(lambda x: x * 400)
])

(到目前为止我已经做了)。在Pytorch中,LSTM输出一个值序列(len(window_size)),然后将最后一个选择作为预测(out [-1])。但是,在这种情况下,我不太确定如何输入从LSTM到完全连接的层的值。另外,如果您能告诉我到目前为止我所做的事情是否正确,那就太好了。谢谢

import torch
import torch.nn as nn
import torch.nn.functional as F

#input data
x = torch.rand((32,30), dtype = torch.float32).unsqueeze(0)

class LSTM(nn.Module):
    def __init__(self):
        super(LSTM,self).__init__():
        #in : (batch, sequence, channel) ---> Out : (batch, channel, sequence) 
        #in shape (1,32,30) ---> out shape (1,64,32)
        self.conv = nn.Conv1d(in_channels = 32, out_channels = 64, kernel_size = 3, stride = 1, padding = 2)

        #in : (batch, sequence, input_size/features) ---> Out : (batch, sequence, channel)
        #in shape (1,64,32) ---> out shape (1,32,64)
        self.lstm1 = nn.LSTM(input_size = 64, hidden_size = 64, batch_first = True)

        #in : (batch, sequence, input_size/features) ---> Out : (batch, sequence, channel)
        #in shape (1,32,64) ---> out shape (1,32,64)
        self.lstm2 = nn.LSTM(input_size = 64, hidden_size = 64, batch_first = True)

        # ---------------------------------------------------- ??? ------------------------------------------------
        #in : (*, in_features) ---> out : (*, out_features)
        #in shape (1,32,64) ---> out shape (1,???) - NOT SURE
        # NOT SURE WHAT TO INPUT AS THE in_features
        self.fc1 = nn.Linear(in_features = 32 * 64, out_features = 30)
        # ---------------------------------------------------- --- -----------------------------------------------
        
        #in : (*, in_features) ---> out : (*, out_features)
        self.fc2 = nn.Linear(in_features = 30, out_features = 30)

        #in : (*, in_features) ---> out : (*, out_features)
        self.fc3 = nn.Linear(in_features = 30, out_features = 1)

    def forward(self,x):
        out = self.conv(x)
        out,ht1 = self.LSTM(out.permute(0,2,1)
        out,ht2 = self.LSTM(out)
        # ---------------------------------------------- ??? --------------------------------------------------------
        # AGAIN THIS WHERE I AM NOT SURE, BUT GENERALLY YOU UNROLL THE TENSOR BEFORE FEEDING INTO FC-LAYER
        out = out.view(-1,64)
        out = F.relu(self.fc1(out))
        # -------------------------------------------------------------------------------------------------------
        # ...

I am trying to implement the following model (from coursera) in pytorch. The model is used on time series data with a sliding window of 30. The output is a single value that represents what comes in the next in the sequence. The Tensorflow Model from coursera

from tensorflow.keras.layers import Conv1D, LSTM, Dense, Lambda
from tensorflow.keras.models import Sequential

(window_size, batch_size, shuffle_buffer_size) = (30,32,1000)

model =  Sequential([
    Conv1D(filters = 64, kernel_size = 3, strides = 1, activation = "relu", padding = "causal", input_shape = [window_size, 1]),
    LSTM(64, return_sequences = True),
    LSTM(64),
    Dense(30, activation = "relu"),
    Dense(30, activation = "relu"),
    Dense(1),
    Lambda(lambda x: x * 400)
])

Implementation of model in pytorch (what I have done so far). In PyTorch, LSTM outputs a sequence of values (len(window_size)) and you select the last one as the prediction (out[-1]). However, I am not quite sure how to input the values from LSTM to the fully-connected layers in this instance. Also, it would be nice if you can tell me if what I have done so far is correct. Thanks

import torch
import torch.nn as nn
import torch.nn.functional as F

#input data
x = torch.rand((32,30), dtype = torch.float32).unsqueeze(0)

class LSTM(nn.Module):
    def __init__(self):
        super(LSTM,self).__init__():
        #in : (batch, sequence, channel) ---> Out : (batch, channel, sequence) 
        #in shape (1,32,30) ---> out shape (1,64,32)
        self.conv = nn.Conv1d(in_channels = 32, out_channels = 64, kernel_size = 3, stride = 1, padding = 2)

        #in : (batch, sequence, input_size/features) ---> Out : (batch, sequence, channel)
        #in shape (1,64,32) ---> out shape (1,32,64)
        self.lstm1 = nn.LSTM(input_size = 64, hidden_size = 64, batch_first = True)

        #in : (batch, sequence, input_size/features) ---> Out : (batch, sequence, channel)
        #in shape (1,32,64) ---> out shape (1,32,64)
        self.lstm2 = nn.LSTM(input_size = 64, hidden_size = 64, batch_first = True)

        # ---------------------------------------------------- ??? ------------------------------------------------
        #in : (*, in_features) ---> out : (*, out_features)
        #in shape (1,32,64) ---> out shape (1,???) - NOT SURE
        # NOT SURE WHAT TO INPUT AS THE in_features
        self.fc1 = nn.Linear(in_features = 32 * 64, out_features = 30)
        # ---------------------------------------------------- --- -----------------------------------------------
        
        #in : (*, in_features) ---> out : (*, out_features)
        self.fc2 = nn.Linear(in_features = 30, out_features = 30)

        #in : (*, in_features) ---> out : (*, out_features)
        self.fc3 = nn.Linear(in_features = 30, out_features = 1)

    def forward(self,x):
        out = self.conv(x)
        out,ht1 = self.LSTM(out.permute(0,2,1)
        out,ht2 = self.LSTM(out)
        # ---------------------------------------------- ??? --------------------------------------------------------
        # AGAIN THIS WHERE I AM NOT SURE, BUT GENERALLY YOU UNROLL THE TENSOR BEFORE FEEDING INTO FC-LAYER
        out = out.view(-1,64)
        out = F.relu(self.fc1(out))
        # -------------------------------------------------------------------------------------------------------
        # ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文