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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use objc2::{msg_send, rc::Retained};
use objc2_foundation::NSString;
use crate::{Graph, ScalarOrTensor, Tensor};
impl Graph {
/// Applies the ReLU activation: `f(x) = max(x, 0)`.
///
/// # Arguments
///
/// * `tensor` – Input tensor.
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A [`Tensor`] containing the ReLU results.
pub fn relu(&self, tensor: &Tensor, name: Option<&str>) -> Retained<Tensor> {
unsafe {
msg_send![self, reLUWithTensor: tensor, name: name.map(NSString::from_str).as_deref()]
}
}
/// Gradient of the ReLU activation.
///
/// # Arguments
///
/// * `gradient` – Incoming gradient (`dL/dR`).
/// * `source` – Tensor used in the forward ReLU pass.
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A [`Tensor`] containing `dL/dX`.
pub fn relu_gradient(
&self,
gradient: &Tensor,
source: &Tensor,
name: Option<&str>,
) -> Retained<Tensor> {
unsafe {
msg_send![
self,
reLUGradientWithIncomingGradient: gradient,
sourceTensor: source,
name: name.map(NSString::from_str).as_deref()
]
}
}
/// Applies the sigmoid activation.
///
/// # Arguments
///
/// * `tensor` – Input tensor.
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A [`Tensor`] after sigmoid.
pub fn sigmoid(&self, tensor: &Tensor, name: Option<&str>) -> Retained<Tensor> {
unsafe {
msg_send![self, sigmoidWithTensor: tensor, name: name.map(NSString::from_str).as_deref()]
}
}
/// Gradient of the sigmoid activation.
///
/// # Arguments
///
/// * `gradient` – Incoming gradient.
/// * `source` – Tensor used in the forward sigmoid pass.
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A [`Tensor`] containing `dL/dX`.
pub fn sigmoid_gradient(
&self,
gradient: &Tensor,
source: &Tensor,
name: Option<&str>,
) -> Retained<Tensor> {
unsafe {
msg_send![
self,
sigmoidGradientWithIncomingGradient: gradient,
sourceTensor: source,
name: name.map(NSString::from_str).as_deref()
]
}
}
/// Applies the softmax function along `axis`.
///
/// # Arguments
///
/// * `tensor` – Input tensor.
/// * `axis` – Axis along which softmax is computed.
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A [`Tensor`] containing probabilities that sum to 1 across `axis`.
pub fn soft_max(&self, tensor: &Tensor, axis: i64, name: Option<&str>) -> Retained<Tensor> {
unsafe {
msg_send![self, softMaxWithTensor: tensor, axis: axis, name: name.map(NSString::from_str).as_deref()]
}
}
/// Gradient of the softmax function.
///
/// # Arguments
///
/// * `gradient` – Incoming gradient.
/// * `source` – Tensor used in the forward softmax pass.
/// * `axis` – Axis along which softmax was computed.
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A [`Tensor`] containing `dL/dX`.
pub fn soft_max_gradient(
&self,
gradient: &Tensor,
source: &Tensor,
axis: i64,
name: Option<&str>,
) -> Retained<Tensor> {
unsafe {
msg_send![
self,
softMaxGradientWithIncomingGradient: gradient,
sourceTensor: source,
axis: axis,
name: name.map(NSString::from_str).as_deref()
]
}
}
/// Applies leaky ReLU: `f(x) = max(x, α x)`.
///
/// # Arguments
///
/// * `tensor` – Input tensor.
/// * `alpha` – Slope for negative values (scalar or tensor).
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A [`Tensor`] after leaky ReLU.
pub fn leaky_relu<'a>(
&self,
tensor: &Tensor,
alpha: ScalarOrTensor<'a, f64>,
name: Option<&str>,
) -> Retained<Tensor> {
unsafe {
match alpha {
ScalarOrTensor::Scalar(alpha) => {
msg_send![self, leakyReLUWithTensor: tensor, alpha: alpha, name: name.map(NSString::from_str).as_deref()]
}
ScalarOrTensor::Tensor(alpha_tensor) => {
msg_send![self, leakyReLUWithTensor: tensor, alphaTensor: alpha_tensor, name: name.map(NSString::from_str).as_deref()]
}
}
}
}
/// Gradient of the leaky ReLU activation.
///
/// Supports broadcasting of `alpha_tensor`.
///
/// # Arguments
///
/// * `gradient` – Incoming gradient.
/// * `source` – Input tensor from forward pass.
/// * `alpha_tensor` – Alpha tensor used in the forward pass.
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A [`Tensor`] containing `dL/dX`.
pub fn leaky_relu_gradient(
&self,
gradient: &Tensor,
source: &Tensor,
alpha_tensor: &Tensor,
name: Option<&str>,
) -> Retained<Tensor> {
unsafe {
msg_send![
self,
leakyReLUGradientWithIncomingGradient: gradient,
sourceTensor: source,
alphaTensor: alpha_tensor,
name: name.map(NSString::from_str).as_deref()
]
}
}
}