数据算法 微服务 arrays hyperlink Semantic UI vue插件 react脚手架搭建 angular视频教程 web前端开发实战项目 java后台框架 matlab四舍五入 docker启动命令 python随机函数 pythonassert函数 python命令行参数 配置python环境 javastring类型 java遍历 java中接口的定义 js判断字符串相等 ad19 思源字体 linux运维之道 迅雷会员共享账号 坐标标注插件 文明6万神殿 jdk9下载 脚本学习 保留两位小数的函数 软媒u盘启动 php单例模式 cad合并成块 wma转换mp3 文字底纹怎么设置 php苹果动态锁屏 python怎么用 css世界 kutools c4d怎么改成中文 js取余
当前位置: 首页 > 学习教程  > 编程语言

GRUNet模块的输入存在问题,Expect input_dim 64,but got 5。

2021/1/13 19:35:11 文章标签: 测试文章如有侵权请发送至邮箱809451989@qq.com投诉后文章立即删除

问题描述: GRUNet模块的输入存在问题,Expect input_dim 64,but got 5。 class GRUNet(nn.Module):def __init__(self, input_dim, output_dim, drop_out, n_layersNone):super(GRUNet, self).__init__()# seq_len: 输入序列时间分布上的长度self.seq_l…

问题描述:

GRUNet模块的输入存在问题,Expect input_dim 64,but got 5。

class GRUNet(nn.Module):
    def __init__(self, input_dim, output_dim, drop_out, n_layers=None):
        super(GRUNet, self).__init__()
        # seq_len: 输入序列时间分布上的长度
        self.seq_len = 0
        if n_layers is None:
            n_layers = 2
        self.gru = nn.GRU(input_dim, output_dim, n_layers, dropout=drop_out if n_layers != 1 else 0, batch_first=True)

        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        # x的格式(batch,seq,feature)
        output, hidden = self.gru(x)
        print(output.size())
        self.seq_len = output.size(1)

        norm = nn.BatchNorm1d(self.seq_len)
        x = norm(x)
        print(x)
        x = self.relu(x)
        return x

class Resblock(nn.Module):
    def __init__(self, input_dim, drop_out, n_layers=None, hidden_dim=None):
        super(Resblock, self).__init__()
        if n_layers is None:
            n_layers = [1, 3]
        if hidden_dim is None:
            hidden_dim = [input_dim * 2, input_dim * 4]

        # self.resblock_1 = GRUNet(input_dim, hidden_dim[0], drop_out if n_layers[0] != 1 else 0, n_layers[0])
        # self.resblock_2 = GRUNet(input_dim, hidden_dim[0], drop_out if n_layers[0] != 1 else 0, n_layers[0])
        # 问题在这里
        self.resblock = nn.Sequential(
            GRUNet(input_dim, hidden_dim[0], drop_out if n_layers[0] != 1 else 0, n_layers[0]),
            GRUNet(hidden_dim[0], hidden_dim[1], drop_out if n_layers[1] != 1 else 0, n_layers[1])
        )

        if input_dim != hidden_dim[1]:
            self.unify = GRUNet(input_dim, hidden_dim[1], drop_out if n_layers[0] != 1 else 0)
        else:
            # 通道数相同,无需做变换,在forward中identity = x
            self.unify = None

        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        identity = x
        x = self.resblock_1(x)
        x = self.resblock_2(x)
        if self.unify is not None:
            identity = self.unify(identity)
        x += identity
        x = self.relu(x)
        return x

原因分析:

 # 问题在这里
        self.resblock = nn.Sequential(
            GRUNet(input_dim, hidden_dim[0], drop_out if n_layers[0] != 1 else 0, n_layers[0]),
            GRUNet(hidden_dim[0], hidden_dim[1], drop_out if n_layers[1] != 1 else 0, n_layers[1])
        )

主要原因我觉得在于,self.resblock中定义了两个实例,我们在初始化实例的时候用了相同的类,这两个实例实际上只有一个初始化的值,所以导致了后面问题中的输入不匹配问题。

解决方案:

class GRUNet(nn.Module):
    def __init__(self, input_dim, output_dim, drop_out, n_layers=None):
        super(GRUNet, self).__init__()
        # seq_len: 输入序列时间分布上的长度
        self.seq_len = 0
        if n_layers is None:
            n_layers = 2
        self.gru = nn.GRU(input_dim, output_dim, n_layers, dropout=drop_out if n_layers != 1 else 0, batch_first=True)

        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        # x的格式(batch,seq,feature)
        output, hidden = self.gru(x)
        print(output.size())
        self.seq_len = output.size(1)

        norm = nn.BatchNorm1d(self.seq_len)
        x = norm(x)
        print(x)
        x = self.relu(x)
        return x


# ---------------------------------------------------#
#   内部堆叠的残差块
# ---------------------------------------------------#
class Resblock(nn.Module):
    def __init__(self, input_dim, drop_out, n_layers=None, hidden_dim=None):
        super(Resblock, self).__init__()
        if n_layers is None:
            n_layers = [1, 3]
        if hidden_dim is None:
            hidden_dim = [input_dim * 2, input_dim * 4]

        self.resblock_1 = GRUNet(input_dim, hidden_dim[0], drop_out if n_layers[0] != 1 else 0, n_layers[0])
        self.resblock_2 = GRUNet(input_dim, hidden_dim[0], drop_out if n_layers[0] != 1 else 0, n_layers[0])

        if input_dim != hidden_dim[1]:
            self.unify = GRUNet(input_dim, hidden_dim[1], drop_out if n_layers[0] != 1 else 0)
        else:
            # 通道数相同,无需做变换,在forward中identity = x
            self.unify = None

        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        identity = x
        x = self.resblock_1(x)
        x = self.resblock_2(x)
        if self.unify is not None:
            identity = self.unify(identity)
        x += identity
        x = self.relu(x)
        return x

分别实例化这些类


本文链接: http://www.dtmao.cc/news_show_600106.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?