<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def apply_transform(sample, transform_matrix, channel_axis=0, fill_mode='nearest', cval=0.):
 """Apply the image transformation specified by a matrix. # Arguments sample: 2D numpy array, single sample. transform_matrix: Numpy array specifying the geometric transformation. channel_axis: Index of axis for channels in the input tensor. fill_mode: Points outside the boundaries of the input are filled according to the given mode (one of `{'constant', 'nearest', 'reflect', 'wrap'}`). cval: Value used for points outside the boundaries of the input if `mode='constant'`. # Returns The transformed version of the input. """

    if sample.ndim == 4:
        channel_axis = channel_axis - 1
        transformed_frames = [transform(frame, transform_matrix, channel_axis, fill_mode, cval) for frame in sample]
        return np.stack(transformed_frames, axis=0)

    if sample.ndim == 3:
        return transform(sample, transform_matrix, channel_axis, fill_mode, cval)