Implement InvalidShapeException

This commit is contained in:
2025-11-21 18:54:45 +01:00
parent 6685aa6629
commit c83e1f0c45
6 changed files with 77 additions and 91 deletions

View File

@@ -6,27 +6,39 @@ using namespace CUDANet::Layers;
MaxPool2d::MaxPool2d(
CUDANet::Shape input_shape,
CUDANet::Shape pooling_shape,
CUDANet::Shape pool_shape,
CUDANet::Shape stride_shape,
CUDANet::Shape padding_shape,
CUDANet::Backend* backend
)
: in_shape(input_shape),
pooling_shape(pooling_shape),
pool_shape(pool_shape),
stride_shape(stride_shape),
padding_shape(padding_shape),
backend(backend) {
size_t out_h = (in_shape[0] + 2 * padding_shape[0] - pooling_shape[0]) /
stride_shape[0] +
1;
size_t out_w = (in_shape[1] + 2 * padding_shape[1] - pooling_shape[1]) /
stride_shape[1] +
1;
if (in_shape.size() != 3) {
throw InvalidShapeException("input", 3, in_shape.size());
}
out_shape.resize(3);
out_shape[0] = out_h;
out_shape[1] = out_w;
out_shape[2] = in_shape[2];
if (pool_shape.size() != 2) {
throw InvalidShapeException("pool", 2, pool_shape.size());
}
if (stride_shape.size() != 2) {
throw InvalidShapeException("stride", 2, stride_shape.size());
}
if (padding_shape.size() != 2) {
throw InvalidShapeException("padding", 2, padding_shape.size());
}
out_shape = {
(in_shape[0] + 2 * padding_shape[0] - pool_shape[0]) / stride_shape[0] +
1,
(in_shape[1] + 2 * padding_shape[1] - pool_shape[1]) / stride_shape[1] +
1,
in_shape[2]
};
output = CUDANet::Tensor(
Shape{out_shape[0] * out_shape[1] * out_shape[3]},
@@ -39,7 +51,7 @@ MaxPool2d::~MaxPool2d() {}
CUDANet::Tensor& MaxPool2d::forward(CUDANet::Tensor& input) {
output.zero();
backend->maxPool2d(
input, output, in_shape, pooling_shape, stride_shape, padding_shape,
input, output, in_shape, pool_shape, stride_shape, padding_shape,
out_shape
);
return output;