Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions tfjs-core/scripts/touch_modular_op_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async function main() {
console.log('Called touch_modular_op_files with args:', args);

if (args.op != null) {
const ops: string[] = args.op.split(',');
const ops: string[] = args.op.split(',').filter((o: string) => o != null);
ops.forEach(op => {
let filePath = `./src/ops/${op}.ts`;
let command = `touch ${filePath}`;
Expand All @@ -84,7 +84,8 @@ async function main() {
return str.charAt(0).toLowerCase() + str.slice(1);
};

const kernels: string[] = args.grad.split(',');
const kernels: string[] =
args.grad.split(',').filter((k: string) => k != null);

kernels.forEach(kernelName => {
const gradientFileTemplate = `/**
Expand All @@ -104,12 +105,12 @@ async function main() {
* =============================================================================
*/

import {KernelName, KernelNameAttrs} from '../kernel_names';
import {${kernelName}, ${kernelName}Attrs} from '../kernel_names';
import {GradConfig, NamedAttrMap} from '../kernel_registry';
import {Tensor} from '../tensor';

export const ${downcaseFirstChar(kernelName)}GradConfig: GradConfig = {
kernelName: KernelName,
kernelName: ${kernelName},
inputsToSave: [], // UPDATE ME
outputsToSave: [], // UPDATE ME
gradFunc: (dy: Tensor, saved: Tensor[], attrs: NamedAttrMap) => {
Expand Down
32 changes: 32 additions & 0 deletions tfjs-core/src/gradients/Abs_grad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import {Abs} from '../kernel_names';
import {GradConfig} from '../kernel_registry';
import {cast} from '../ops/array_ops';
import {mul} from '../ops/mul';
import {step} from '../ops/unary_ops';
import {Tensor} from '../tensor';

export const absGradConfig: GradConfig = {
kernelName: Abs,
inputsToSave: ['x'],
gradFunc: (dy: Tensor, saved: Tensor[]) => {
const [x] = saved;
return {x: () => mul(dy, step(cast(x, 'float32'), -1))};
}
};
44 changes: 44 additions & 0 deletions tfjs-core/src/gradients/Acos_grad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import {Acos} from '../kernel_names';
import {GradConfig} from '../kernel_registry';
import {cast} from '../ops/array_ops';
import {div} from '../ops/div';
import {neg} from '../ops/neg';
import {square} from '../ops/square';
import {sub} from '../ops/sub';
import {scalar} from '../ops/tensor_ops';
import {sqrt} from '../ops/unary_ops';
import {Tensor} from '../tensor';

export const acosGradConfig: GradConfig = {
kernelName: Acos,
inputsToSave: ['x'],
gradFunc: (dy: Tensor, saved: Tensor[]) => {
const [x] = saved;

return {
x: () => {
const a = square(cast(x, 'float32'));
const b = sqrt(sub(scalar(1), a));
return neg(div(dy, b));
}

};
}
};
40 changes: 40 additions & 0 deletions tfjs-core/src/gradients/Acosh_grad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import {Acosh} from '../kernel_names';
import {GradConfig} from '../kernel_registry';
import {cast} from '../ops/array_ops';
import {div} from '../ops/div';
import {square} from '../ops/square';
import {sub} from '../ops/sub';
import {sqrt} from '../ops/unary_ops';
import {Tensor} from '../tensor';

export const acoshGradConfig: GradConfig = {
kernelName: Acosh,
inputsToSave: ['x'],
gradFunc: (dy: Tensor, saved: Tensor[]) => {
const [x] = saved;

return {
x: () => {
const a = sqrt(sub(square(cast(x, 'float32')), 1));
return div(dy, a);
}
};
}
};
35 changes: 35 additions & 0 deletions tfjs-core/src/gradients/Asin_grad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import {Asin} from '../kernel_names';
import {GradConfig} from '../kernel_registry';
import {cast} from '../ops/array_ops';
import {div} from '../ops/div';
import {square} from '../ops/square';
import {sub} from '../ops/sub';
import {scalar} from '../ops/tensor_ops';
import {sqrt} from '../ops/unary_ops';
import {Tensor} from '../tensor';

export const asinGradConfig: GradConfig = {
kernelName: Asin,
inputsToSave: ['x'],
gradFunc: (dy: Tensor, saved: Tensor[]) => {
const [x] = saved;
return {x: () => div(dy, sqrt(sub(scalar(1), square(cast(x, 'float32')))))};
}
};
41 changes: 41 additions & 0 deletions tfjs-core/src/gradients/Asinh_grad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import {Asinh} from '../kernel_names';
import {GradConfig} from '../kernel_registry';
import {add} from '../ops/add';
import {cast} from '../ops/array_ops';
import {div} from '../ops/div';
import {square} from '../ops/square';
import {scalar} from '../ops/tensor_ops';
import {sqrt} from '../ops/unary_ops';
import {Tensor} from '../tensor';

export const asinhGradConfig: GradConfig = {
kernelName: Asinh,
inputsToSave: ['x'],
gradFunc: (dy: Tensor, saved: Tensor[]) => {
const [x] = saved;

return {
x: () => {
const a = sqrt(add(scalar(1), square(cast(x, 'float32'))));
return div(dy, a);
}
};
}
};
34 changes: 34 additions & 0 deletions tfjs-core/src/gradients/Atan_grad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import {Atan} from '../kernel_names';
import {GradConfig} from '../kernel_registry';
import {add} from '../ops/add';
import {cast} from '../ops/array_ops';
import {div} from '../ops/div';
import {square} from '../ops/square';
import {Tensor} from '../tensor';

export const atanGradConfig: GradConfig = {
kernelName: Atan,
inputsToSave: ['x'],
gradFunc: (dy: Tensor, saved: Tensor[]) => {
const [x] = saved;

return {x: () => div(dy, add(square(cast(x, 'float32')), 1))};
}
};
35 changes: 35 additions & 0 deletions tfjs-core/src/gradients/Atanh_grad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import {Atanh} from '../kernel_names';
import {GradConfig} from '../kernel_registry';
import {cast} from '../ops/array_ops';
import {div} from '../ops/div';
import {square} from '../ops/square';
import {sub} from '../ops/sub';
import {scalar} from '../ops/tensor_ops';
import {Tensor} from '../tensor';

export const atanhGradConfig: GradConfig = {
kernelName: Atanh,
inputsToSave: ['x'],
gradFunc: (dy: Tensor, saved: Tensor[]) => {
const [x] = saved;

return {x: () => div(dy, sub(scalar(1), square(cast(x, 'float32'))))};
}
};
34 changes: 34 additions & 0 deletions tfjs-core/src/gradients/Cos_grad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import {Cos} from '../kernel_names';
import {GradConfig} from '../kernel_registry';
import {cast} from '../ops/array_ops';
import {mul} from '../ops/mul';
import {neg} from '../ops/neg';
import {sin} from '../ops/sin';
import {Tensor} from '../tensor';

export const cosGradConfig: GradConfig = {
kernelName: Cos,
inputsToSave: ['x'],
gradFunc: (dy: Tensor, saved: Tensor[]) => {
const [x] = saved;

return {x: () => mul(neg(sin(cast(x, 'float32'))), dy)};
}
};
33 changes: 33 additions & 0 deletions tfjs-core/src/gradients/Cosh_grad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import {Cosh} from '../kernel_names';
import {GradConfig} from '../kernel_registry';
import {cast} from '../ops/array_ops';
import {mul} from '../ops/mul';
import {sinh} from '../ops/sinh';
import {Tensor} from '../tensor';

export const coshGradConfig: GradConfig = {
kernelName: Cosh,
inputsToSave: ['x'],
gradFunc: (dy: Tensor, saved: Tensor[]) => {
const [x] = saved;

return {x: () => mul(sinh(cast(x, 'float32')), dy)};
}
};
Loading