|
1 | 1 | #' Random convolutional features |
2 | 2 | #' |
3 | | -#' Generates random convolutional features from a list of images for use in |
4 | | -#' regression modeling. Convolutional kernels are generated randomly (either |
| 3 | +#' Generates random convolutional features from a list of images. |
| 4 | +#' Convolutional kernels are generated randomly (either |
5 | 5 | #' from a Gaussian distribution or as patches extracted from the training |
6 | 6 | #' images), applied to each image via efficient matrix multiplication, and then |
7 | | -#' pooled to produce a fixed-size feature vector per image. This approach allows |
8 | | -#' kernel methods and other flexible models to be applied to image data. |
| 7 | +#' pooled to produce a fixed-size feature vector per image. |
9 | 8 | #' |
| 9 | +#' @inheritParams b_rff |
10 | 10 | #' @param x A list of images, where each image is a matrix (for grayscale) or a |
11 | 11 | #' 3D array with dimensions (height, width, channels) for color images. Images |
12 | 12 | #' may have different dimensions, but must be large enough to accommodate the |
|
17 | 17 | #' @param stride The stride for the convolution operation, i.e., how many |
18 | 18 | #' pixels to skip between kernel applications. Default is 1. |
19 | 19 | #' @param kernel_gen Method for generating convolutional kernels. Either `"rnorm"` |
20 | | -#' to generate kernels with entries drawn iid from a standard Normal |
| 20 | +#' to generate kernels with entries drawn i.i.d. from a standard Normal |
21 | 21 | #' distribution, or `"patch"` to extract random patches from the input images. |
22 | 22 | #' @param activation A function to pool the convolution outputs for each kernel. |
23 | 23 | #' Defaults to [max()]. The function should accept a numeric vector and return |
24 | 24 | #' a scalar or vector of pooled values. Common choices include [max()], |
25 | | -#' [mean()], or custom functions. |
26 | | -#' @param stdize How to standardize the image pixel values before convolution, |
27 | | -#' if at all. The default `"scale"` applies `scale()` to the pixels so they |
28 | | -#' have mean zero and unit variance across all images. `"box"` scales pixels |
29 | | -#' to lie in \[0, 1\], `"symbox"` scales to lie in \[-0.5, 0.5\], and `"none"` |
30 | | -#' applies no standardization. |
| 25 | +#' [mean()], functions like the proportion of positive values (PPV), which |
| 26 | +#' can be implemented with `function(x) mean(x > 0)`. Multivariate pooling |
| 27 | +#' functions are also supported. |
31 | 28 | #' @param kernels Optional matrix of pre-specified convolutional kernels, where |
32 | 29 | #' each column is a kernel in column-major format. If provided, overrides `p`, |
33 | 30 | #' `size`, and `kernel_gen`. |
34 | | -#' @param shift Optional shift value(s) for standardization. If provided, |
35 | | -#' overrides the shift calculated according to `stdize`. |
36 | | -#' @param scale Optional scale value(s) for standardization. If provided, |
37 | | -#' overrides the scale calculated according to `stdize`. |
38 | 31 | #' |
39 | 32 | #' @returns A matrix of random convolutional features with one row per image in |
40 | 33 | #' `x` and one column per kernel (or more columns if `activation` is |
41 | 34 | #' multivariate). |
42 | 35 | #' |
43 | 36 | #' @examples |
44 | | -#' # Create synthetic grayscale images |
45 | | -#' images = lapply(1:20, function(i) matrix(runif(28*28), 28, 28)) |
| 37 | +#' x = outer(1:28, 1:28, function(x, y) { |
| 38 | +#' d = sqrt(4*(x - 14)^2 + (y - 14)^2) |
| 39 | +#' dnorm(d, mean = 10, sd = 0.8) |
| 40 | +#' }) |
| 41 | +#' pal = gray.colors(256, 1, 0) |
| 42 | +#' image(x, col = pal) |
46 | 43 | #' |
47 | | -#' # Generate random convolutional features |
48 | | -#' m = b_conv(images, p = 10, size = 3) |
| 44 | +#' # one random kernel (no activation) |
| 45 | +#' m = b_conv(list(x), p=1, activation=\(x) x) |
| 46 | +#' image(matrix(m, nrow = 26), col = pal) |
| 47 | +#' |
| 48 | +#' # many kernels (realistic use case) |
| 49 | +#' m = b_conv(list(x), p = 100, size = 3) |
| 50 | +#' str(m) |
49 | 51 | #' @export |
50 | 52 | b_conv <- function( |
51 | 53 | x, |
|
0 commit comments