U
    ™PÓh,  ã                   @  sˆ   d dl mZ d dlmZ d dlZd dlmZ d dlmZ d dl	m
Z
 ddgZdd	d
ddœdd„ZG dd„ dejƒZG dd„ dejƒZdS )é    )Úannotations)ÚSequenceN©ÚADN)ÚActÚFullyConnectedNetÚVarFullyConnectedNetútuple | str | Noneútuple | str | float | Noneú
str | Noner   )ÚactÚdropoutÚorderingÚreturnc                 C  s"   |rt | |d|dS t | |ddS )Né   )r   r   Údropout_dimr   )r   r   r   r   )r   r   r   © r   úZ/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/nets/fullyconnectednet.pyÚ_get_adn_layer   s    r   c                
      sT   e Zd ZdZdejddfddddddd	d
dœ‡ fdd„Zdddddœdd„Z‡  ZS )r   aê  
    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_orderingr   c              	     s’   t ƒ  ¡  || _|| _t|ƒ| _|| _|| _|| _|  	dt
 ¡ ¡ | j}t|ƒD ]&\}	}
|  	d|	 |  ||
|¡¡ |
}qP|  	dt
 |||¡¡ dS )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__r   r   Úlistr   r   r   r   Ú
add_moduleÚnnÚFlattenÚ	enumerateÚ
_get_layerÚLinear)Úselfr   r   r   r   r   r   r   Úprev_channelsÚiÚc©Ú	__class__r   r   r!   5   s    

zFullyConnectedNet.__init__únn.Sequential©r   r   r   r   c                 C  s(   t  t  |||¡t| j| j| jƒ¡}|S ©N)r$   Ú
Sequentialr(   r   r   r   r   ©r)   r   r   r   Úseqr   r   r   r'   T   s
     ÿzFullyConnectedNet._get_layer)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚPRELUr!   r'   Ú__classcell__r   r   r-   r   r      s   ø"c                      sž   e Zd ZdZdejddfddddddddd	d
dœ
‡ fdd„Zdddddœdd„Zdddœdd„Zd ddddœdd„Z	ddddœdd„Z
dddœdd„Z‡  ZS )!r   a¼  
    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])

    NTr   r   r
   r	   r   r   r   )
r   r   Úlatent_sizeÚencode_channelsÚdecode_channelsr   r   r   r   r   c
              	     sþ   t ƒ  ¡  || _|| _|| _t ¡ | _t ¡ | _t 	¡ | _
t|||	ƒ| _| j}
t|ƒD ](\}}| j d| |  |
||¡¡ |}
qVt |
| j¡| _t |
| j¡| _t | j|
¡| _t|ƒD ](\}}| j d| |  |
||¡¡ |}
q¸| j dt |
||¡¡ d S )Nz	encode_%izdecode%iÚfinal)r    r!   r   r   r;   r$   r2   ÚencodeÚdecoder%   r   r   Ú	adn_layerr&   r#   r'   r(   ÚmuÚlogvarÚdecodeL)r)   r   r   r;   r<   r=   r   r   r   r   r*   r+   r,   r-   r   r   r!   r   s&    



zVarFullyConnectedNet.__init__r/   r0   c                 C  s&   t  t  |||¡¡}| d| j¡ |S )Nr   )r$   r2   r(   r#   rA   r3   r   r   r   r'   ˜   s    zVarFullyConnectedNet._get_layerztorch.Tensorz!tuple[torch.Tensor, torch.Tensor])Úxr   c                 C  s0   |   |¡}|  |¡}|  |¡}|  |¡}||fS r1   )r?   r   rB   rC   )r)   rE   rB   rC   r   r   r   Úencode_forward   s
    



z#VarFullyConnectedNet.encode_forward)ÚzÚuse_sigmoidr   c                 C  s:   |   |¡}t |¡}|  |¡}|  |¡}|r6t |¡}|S r1   )rD   ÚtorchÚrelur   r@   Úsigmoid)r)   rG   rH   rE   r   r   r   Údecode_forward¤   s    




z#VarFullyConnectedNet.decode_forward)rB   rC   r   c                 C  s.   t  d| ¡}| jr$t  |¡ |¡}| |¡S )Ng      à?)rI   ÚexpÚtrainingÚ
randn_likeÚmulÚadd_)r)   rB   rC   Ústdr   r   r   Úreparameterize®   s    z#VarFullyConnectedNet.reparameterizez=tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]c                 C  s,   |   |¡\}}|  ||¡}|  |¡|||fS r1   )rF   rS   rL   )r)   rE   rB   rC   rG   r   r   r   Úforward¶   s    zVarFullyConnectedNet.forward)T)r5   r6   r7   r8   r   r9   r!   r'   rF   rL   rS   rT   r:   r   r   r-   r   r   [   s   ö&&
)Ú
__future__r   Úcollections.abcr   rI   Útorch.nnr$   Zmonai.networks.blocksr   Úmonai.networks.layers.factoriesr   Ú__all__r   r2   r   ÚModuler   r   r   r   r   Ú<module>   s   <