U ™PÓh,ã@sˆddlmZddlmZddlZddlmZddlmZddl m Z ddgZ dd d d d œd d„Z Gdd„dej ƒZGdd„dejƒZdS)é)Ú annotations)ÚSequenceN©ÚADN)ÚActÚFullyConnectedNetÚVarFullyConnectedNetútuple | str | Noneútuple | str | float | Noneú str | Noner)ÚactÚdropoutÚorderingÚreturncCs"|rt||d|dSt||ddS)Né)r r Ú dropout_dimr)r r rr)r r r©rúZ/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/nets/fullyconnectednet.pyÚ_get_adn_layersrc sTeZdZdZdejddfddddddd d d œ‡fd d „ Zdddddœdd„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Úintú Sequence[int]r r Úboolr ÚNone)Ú in_channelsÚ out_channelsÚhidden_channelsr r ÚbiasÚ adn_orderingrc s’tƒ ¡||_||_t|ƒ|_||_||_||_|  dt   ¡¡|j}t |ƒD]&\} } |  d| |  || |¡¡| }qP|  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.Sequential©rrrrcCs(t t |||¡t|j|j|jƒ¡}|S©N)r$Ú Sequentialr(rr r r©r)rrrÚseqrrrr'Ts  ÿzFullyConnectedNet._get_layer) Ú__name__Ú __module__Ú __qualname__Ú__doc__rÚPRELUr!r'Ú __classcell__rrr-rrsø"c sžeZdZdZdejddfddddddddd d d œ ‡fd d „ Zdddddœdd„Zdddœdd„Zd ddddœdd„Z ddddœdd„Z dddœdd„Z ‡Z S)!ra¼ 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 r rr r) rrÚ latent_sizeÚencode_channelsÚdecode_channelsr r rrrc sþtƒ ¡||_||_||_t ¡|_t ¡|_t  ¡|_ t ||| ƒ|_ |j} t |ƒD](\} } |j d| | | | |¡¡| } qVt | |j¡|_t | |j¡|_t |j| ¡|_t |ƒD](\} } |j d| | | | |¡¡| } q¸|j dt | ||¡¡dS)Nz encode_%izdecode%iÚfinal)r r!rrr;r$r2Ú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/r0cCs&t t |||¡¡}| d|j¡|S)Nr)r$r2r(r#rAr3rrrr'˜szVarFullyConnectedNet._get_layerz torch.Tensorz!tuple[torch.Tensor, torch.Tensor])ÚxrcCs0| |¡}| |¡}| |¡}| |¡}||fSr1)r?rrBrC)r)rErBrCrrrÚencode_forwards     z#VarFullyConnectedNet.encode_forward)ÚzÚ use_sigmoidrcCs:| |¡}t |¡}| |¡}| |¡}|r6t |¡}|Sr1)rDÚtorchÚrelurr@Úsigmoid)r)rGrHrErrrÚdecode_forward¤s     z#VarFullyConnectedNet.decode_forward)rBrCrcCs.t d|¡}|jr$t |¡ |¡}| |¡S)Ngà?)rIÚexpÚtrainingÚ randn_likeÚmulÚadd_)r)rBrCÚstdrrrÚreparameterize®sz#VarFullyConnectedNet.reparameterizez=tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]cCs,| |¡\}}| ||¡}| |¡|||fSr1)rFrSrL)r)rErBrCrGrrrÚforward¶s zVarFullyConnectedNet.forward)T) r5r6r7r8rr9r!r'rFrLrSrTr:rrr-rr[sö&& )Ú __future__rÚcollections.abcrrIÚtorch.nnr$Zmonai.networks.blocksrÚmonai.networks.layers.factoriesrÚ__all__rr2rÚModulerrrrrÚ s     <