o - i,ã@s~ddlmZddlmZddlZddlmZddlmZddl m Z ddgZ ddd„Z Gdd„dej ƒZGdd„dejƒZdS)é)Ú annotations)ÚSequenceN©ÚADN)ÚActÚFullyConnectedNetÚVarFullyConnectedNetÚactútuple | str | NoneÚdropoutútuple | str | float | NoneÚorderingú str | NoneÚreturnrcCs"|r t||d|dSt||ddS)Né)r r Ú dropout_dimr )r r rr)r r r ©rúg/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/nets/fullyconnectednet.pyÚ_get_adn_layersrcs8eZdZdZdejddfd‡fdd„ Zddd„Z‡ZS)raê Simple full-connected layer neural network composed of a sequence of linear layers with PReLU activation and dropout. The network accepts input with `in_channels` channels, has output with `out_channels` channels, and hidden layer output channels given in `hidden_channels`. If `bias` is True then linear units have a bias term. Args: in_channels: number of input channels. out_channels: number of output channels. hidden_channels: number of output channels for each hidden layer. dropout: dropout ratio. Defaults to no dropout. act: activation type and arguments. Defaults to PReLU. bias: whether to have a bias term in linear units. Defaults to True. adn_ordering: order of operations in :py:class:`monai.networks.blocks.ADN`. Examples:: # accepts 4 values and infers 3 values as output, has 3 hidden layers with 10, 20, 10 values as output net = FullyConnectedNet(4, 3, [10, 20, 10], dropout=0.2) NTÚ in_channelsÚintÚ out_channelsÚhidden_channelsú Sequence[int]r r r r ÚbiasÚboolÚ adn_orderingrrÚNonec s’tƒ ¡||_||_t|ƒ|_||_||_||_|  dt   ¡¡|j}t |ƒD]\} } |  d| |  || |¡¡| }q(|  dt  |||¡¡dS)zê Defines a network accept input with `in_channels` channels, output of `out_channels` channels, and hidden layers with channels given in `hidden_channels`. If `bias` is True then linear units have a bias term. Úflattenz hidden_%iÚoutputN)ÚsuperÚ__init__rrÚlistrr r rÚ add_moduleÚnnÚFlattenÚ enumerateÚ _get_layerÚLinear) Úselfrrrr r rrÚ prev_channelsÚiÚc©Ú __class__rrr!5s  zFullyConnectedNet.__init__ú nn.SequentialcCs(t t |||¡t|j|j|jƒ¡}|S©N)r$Ú Sequentialr(rr r r©r)rrrÚseqrrrr'TsÿzFullyConnectedNet._get_layer)rrrrrrr r r r rrrrrr©rrrrrrrr/) Ú__name__Ú __module__Ú __qualname__Ú__doc__rÚPRELUr!r'Ú __classcell__rrr-rrsøcsbeZdZdZdejddfd*‡fdd„ Zd+dd„Zd,dd„Zd-d.d!d"„Z d/d%d&„Z d0d(d)„Z ‡Z S)1ra¼ Variational fully-connected network. This is composed of an encode layer, reparameterization layer, and then a decode layer. Args: in_channels: number of input channels. out_channels: number of output channels. latent_size: number of latent variables to use. encode_channels: number of output channels for each hidden layer of the encode half. decode_channels: number of output channels for each hidden layer of the decode half. dropout: dropout ratio. Defaults to no dropout. act: activation type and arguments. Defaults to PReLU. bias: whether to have a bias term in linear units. Defaults to True. adn_ordering: order of operations in :py:class:`monai.networks.blocks.ADN`. Examples:: # accepts inputs with 4 values, uses a latent space of 2 variables, and produces outputs of 3 values net = VarFullyConnectedNet(4, 3, 2, [5, 10], [10, 5]) NTrrrÚ latent_sizeÚencode_channelsrÚdecode_channelsr r r r rrrrrrc sþtƒ ¡||_||_||_t ¡|_t ¡|_t  ¡|_ t ||| ƒ|_ |j} t |ƒD]\} } |j d| | | | |¡¡| } q+t | |j¡|_t | |j¡|_t |j| ¡|_t |ƒD]\} } |j d| | | | |¡¡| } q\|j dt | ||¡¡dS)Nz encode_%izdecode%iÚfinal)r r!rrr;r$r1ÚencodeÚdecoder%rrÚ adn_layerr&r#r'r(ÚmuÚlogvarÚdecodeL) r)rrr;r<r=r r rrr*r+r,r-rrr!rs&    zVarFullyConnectedNet.__init__r/cCs&t t |||¡¡}| d|j¡|S)Nr)r$r1r(r#rAr2rrrr'˜szVarFullyConnectedNet._get_layerÚxú torch.Tensorú!tuple[torch.Tensor, torch.Tensor]cCs0| |¡}| |¡}| |¡}| |¡}||fSr0)r?rrBrC)r)rErBrCrrrÚencode_forwards    z#VarFullyConnectedNet.encode_forwardÚzÚ use_sigmoidcCs:| |¡}t |¡}| |¡}| |¡}|rt |¡}|Sr0)rDÚtorchÚrelurr@Úsigmoid)r)rIrJrErrrÚdecode_forward¤s     z#VarFullyConnectedNet.decode_forwardrBrCcCs.t d|¡}|jrt |¡ |¡}| |¡S)Ngà?)rKÚexpÚtrainingÚ randn_likeÚmulÚadd_)r)rBrCÚstdrrrÚreparameterize®s z#VarFullyConnectedNet.reparameterizeú=tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]cCs,| |¡\}}| ||¡}| |¡|||fSr0)rHrUrN)r)rErBrCrIrrrÚforward¶s zVarFullyConnectedNet.forward)rrrrr;rr<rr=rr r r r rrrrrrr4)rErFrrG)T)rIrFrJrrrF)rBrFrCrFrrF)rErFrrV) r5r6r7r8rr9r!r'rHrNrUrWr:rrr-rr[sö &   )r r r r r rrr)Ú __future__rÚcollections.abcrrKÚtorch.nnr$Úmonai.networks.blocksrÚmonai.networks.layers.factoriesrÚ__all__rr1rÚModulerrrrrÚs     <