Path: blob/main/a4/__pycache__/nmt_model.cpython-310.pyc
3764 views
o
J�"dr � @ s� d Z ddlmZ ddlZddlmZmZmZmZm Z ddl
Z
ddlmZ ddl
Z
ddlm mZ ddlmZmZ ddlmZ eddd g�ZG d
d� dej�ZdS )z�
CS224N 2022-23: Homework 4
nmt_model.py: NMT Model
Pencheng Yin <[email protected]>
Sahil Chopra <[email protected]>
Vera Lin <[email protected]>
Siyan Li <[email protected]>
� )�
namedtupleN)�List�Tuple�Dict�Set�Union)�pad_packed_sequence�pack_padded_sequence)�ModelEmbeddings�
Hypothesis�value�scorec sh e Zd ZdZd*� fdd� Zdeee deee dejfdd �Z d
ejdee
deejeejejf f fdd
�Zdejdejdeejejf dejdejf
dd�Z
dejdeejejf dejdejdejdeeejejf fdd�Zdejdee
dejfdd�Zd+dee de
de
dee fd d!�Zedejfd"d#��Zed$efd%d&��Zd'efd(d)�Z� ZS ),�NMTz� Simple Neural Machine Translation Model:
- Bidrectional LSTM Encoder
- Unidirection LSTM Decoder
- Global Attention Model (Luong, et al. 2015)
皙�����?c s: t t| ��� t||�| _|| _|| _|| _d| _d| _ d| _
d| _d| _d| _
d| _d| _d| _d| _tj||ddd�| _tj|| jdd�| _tj|| j | jd �| _ tjd| j | jdd
�| _
tjd| j | jdd
�| _tjd| j | jdd
�| _tjd| j | jdd
�| _
t�t| jj�| j�| _t�| j�| _dS )a� Init NMT Model.
@param embed_size (int): Embedding size (dimensionality)
@param hidden_size (int): Hidden Size, the size of hidden states (dimensionality)
@param vocab (Vocab): Vocabulary object containing src and tgt languages
See vocab.py for documentation.
@param dropout_rate (float): Dropout probability, for attention
NFr � �same)�kernel_size�paddingT)�
input_size�hidden_size�
bidirectional)r r )�bias� )�superr �__init__r
�model_embeddingsr �dropout_rate�vocab�encoder�decoder�h_projection�c_projection�att_projection�combined_output_projection�target_vocab_projection�dropout�gen_sanity_check�counter�nn�Conv1d�post_embed_cnn�LSTM�LSTMCell�Linear�len�tgt�Dropout)�self�
embed_sizer r r �� __class__� �;/Users/yimingwang/Desktop/cs224n/assignment/a4/nmt_model.pyr ! s0 zNMT.__init__�source�target�returnc C s� dd� |D �}| j jj|| jd�}| j jj|| jd�}| �||�\}}| �||�}| �||||�} tj | �
| �dd�}
|| j jd k�� }tj
|
|dd� �d�dd ��d�|dd� }|jd
d�}
|
S )ak Take a mini-batch of source and target sentences, compute the log-likelihood of
target sentences under the language models learned by the NMT system.
@param source (List[List[str]]): list of source sentence tokens
@param target (List[List[str]]): list of target sentence tokens, wrapped by `<s>` and `</s>`
@returns scores (Tensor): a variable/tensor of shape (b, ) representing the
log-likelihood of generating the gold-standard target sentence for
each example in the input batch. Here b = batch size.
c S s g | ]}t |��qS r5 )r. )�.0�sr5 r5 r6 �
<listcomp>l s zNMT.forward.<locals>.<listcomp>��device�������dimz<pad>� N)�indexrA r )r �src�to_input_tensorr>