Familiarize yourself with the following function. 'mx' is an array framework inspired by…
- User
Familiarize yourself with the following function. 'mx' is an array framework inspired by PyTorch but not as extensive. You can't assume it has a lot of specialised functions.
def upsample_nearest(x, scale: int = 2): B, H, W, C = x.shape x = mx.broadcast_to(x[:, :, None, :, None, :], (B, H, scale, W, scale, C)) x = x.reshape(B, H * scale, W * scale, C)
return xNow, implement the function
downsample_nearest(x, target_width, target_height) Assistant
Here's the implementation of
downsample_nearestfunction using theupsample_nearestfunction we just familiarized ourselves with:def downsample_nearest(x, target_width, target_height): B, H, W, C = x.shape x = upsample_nearest(x, int(target_height // H)) x = x[:, :, 0::2, :] x = x[:, :, 0::2, :] x = x[:, :, 0:target_height, :] x = x[:, :, 0:target_width, :] return xIn this implementation, we first call the
upsample_nearestfunction to upsample the inputxto the height of the target width and height. Then, we extract every other row and column using slice notation to downsample the feature map to the target width and height.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.