Example:

复制代码
import torch
import torch.nn as nn
import torch.nn.functional as F

class FCC(nn.Module):
    def __init__(self,input_dim,hidden_dim,output_dim):
        super(FCC, self).__init__()
        self.linear1 = nn.Linear(input_dim,hidden_dim)
        self.linear2 = nn.Linear(hidden_dim,output_dim)
        self.Dropout = nn.Dropout(p=0.8)

        self.dropout = 0.8
        self.training = True

    def forward(self, input):
        print("input = ",input)
        input = F.dropout(input, self.dropout, self.training)
        print("input1 = ", input)
        out = self.linear1(input)
        out = F.dropout(out, self.dropout, self.training)
        print("out1 = ", out)
        out = self.linear2(out)
        out = self.Dropout(out)
        print("out2 = ", out)


input = torch.randint(1,4,(5,4))
model = FCC(4,3,2)
model(input)
复制代码

输出:

复制代码
input =  tensor([[2., 3., 1., 1.],
        [2., 1., 1., 2.],
        [1., 1., 1., 3.],
        [2., 3., 1., 3.],
        [3., 1., 1., 3.]])
input1 =  tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 5., 0., 0.]])
out1 =  tensor([[ 0.0000, -0.0000,  0.0000],
        [ 0.8460, -0.0000,  0.0000],
        [ 0.0000, -0.0000,  1.0678],
        [ 0.8460, -0.0000,  0.0000],
        [ 0.0000,  0.0000,  0.0000]], grad_fn=<DropoutBackward>)
out2 =  tensor([[ 2.6848, -0.0000],
        [ 0.0000, -0.0000],
        [-0.0000, -1.7823],
        [ 0.0000, -0.0000],
        [ 0.0000, -0.0000]], grad_fn=<DropoutBackward>)
复制代码

 

posted @   别关注我了,私信我吧  阅读(246)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
Live2D
点击右上角即可分享
微信分享提示