* End a scope. Use this with startScope() to achieve the same functionality * as scope() without the need for a function closure.
(result?: TensorContainer)
| 1069 | * as scope() without the need for a function closure. |
| 1070 | */ |
| 1071 | endScope(result?: TensorContainer) { |
| 1072 | const tensorsToTrackInParent = getTensorsInContainer(result); |
| 1073 | const tensorsToTrackInParentSet = |
| 1074 | new Set(tensorsToTrackInParent.map(t => t.id)); |
| 1075 | |
| 1076 | // Dispose the arrays tracked in this scope. |
| 1077 | for (let i = 0; i < this.state.activeScope.track.length; i++) { |
| 1078 | const tensor = this.state.activeScope.track[i]; |
| 1079 | if (!tensor.kept && !tensorsToTrackInParentSet.has(tensor.id)) { |
| 1080 | tensor.dispose(); |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | const oldScope = this.state.scopeStack.pop(); |
| 1085 | this.state.activeScope = this.state.scopeStack.length === 0 ? |
| 1086 | null : |
| 1087 | this.state.scopeStack[this.state.scopeStack.length - 1]; |
| 1088 | |
| 1089 | // Track the current result in the parent scope. |
| 1090 | tensorsToTrackInParent.forEach(tensor => { |
| 1091 | // Only track the tensor if was allocated in the inner scope and is not |
| 1092 | // globally kept. |
| 1093 | if (!tensor.kept && tensor.scopeId === oldScope.id) { |
| 1094 | this.track(tensor); |
| 1095 | } |
| 1096 | }); |
| 1097 | } |
| 1098 | |
| 1099 | /** |
| 1100 | * Returns gradients of `f` with respect to each of the `xs`. The gradients |
no test coverage detected