scalar-autograd 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/V.d.ts +0 -0
- package/dist/V.js +0 -0
- package/dist/Value.d.ts +5 -0
- package/dist/Value.js +7 -0
- package/dist/Value.spec.js +9 -0
- package/dist/ValueArithmetic.d.ts +1 -0
- package/dist/ValueArithmetic.js +10 -0
- package/package.json +1 -1
package/dist/V.d.ts
CHANGED
|
Binary file
|
package/dist/V.js
CHANGED
|
Binary file
|
package/dist/Value.d.ts
CHANGED
|
@@ -200,6 +200,11 @@ export declare class Value {
|
|
|
200
200
|
* @returns New Value which is the negation.
|
|
201
201
|
*/
|
|
202
202
|
neg(): Value;
|
|
203
|
+
/**
|
|
204
|
+
* Returns sign(this).
|
|
205
|
+
* @returns New Value with sign.
|
|
206
|
+
*/
|
|
207
|
+
sign(): Value;
|
|
203
208
|
/**
|
|
204
209
|
* Returns the sum of the given Values.
|
|
205
210
|
* @param vals Array of Value objects
|
package/dist/Value.js
CHANGED
|
@@ -289,6 +289,13 @@ class Value {
|
|
|
289
289
|
neg() {
|
|
290
290
|
return ValueArithmetic_1.ValueArithmetic.neg(this);
|
|
291
291
|
}
|
|
292
|
+
/**
|
|
293
|
+
* Returns sign(this).
|
|
294
|
+
* @returns New Value with sign.
|
|
295
|
+
*/
|
|
296
|
+
sign() {
|
|
297
|
+
return ValueArithmetic_1.ValueArithmetic.sign(this);
|
|
298
|
+
}
|
|
292
299
|
/**
|
|
293
300
|
* Returns the sum of the given Values.
|
|
294
301
|
* @param vals Array of Value objects
|
package/dist/Value.spec.js
CHANGED
|
@@ -230,6 +230,15 @@ describe('Value unary and binary operators: trigs, relu, abs, exp/log, min/max',
|
|
|
230
230
|
it('numerical gradient: log', () => testUnaryGrad('log', x => x.log(), x => 1 / x, 1.5));
|
|
231
231
|
it('numerical gradient: tanh', () => testUnaryGrad('tanh', x => x.tanh(), x => 1 - Math.tanh(x) ** 2, 0.9));
|
|
232
232
|
it('numerical gradient: sigmoid', () => testUnaryGrad('sigmoid', x => x.sigmoid(), x => { const s = 1 / (1 + Math.exp(-x)); return s * (1 - s); }, 0.7));
|
|
233
|
+
it('numerical gradient: sign', () => testUnaryGrad('sign', x => x.sign(), x => 0, 2.0));
|
|
234
|
+
it('numerical gradient: sign negative', () => testUnaryGrad('sign', x => x.sign(), x => 0, -2.0));
|
|
235
|
+
it('gradient of sign(0) is 0', () => {
|
|
236
|
+
const x = new Value_1.Value(0.0, "x", true);
|
|
237
|
+
const y = x.sign();
|
|
238
|
+
expect(y.data).toBe(0); // sign(0) should be 0
|
|
239
|
+
y.backward();
|
|
240
|
+
expect(x.grad).toBe(0); // Analytical gradient for sign(0) is implemented as 0
|
|
241
|
+
});
|
|
233
242
|
// Numerical vs analytic gradient checks for binary operators
|
|
234
243
|
it('numerical gradient: add', () => testBinaryGrad('add', (a, b) => a.add(b), (a, b) => 1, (a, b) => 1, 1.3, -2.1));
|
|
235
244
|
it('numerical gradient: sub', () => testBinaryGrad('sub', (a, b) => a.sub(b), (a, b) => 1, (a, b) => -1, 5.2, -1.2));
|
package/dist/ValueArithmetic.js
CHANGED
|
@@ -176,5 +176,15 @@ class ValueArithmetic {
|
|
|
176
176
|
a.grad -= out.grad;
|
|
177
177
|
}, `(-${a.label})`);
|
|
178
178
|
}
|
|
179
|
+
static sign(a) {
|
|
180
|
+
const s = Math.sign(a.data);
|
|
181
|
+
return Value_1.Value.make(s, a, null, (out) => () => {
|
|
182
|
+
// The derivative of sign(x) is 0 for x != 0.
|
|
183
|
+
// At x = 0, the derivative is undefined (Dirac delta), but for practical purposes in ML,
|
|
184
|
+
// we can define it as 0.
|
|
185
|
+
if (a.requiresGrad)
|
|
186
|
+
a.grad += 0 * out.grad;
|
|
187
|
+
}, `sign(${a.label})`);
|
|
188
|
+
}
|
|
179
189
|
}
|
|
180
190
|
exports.ValueArithmetic = ValueArithmetic;
|