o
    -i,  ã                   @  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„ZG dd„ dejƒZG dd„ dejƒZdS )é    )Úannotations)ÚSequenceN©ÚADN)ÚActÚFullyConnectedNetÚVarFullyConnectedNetÚactútuple | str | NoneÚdropoutútuple | str | float | NoneÚorderingú
str | NoneÚreturnr   c                 C  s"   |r
t | |d|dS t | |ddS )Né   )r	   r   Údropout_dimr   )r	   r   r   r   )r	   r   r   © r   úg/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/nets/fullyconnectednet.pyÚ_get_adn_layer   s   r   c                      s8   e Zd ZdZdejddfd‡ fdd„Z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Úin_channelsÚintÚout_channelsÚhidden_channelsúSequence[int]r   r   r	   r
   ÚbiasÚboolÚadn_orderingr   r   ÚNonec              	     s’   t ƒ  ¡  || _|| _t|ƒ| _|| _|| _|| _|  	dt
 ¡ ¡ | j}t|ƒD ]\}	}
|  	d|	 |  ||
|¡¡ |
}q(|  	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.Sequentialc                 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)r   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   ©r   r   r   r   r   r   r   r/   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚPRELUr!   r'   Ú__classcell__r   r   r-   r   r      s    øc                      sb   e Zd ZdZdejddfd*‡ fdd„Zd+dd„Zd,dd„Zd-d.d!d"„Z	d/d%d&„Z
d0d(d)„Z‡  ZS )1r   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   Úlatent_sizeÚencode_channelsr   Údecode_channelsr   r   r	   r
   r   r   r   r   r   r   c
              	     sþ   t ƒ  ¡  || _|| _|| _t ¡ | _t ¡ | _t 	¡ | _
t|||	ƒ| _| j}
t|ƒD ]\}}| j d| |  |
||¡¡ |}
q+t |
| 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$   r1   Ú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/   c                 C  s&   t  t  |||¡¡}| d| j¡ |S )Nr   )r$   r1   r(   r#   rA   r2   r   r   r   r'   ˜   s   zVarFullyConnectedNet._get_layerÚxútorch.Tensorú!tuple[torch.Tensor, torch.Tensor]c                 C  s0   |   |¡}|  |¡}|  |¡}|  |¡}||fS r0   )r?   r   rB   rC   )r)   rE   rB   rC   r   r   r   Úencode_forward   s
   



z#VarFullyConnectedNet.encode_forwardÚzÚuse_sigmoidc                 C  s:   |   |¡}t |¡}|  |¡}|  |¡}|rt |¡}|S r0   )rD   ÚtorchÚrelur   r@   Úsigmoid)r)   rI   rJ   rE   r   r   r   Údecode_forward¤   s   




z#VarFullyConnectedNet.decode_forwardrB   rC   c                 C  s.   t  d| ¡}| jrt  |¡ |¡}| |¡S )Ng      à?)rK   ÚexpÚtrainingÚ
randn_likeÚmulÚadd_)r)   rB   rC   Ústdr   r   r   Úreparameterize®   s   
z#VarFullyConnectedNet.reparameterizeú=tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]c                 C  s,   |   |¡\}}|  ||¡}|  |¡|||fS r0   )rH   rU   rN   )r)   rE   rB   rC   rI   r   r   r   Úforward¶   s   zVarFullyConnectedNet.forward)r   r   r   r   r;   r   r<   r   r=   r   r   r   r	   r
   r   r   r   r   r   r   r4   )rE   rF   r   rG   )T)rI   rF   rJ   r   r   rF   )rB   rF   rC   rF   r   rF   )rE   rF   r   rV   )r5   r6   r7   r8   r   r9   r!   r'   rH   rN   rU   rW   r:   r   r   r-   r   r   [   s    ö
&


)r	   r
   r   r   r   r   r   r   )Ú
__future__r   Úcollections.abcr   rK   Útorch.nnr$   Úmonai.networks.blocksr   Úmonai.networks.layers.factoriesr   Ú__all__r   r1   r   ÚModuler   r   r   r   r   Ú<module>   s   
<