Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yiming-wange
GitHub Repository: yiming-wange/cs224n-2023-solution
Path: blob/main/a3/utils/__pycache__/general_utils.cpython-310.pyc
1004 views
o

a��cy	�@s*dZddlZd
dd�Zdd�Zdd	�ZdS)zp
CS224N 2021-2022: Homework 3
general_utils.py: General purpose utilities.
Sahil Chopra <[email protected]>
�NTc#s��t|�tuot|d�tupt|d�tju}|r t|d�nt|�}t�|�}|r1tj�|�t�d||�D]}||||��|rM�fdd�|D�nt|��Vq8dS)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.

    rcsg|]}t|���qS�)�
_minibatch)�.0�d�Zminibatch_indicesr�E/Users/yimingwang/Desktop/cs224n/assignment/a3/utils/general_utils.py�
<listcomp>.sz#get_minibatches.<locals>.<listcomp>N)	�type�list�np�ndarray�len�arange�random�shuffler)�dataZminibatch_sizerZ	list_data�	data_size�indicesZminibatch_startrrr�get_minibatchess�.
��rcs(t��tjur�|S�fdd�|D�S)Ncsg|]}�|�qSrr)r�i�rrrr3sz_minibatch.<locals>.<listcomp>)r	rr)rZ
minibatch_idxrrrr2s(rcCsZ|j|jkrtd�||j|j���t�t�||��dkr&td�|||���t|d�dS)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�expectedrrr�test_all_close6s�r )T)�__doc__�numpyrrrr rrrr�<module>s

&