Uploaded image for project: ' Swift for TensorFlow'
  1. Swift for TensorFlow
  2. TF-637

Conditional application of @differentiable

    XMLWordPrintable

    Details

    • Type: Story
    • Status: New
    • Priority: Minor
    • Resolution: Unresolved
    • Component/s: None
    • Labels:
      None

      Description

      There are several use cases where certain types only conditionally conform to Differentiable and we then want to have some of their functions also be differentiable. For example:

      public protocol Distribution {
        associatedtype Value
      
        // The following cannot currently be done because conforming 
        // types need to also add this attribute, but non-differentiable ones
        // cannot. Example shown below.
        @differentiable(wrt: self where Self: Differentiable)
        func logProbability(of value: Value) -> Tensor<Float>
      }
      
      public struct Deterministic<Value: Scalar: TensorFlowScalar & Equatable>: Distribution {
        public let value: Tensor<Scalar>
      
        // The compiler complains about a missing `@differentiable` attribute, but 
        // there is no way to add it such that it conforms to `Distribution`.
        public func logProbability(of value: Tensor<Scalar>) -> Tensor<Float> {
          return log(Tensor<Float>(value .== self.value)) // Ignore NaNs for this example.
        }
      }
      

      Also, the following approach with an additional protocol does not work either:

      public protocol DifferentiableDistribution: Distribution, Differentiable {
        @differentiable(wrt: self)
        func logProbability(of value: Value) -> Tensor<Float>
      }
      

      If I have a policy that conforms to DifferentiableDistribution and I call logProbability(of: ) on that, the compiler does not recognize that expression as differentiable.

        Attachments

          Activity

            People

            • Assignee:
              xinranmsn Richard Wei
              Reporter:
              eaplatanios Anthony Platanios
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: