Closed laclouis5 closed 3 years ago
Thanks for reporting!
- Any idea on how to solve this?
Could you please share the compiler version you're using (swift --version
)?
Your code snippet compiles fine for me, with Swift for TensorFlow 0.12:
$ swift --version
Swift version 5.3-dev (LLVM 69d8678431d3eee, Swift e1aef96b7fea59b)
Target: x86_64-apple-darwin19.6.0
```swift
import TensorFlow
struct TensorPair
- Is there a better alternative to write layers that accept more than one input?
- Will the
Layer
protocol support acallAsFunction()
requirement with multiple inputs in the future (when generics allow that of course), i.e acallAsFunction(_ input1: Input1, _ input2: Input2, ...)
orcallAsFunction(_ inputs: Input...)
? A also think that theTensorPair
struct should be part of the TensorFlow Swift API, or that tuples should automatically conform to Differentiable when possible.
There isn't a better way to write layers taking/return more than one value currently, unfortunately.
Layer.callAsFunction
takes a single Differentiable
-conforming argument and returns a single Differentiable
-conforming result. To encode multiple arguments or results, a tuple-representing struct like TensorPair
is the best solution.
Tuple types cannot yet conform to protocols like Differentiable
. Some hardcoded support is ~being added to let tuples conform to Equatable
, Comparable
, and Hashable
(https://github.com/apple/swift/pull/28833), but that is quite involved and not extensible. There's also the nuance that different tuple types with the same arity may have different TangentVector
types based on elements' conformances to Differentiable
(like (Float, Float)
vs (Float, Int)
).
By the way, here's an alternative (simpler) UNet
definition. It directly calls functions like relu
in callAsFunction
methods instead of creating a dedicated ReLU
layer.
Thank you for the quick and detailed answer! I'm using S4TF in a Google Colab but I was not enable to find the swift version.
I see that the simpler implementation you linked inlines everything in the UNet Module, this is great but not flexible enough for my application.
I'll try on a different environment as the code seems to compile correctly on your side.
Please open a new issue if you have any more questions :)
I'm trying to implement a UNet-like segmentation network, here is the complete definition:
The problem is that the
Up
layer must accept two inputs (the input and shortcut connection), so I created a Differentiable struct to handle this case as thecallAsFunction()
method only accept one input. However, this leads to an error:Same error for the three other
Up
layers.Layer
protocol support acallAsFunction()
requirement with multiple inputs in the future (when generics allow that of course), i.e acallAsFunction(_ input1: Input1, _ input2: Input2, ...)
orcallAsFunction(_ inputs: Input...)
? A also think that theTensorPair
struct should be part of the TensorFlow Swift API, or that tuples should automatically conform to Differentiable when possible.