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

Update Dropout to be more concise #560

Merged
merged 6 commits into from
Nov 17, 2019
Merged
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
16 changes: 3 additions & 13 deletions Sources/TensorFlow/Layers/Dropout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,14 @@ public struct Dropout<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {

/// Creates a dropout layer.
///
/// - Parameter probability: The drop probability.
/// - Parameter probability: The probability of a node dropping out.
/// - Precondition: probability must be a value between 0 and 1 (inclusive).
public init(probability: Double) {
precondition(0...1 ~= probability,
"Probability must be a value between 0 and 1 (inclusive) but is \(probability)")
self.probability = probability
}

@differentiable
private func applyingTraining(to input: Tensor<Scalar>) -> Tensor<Scalar> {
return input._droppingOut(probability: probability)
}

@differentiable
private func applyingInference(to input: Tensor<Scalar>) -> Tensor<Scalar> {
return input
}

/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameter input: The input to the layer.
Expand All @@ -72,9 +62,9 @@ public struct Dropout<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
switch Context.local.learningPhase {
case .training:
return applyingTraining(to: input)
return input._droppingOut(probability: probability)
case .inference:
return applyingInference(to: input)
return input
}
}
}