Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Fix "unable to type-check this expression in reasonable time" errors. #120

Merged
merged 1 commit into from
May 10, 2019
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
10 changes: 6 additions & 4 deletions Sources/DeepLearning/Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ public extension Tensor where Scalar: TensorFlowFloatingPoint {
let squaredDiff: Tensor = Raw.squaredDifference(self, mean)
let variance = squaredDiff.mean(alongAxes: axis)

let diff = self - mean
let diff = self - mean
let inv = rsqrt(variance + epsilon)
let norm = diff * inv

let dNorm = v * scale
let dNorm = v * scale
let dVariance = -(dNorm * diff).sum(alongAxes: axis) / 2 * pow(inv, -3)
let dMean = (-dNorm * inv).sum(alongAxes: axis) +
dVariance * (-diff * 2).mean(alongAxes: axis)
// Note: `dMean` is split into two lines to avoid the "compiler is unable to type-check
// this expression in reasonable time" error.
var dMean = (-dNorm * inv).sum(alongAxes: axis)
dMean = dMean + dVariance * (-diff * 2).mean(alongAxes: axis)
let dOffset = v.sum(alongAxes: axis)
let dScale = (norm * v).sum(alongAxes: axis)
let dim = Tensor(Tensor<Int32>(self.shapeTensor[axis]))
Expand Down
6 changes: 4 additions & 2 deletions Sources/DeepLearning/Optimizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ public class Adam<Model: Layer>: Optimizer
along direction: Model.AllDifferentiableVariables) {
step += 1
let learningRate = self.learningRate * 1 / (1 + decay * Float(step))
let stepSize = learningRate * (sqrt(1 - pow(beta2, Float(step))) /
(1 - pow(beta1, Float(step))))
// Note: `stepSize` is split into two lines to avoid the "compiler is unable to type-check
// this expression in reasonable time" error.
var stepSize = learningRate * sqrt(1 - pow(beta2, Float(step)))
stepSize = stepSize / (1 - pow(beta1, Float(step)))
// Update Float & Double Tensor variables.
for kp in model.recursivelyAllWritableKeyPaths(to: Tensor<Float>.self) {
firstMoments[keyPath: kp] =
Expand Down