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

Commit d0b108c

Browse files
committed
Add @Trainable property wrapper.
`@Trainable` wraps differentiable values and provides toggleable trainability via the `isTrainable` property.
1 parent 140fc41 commit d0b108c

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

Sources/TensorFlow/Layer.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,35 @@ public final class Parameter<Scalar: TensorFlowScalar> {
191191
self.value = value
192192
}
193193
}
194+
195+
/// A wrapper around a differentiable value with toggleable trainability.
196+
@_propertyWrapper
197+
public struct Trainable<Value: Differentiable> : Differentiable {
198+
@noDerivative public var isTrainable: Bool = true
199+
public var _value: Value
200+
201+
public init(initialValue: Value) {
202+
_value = initialValue
203+
}
204+
205+
@differentiable(vjp: _vjpValue)
206+
public var value: Value {
207+
get { _value.withoutDerivative() }
208+
set { _value = newValue }
209+
}
210+
211+
@usableFromInline
212+
func _vjpValue() -> (value: Value, pullback: (Value.TangentVector) -> TangentVector) {
213+
return (value, { [isTrainable = self.isTrainable] v in
214+
if isTrainable {
215+
return v
216+
}
217+
return .zero
218+
})
219+
}
220+
221+
public typealias TangentVector = Value.TangentVector
222+
public mutating func move(along direction: TangentVector) {
223+
_value.move(along: direction)
224+
}
225+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import XCTest
16+
@testable import TensorFlow
17+
18+
final class TrainableTests: XCTestCase {
19+
func testTrainableLayer() {
20+
struct TrainableDense : Layer {
21+
@Trainable var weight: Float
22+
// Workaround for internal storage property.
23+
var trainableWeight: Trainable<Float> {
24+
get { $weight }
25+
set { $weight = newValue }
26+
}
27+
28+
@Trainable var bias: Float
29+
// Workaround for internal storage property.
30+
var trainableBias: Trainable<Float> {
31+
get { $bias }
32+
set { $bias = newValue }
33+
}
34+
35+
@differentiable
36+
func callAsFunction(_ input: Float) -> Float {
37+
return input * weight + bias
38+
}
39+
}
40+
41+
var dense = TrainableDense(weight: 2, bias: 3)
42+
let x: Float = 4
43+
do {
44+
let (value, gradient) = valueWithGradient(at: dense, x) { dense, x in dense(x) }
45+
XCTAssertEqual(11, value)
46+
// FIXME: '$' is not a valid identifier:
47+
// cannot declare entity named '$weight'; the '$' prefix is reserved for
48+
// implicitly-synthesized declarations.
49+
//
50+
// Tentative solution: change `Differentiable` derived conformances to use original
51+
// property names instead of underlying storage property names.
52+
//
53+
// Now: `TrainableDense.TangentVector($weight: ..., $bias: ...)
54+
// Goal: `TrainableDense.TangentVector(weight: ..., bias: ...)
55+
//
56+
// XCTAssertEqual(TrainableDense.TangentVector($weight: 4, $bias: 1), gradient.0)
57+
XCTAssertEqual(4, gradient.0.$weight)
58+
XCTAssertEqual(1, gradient.0.$bias)
59+
XCTAssertEqual(2, gradient.1)
60+
}
61+
dense.trainableWeight.isTrainable = false
62+
do {
63+
let (value, gradient) = valueWithGradient(at: dense, x) { dense, x in dense(x) }
64+
XCTAssertEqual(11, value)
65+
XCTAssertEqual(0, gradient.0.$weight)
66+
XCTAssertEqual(1, gradient.0.$bias)
67+
XCTAssertEqual(2, gradient.1)
68+
}
69+
}
70+
71+
static var allTests = [
72+
("testTrainableLayer", testTrainableLayer),
73+
]
74+
}

0 commit comments

Comments
 (0)