forked from arrayfire/arrayfire-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathml.py
More file actions
79 lines (59 loc) · 2.6 KB
/
ml.py
File metadata and controls
79 lines (59 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#######################################################
# Copyright (c) 2020, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################
"""
Machine learning functions
- Pool 2D, ND, maxpooling, minpooling, meanpooling
- Forward and backward convolution passes
"""
from .array import Array
from .library import backend, safe_call, c_pointer, CONV_GRADIENT
from .util import dim4
def convolve2GradientNN(
incoming_gradient, original_signal, original_kernel, convolved_output, stride=(1, 1), padding=(0, 0),
dilation=(1, 1), gradType=CONV_GRADIENT.DEFAULT):
"""
Function for calculating backward pass gradient of 2D convolution.
This function calculates the gradient with respect to the output of the
\ref convolve2NN() function that uses the machine learning formulation
for the dimensions of the signals and filters
Multiple signals and filters can be batched against each other, however
their dimensions must match.
Example:
Signals with dimensions: d0 x d1 x d2 x Ns
Filters with dimensions: d0 x d1 x d2 x Nf
Resulting Convolution: d0 x d1 x Nf x Ns
Parameters
-----------
incoming_gradient: af.Array
- Gradients to be distributed in backwards pass
original_signal: af.Array
- A 2 dimensional signal or batch of 2 dimensional signals.
original_kernel: af.Array
- A 2 dimensional kernel or batch of 2 dimensional kernels.
convolved_output: af.Array
- output of forward pass of convolution
stride: tuple of ints. default: (1, 1).
- Specifies how much to stride along each dimension
padding: tuple of ints. default: (0, 0).
- Specifies signal padding along each dimension
dilation: tuple of ints. default: (1, 1).
- Specifies how much to dilate kernel along each dimension before convolution
Returns
--------
output: af.Array
- Gradient wrt/requested gradient type
"""
output = Array()
stride_dim = dim4(stride[0], stride[1])
padding_dim = dim4(padding[0], padding[1])
dilation_dim = dim4(dilation[0], dilation[1])
safe_call(backend.get().af_convolve2_gradient_nn(
c_pointer(output.arr), incoming_gradient.arr, original_signal.arr, original_kernel.arr, convolved_output.arr,
2, c_pointer(stride_dim), 2, c_pointer(padding_dim), 2, c_pointer(dilation_dim), gradType.value))
return output