|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2010 Google Inc. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import {SquaredDifference} from '../kernel_names'; |
| 19 | +import {GradConfig} from '../kernel_registry'; |
| 20 | +import {mul, sub} from '../ops/binary_ops'; |
| 21 | +import {scalar} from '../ops/tensor_ops'; |
| 22 | +import {Tensor} from '../tensor'; |
| 23 | + |
| 24 | +export const squaredDifferenceGrad_: GradConfig = { |
| 25 | + kernelName: SquaredDifference, |
| 26 | + gradFunc: (dy: Tensor, saved: Tensor[]) => { |
| 27 | + const [$a, $b] = saved; |
| 28 | + const two = scalar(2); |
| 29 | + const derA = () => mul(dy, mul(two, sub($a, $b))); |
| 30 | + const derB = () => mul(dy, mul(two, sub($b, $a))); |
| 31 | + return {$a: derA, $b: derB}; |
| 32 | + } |
| 33 | +}; |
0 commit comments