Skip to content

Commit

Permalink
feat: max pool
Browse files Browse the repository at this point in the history
  • Loading branch information
chachaleo committed Feb 24, 2024
1 parent 7d84a42 commit 9e97295
Show file tree
Hide file tree
Showing 64 changed files with 7,008 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# LinearClassifierTrait::predict

```rust
fn predict(ref self: LinearClassifier<T>, X: Tensor<T>) -> Tensor<T>;
fn predict(classifier: LinearClassifier<T>, X: Tensor<T>) -> Tensor<T>;
```

Linear Classifier. Performs the linear classification.
Expand Down Expand Up @@ -85,7 +85,7 @@ fn linear_classifier_helper(
fn linear_classifier_multi_softmax() -> (Span<usize>, Tensor<FP16x16>) {
let (mut classifier, X) = linear_classifier_helper(POST_TRANSFORM::SOFTMAX);

let (labels, mut scores) = LinearClassifierTrait::predict(ref classifier, X);
let (labels, mut scores) = LinearClassifierTrait::predict(classifier, X);

(labels, scores)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# LinearRegressorTrait::predict

```rust
fn predict(ref self: LinearRegressor<T>, X: Tensor<T>) -> Tensor<T>;
fn predict(regressor: LinearRegressor<T>, X: Tensor<T>) -> Tensor<T>;
```

Linear Regressor. Performs the generalized linear regression evaluation.

## Args

* `self`: LinearRegressor<T> - A LinearRegressor object.
* `regressor`: LinearRegressor<T> - A LinearRegressor object.
* `X`: Input 2D tensor.

## Returns
Expand Down Expand Up @@ -68,7 +68,7 @@ fn example_linear_regressor() -> Tensor<FP16x16> {
post_transform
};

let scores = LinearRegressorTrait::predict(ref regressor, X);
let scores = LinearRegressorTrait::predict(regressor, X);

scores
}
Expand Down Expand Up @@ -120,7 +120,7 @@ fn example_linear_regressor_2() -> Tensor<FP16x16> {
post_transform
};

let scores = LinearRegressorTrait::predict(ref regressor, X);
let scores = LinearRegressorTrait::predict(regressor, X);

scores
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# TreeEnsembleClassifier::predict

```rust
fn predict(ref self: TreeEnsembleClassifier<T>, X: Tensor<T>) -> (Span<usize>, MutMatrix::<T>);
fn predict(classifier: TreeEnsembleClassifier<T>, X: Tensor<T>) -> (Span<usize>, MutMatrix::<T>);
```

Tree Ensemble classifier. Returns the top class for each of N inputs.
Expand Down Expand Up @@ -185,7 +185,7 @@ fn tree_ensemble_classifier_helper(
fn test_tree_ensemble_classifier_multi_pt_softmax() -> (Span<usize>, MutMatrix::<FP16x16>) {
let (mut classifier, X) = tree_ensemble_classifier_helper(POST_TRANSFORM::SOFTMAX);

let (labels, scores) = TreeEnsembleClassifierTrait::predict(ref classifier, X);
let (labels, scores) = TreeEnsembleClassifierTrait::predict(classifier, X);
(labels, scores)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# TreeEnsembleRegressor::predict

```rust
fn predict(ref self: TreeEnsembleRegressor<T>, X: Tensor<T>) -> (Span<usize>, MutMatrix::<T>);
fn predict(regressor: TreeEnsembleRegressor<T>, X: Tensor<T>) -> (Span<usize>, MutMatrix::<T>);
```

Tree Ensemble regressor. Returns the regressed values for each input in N.
Expand Down Expand Up @@ -160,7 +160,7 @@ fn tree_ensemble_regressor_helper(

fn test_tree_ensemble_regressor_SUM() -> MutMatrix::<FP16x16> {
let (mut regressor, X) = tree_ensemble_regressor_helper(AGGREGATE_FUNCTION::SUM);
let mut res = TreeEnsembleRegressorTrait::predict(ref regressor, X);
let mut res = TreeEnsembleRegressorTrait::predict(regressor, X);
res
}
>>>
Expand Down
1 change: 0 additions & 1 deletion docs/framework/operators/neural-network/nn.col2im.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# NNTrait::col2im

```rust
Expand Down
107 changes: 107 additions & 0 deletions docs/framework/operators/neural-network/nn.max_pool.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

# NNTrait::max_pool

```rust
fn max_pool(
X: @Tensor<T>,
auto_pad: Option<AUTO_PAD>,
ceil_mode: Option<usize>,
dilations: Option<Span<usize>>,
kernel_shape: Span<usize>,
pads: Option<Span<usize>>,
storage_order: Option<usize>,
strides: Option<Span<usize>>,
output_len: usize,
) -> (Tensor<T>, Option<Tensor<i32>>);
```

MaxPool consumes an input tensor X and applies max pooling across the tensor according to kernel sizes, stride sizes, and pad lengths. max pooling consisting of computing the max on all values of a subset of the input tensor according to the kernel size and downsampling the data into the output tensor Y for further processing. The output spatial shape is calculated differently depending on whether explicit padding is used, where pads is employed, or auto padding is used, where auto_pad is utilized.

## Args

* `X`(`@Tensor<T>`) - Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size.
* `auto_pad`(`Option<AUTO_PAD>`) - Default is NOTSET, auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. NOTSET means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that `output_shape[i] = ceil(input_shape[i] / strides[i])` for each axis `i`.
* `ceil_mode`(`Option<usize>`) - Default is 1, Whether to use ceil or floor (default) to compute the output shape.
* `dilations`(`Option<Span<usize>>`) - Dilation value along each spatial axis of the filter. If not present, the dilation defaults to 1 along each spatial axis.
* `kernel_shape`(`Span<usize>`) - The size of the kernel along each axis.
* `pads`(`Option<Span<usize>>`) - Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis.
* `storage_order`(`Option<usize>`) - Default is 0, The storage order of the tensor. 0 is row major, and 1 is column major.
* `strides`(`Option<Span<usize>>`) - Stride along each spatial axis. If not present, the stride defaults to 1 along each spatial axis.
* `output_len`(`Option<usize>`) - Default is 1, If set to 2, return the indices tensor.

## Returns

A `Tensor<T>` that contains the result of the max pool.
A `Option<Tensor<i32>>` with the indices tensor from max pooling across the input tensor. The dimensions of indices are the same as output tensor.
## Examples

```rust
use orion::operators::nn::NNTrait;
use orion::numbers::FixedTrait;
use orion::operators::nn::FP16x16NN;
use orion::numbers::FP16x16;
use orion::operators::tensor::{Tensor, TensorTrait, FP16x16Tensor};


fn example_max_pool() -> (Tensor<FP16x16>, Option<Tensor<i32>>) {
let mut shape = ArrayTrait::<usize>::new();
shape.append(1);
shape.append(1);
shape.append(5);
shape.append(5);
let mut data = ArrayTrait::new();
data.append(FP16x16 { mag: 65536, sign: false });
data.append(FP16x16 { mag: 131072, sign: false });
data.append(FP16x16 { mag: 196608, sign: false });
data.append(FP16x16 { mag: 262144, sign: false });
data.append(FP16x16 { mag: 327680, sign: false });
data.append(FP16x16 { mag: 393216, sign: false });
data.append(FP16x16 { mag: 458752, sign: false });
data.append(FP16x16 { mag: 524288, sign: false });
data.append(FP16x16 { mag: 589824, sign: false });
data.append(FP16x16 { mag: 655360, sign: false });
data.append(FP16x16 { mag: 720896, sign: false });
data.append(FP16x16 { mag: 786432, sign: false });
data.append(FP16x16 { mag: 851968, sign: false });
data.append(FP16x16 { mag: 917504, sign: false });
data.append(FP16x16 { mag: 983040, sign: false });
data.append(FP16x16 { mag: 1048576, sign: false });
data.append(FP16x16 { mag: 1114112, sign: false });
data.append(FP16x16 { mag: 1179648, sign: false });
data.append(FP16x16 { mag: 1245184, sign: false });
data.append(FP16x16 { mag: 1310720, sign: false });
data.append(FP16x16 { mag: 1376256, sign: false });
data.append(FP16x16 { mag: 1441792, sign: false });
data.append(FP16x16 { mag: 1507328, sign: false });
data.append(FP16x16 { mag: 1572864, sign: false });
data.append(FP16x16 { mag: 1638400, sign: false });
let mut X = TensorTrait::new(shape.span(), data.span());
return NNTrait::max_pool(
@X,
Option::None,
Option::None,
Option::None,
array![5, 5, 5].span(),
Option::Some(array![2, 2, 2, 2].span()),
Option::None,
Option::None,
1
);

}

>>> ([
[
[
[13, 14, 15, 15, 15],
[18, 19, 20, 20, 20],
[23, 24, 25, 25, 25],
[23, 24, 25, 25, 25],
[23, 24, 25, 25, 25],
]
]
],
Option::None)


````
Loading

0 comments on commit 9e97295

Please sign in to comment.