Path: blob/main/a3/utils/__pycache__/general_utils.cpython-310.pyc
3764 views
o
a��cy � @ s* d Z ddlZd
dd�Zdd� Zdd � ZdS )zp
CS224N 2021-2022: Homework 3
general_utils.py: General purpose utilities.
Sahil Chopra <[email protected]>
� NTc # s� � t | �tu ot | d �tu pt | d �tju }|r t| d �nt| �}t�|�}|r1tj�|� t�d||�D ]}|||| � � |rM� fdd�| D �nt| � �V q8dS )a0
Iterates through the provided data one minibatch at at time. You can use this function to
iterate through data in minibatches as follows:
for inputs_minibatch in get_minibatches(inputs, minibatch_size):
...
Or with multiple data sources:
for inputs_minibatch, labels_minibatch in get_minibatches([inputs, labels], minibatch_size):
...
Args:
data: there are two possible values:
- a list or numpy array
- a list where each element is either a list or numpy array
minibatch_size: the maximum number of items in a minibatch
shuffle: whether to randomize the order of returned data
Returns:
minibatches: the return value depends on data:
- If data is a list/array it yields the next minibatch of data.
- If data a list of lists/arrays it returns the next minibatch of each element in the
list. This can be used to iterate through multiple data sources
(e.g., features and labels) at the same time.
r c s g | ]}t |� ��qS � )�
_minibatch)�.0�d�Zminibatch_indicesr �E/Users/yimingwang/Desktop/cs224n/assignment/a3/utils/general_utils.py�
<listcomp>. s z#get_minibatches.<locals>.<listcomp>N) �type�list�np�ndarray�len�arange�random�shuffler )�dataZminibatch_sizer Z list_data� data_size�indicesZminibatch_startr r r �get_minibatches s �.
��r c s( t � �tju r� | S � fdd�|D �S )Nc s g | ]}� | �qS r r )r �i�r r r r 3 s z_minibatch.<locals>.<listcomp>)r r r )r Z
minibatch_idxr r r r 2 s (r c C sZ |j |j krtd�| |j |j ���t�t�|| ��dkr&td�| ||���t| d� d S )Nz?{:} failed, expected output to have shape {:} but has shape {:}g�����ư>z){:} failed, expected {:} but value is {:}zpassed!)�shape�
ValueError�formatr �amax�fabs�print)�name�actual�expectedr r r �test_all_close6 s �r )T)�__doc__�numpyr r r r r r r r �<module> s
&