def _write_numpy_to_dense_tensor()

in src/sagemaker_training/recordio.py [0:0]


def _write_numpy_to_dense_tensor(file, array, labels=None):
    """Writes a numpy array to a dense record.

    Args:
        file (file-like object): File-like object where the
                                 records will be written.
        array (numpy array): Numpy array containing the features.
        labels (numpy array): Numpy array containing the labels.
    """

    # Validate shape of array and labels, resolve array and label types
    if not len(array.shape) == 2:
        raise ValueError("Array must be a Matrix")
    if labels is not None:
        if not len(labels.shape) == 1:
            raise ValueError("Labels must be a Vector")
        if labels.shape[0] not in array.shape:
            raise ValueError(
                "Label shape {} not compatible with array shape {}".format(
                    labels.shape, array.shape
                )
            )
        resolved_label_type = _resolve_type(labels.dtype)
    resolved_type = _resolve_type(array.dtype)

    # Write each vector in array into a Record in the file object
    record = Record()
    for index, vector in enumerate(array):
        record.Clear()
        _write_feature_tensor(resolved_type, record, vector)
        if labels is not None:
            _write_label_tensor(resolved_label_type, record, labels[index])
        _write_recordio(file, record.SerializeToString())