|
| 1 | +// Copyright 2020 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 TensorFlow |
| 16 | + |
| 17 | +#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) |
| 18 | + import Darwin |
| 19 | +#elseif os(Windows) |
| 20 | + import ucrt |
| 21 | +#else |
| 22 | + import Glibc |
| 23 | +#endif |
| 24 | + |
| 25 | +/// Lattice |
| 26 | +/// |
| 27 | +/// Represents the lattice used by the WordSeg algorithm. |
| 28 | +public struct Lattice: Differentiable { |
| 29 | + /// Edge |
| 30 | + /// |
| 31 | + /// Represents an Edge |
| 32 | + public struct Edge: Differentiable { |
| 33 | + @noDerivative public var start: Int |
| 34 | + @noDerivative public var end: Int |
| 35 | + @noDerivative public var string: CharacterSequence |
| 36 | + public var logp: Float |
| 37 | + |
| 38 | + // expectation |
| 39 | + public var score: SemiRing |
| 40 | + public var totalScore: SemiRing |
| 41 | + |
| 42 | + @differentiable |
| 43 | + init( |
| 44 | + start: Int, end: Int, sentence: CharacterSequence, logp: Float, |
| 45 | + previous: SemiRing, order: Int |
| 46 | + ) { |
| 47 | + self.start = start |
| 48 | + self.end = end |
| 49 | + self.string = sentence |
| 50 | + self.logp = logp |
| 51 | + |
| 52 | + self.score = |
| 53 | + SemiRing( |
| 54 | + logp: logp, |
| 55 | + // TODO(abdulras): this should really use integeral pow |
| 56 | + logr: logp + logf(powf(Float(sentence.count), Float(order)))) |
| 57 | + self.totalScore = self.score * previous |
| 58 | + } |
| 59 | + |
| 60 | + @differentiable |
| 61 | + public init( |
| 62 | + start: Int, end: Int, string: CharacterSequence, logp: Float, |
| 63 | + score: SemiRing, totalScore: SemiRing |
| 64 | + ) { |
| 65 | + self.start = start |
| 66 | + self.end = end |
| 67 | + self.string = string |
| 68 | + self.logp = logp |
| 69 | + self.score = score |
| 70 | + self.totalScore = totalScore |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// Node |
| 75 | + /// |
| 76 | + /// Represents a node in the lattice |
| 77 | + public struct Node: Differentiable { |
| 78 | + @noDerivative public var bestEdge: Edge? |
| 79 | + public var bestScore: Float = 0.0 |
| 80 | + public var edges = [Edge]() |
| 81 | + public var semiringScore: SemiRing = SemiRing.one |
| 82 | + |
| 83 | + init() {} |
| 84 | + |
| 85 | + @differentiable |
| 86 | + public init( |
| 87 | + bestEdge: Edge?, bestScore: Float, edges: [Edge], |
| 88 | + semiringScore: SemiRing |
| 89 | + ) { |
| 90 | + self.bestEdge = bestEdge |
| 91 | + self.bestScore = bestScore |
| 92 | + self.edges = edges |
| 93 | + self.semiringScore = semiringScore |
| 94 | + } |
| 95 | + |
| 96 | + @differentiable |
| 97 | + func computeSemiringScore() -> SemiRing { |
| 98 | + // TODO: Reduceinto and += |
| 99 | + edges.differentiableMap { $0.totalScore }.differentiableReduce(SemiRing.zero) { $0 + $1 } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + var positions: [Node] |
| 104 | + |
| 105 | + @differentiable |
| 106 | + public subscript(index: Int) -> Node { |
| 107 | + get { return positions[index] } |
| 108 | + set(v) { positions[index] = v } |
| 109 | + //_modify { yield &positions[index] } |
| 110 | + } |
| 111 | + |
| 112 | + // TODO: remove dummy. (workaround to make AD thing that lattice is varied) |
| 113 | + @differentiable |
| 114 | + init(count: Int, _ dummy: Tensor<Float>) { |
| 115 | + positions = Array(repeating: Node(), count: count + 1) |
| 116 | + } |
| 117 | + |
| 118 | + public init(positions: [Node]) { |
| 119 | + self.positions = positions |
| 120 | + } |
| 121 | + |
| 122 | + mutating func viterbi(sentence: String) -> [Edge] { |
| 123 | + // Forwards pass |
| 124 | + for position in 0...sentence.count { |
| 125 | + var bestScore = -Float.infinity |
| 126 | + var bestEdge: Edge! |
| 127 | + for edge in self[position].edges { |
| 128 | + let score: Float = self[edge.start].bestScore + edge.logp |
| 129 | + if score > bestScore { |
| 130 | + bestScore = score |
| 131 | + bestEdge = edge |
| 132 | + } |
| 133 | + } |
| 134 | + self[position].bestScore = bestScore |
| 135 | + self[position].bestEdge = bestEdge |
| 136 | + } |
| 137 | + |
| 138 | + // Backwards |
| 139 | + var bestPath: [Edge] = [] |
| 140 | + var nextEdge = self[sentence.count].bestEdge! |
| 141 | + while nextEdge.start != 0 { |
| 142 | + bestPath.append(nextEdge) |
| 143 | + nextEdge = self[nextEdge.start].bestEdge! |
| 144 | + } |
| 145 | + bestPath.append(nextEdge) |
| 146 | + |
| 147 | + return bestPath.reversed() |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +extension Lattice: CustomStringConvertible { |
| 152 | + public var description: String { |
| 153 | + """ |
| 154 | + [ |
| 155 | + \(positions.enumerated().map { " \($0.0): \($0.1)" }.joined(separator: "\n\n")) |
| 156 | + ] |
| 157 | + """ |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +extension Lattice.Node: CustomStringConvertible { |
| 162 | + public var description: String { |
| 163 | + var edgesStr: String |
| 164 | + if edges.isEmpty { |
| 165 | + edgesStr = " <no edges>" |
| 166 | + } else { |
| 167 | + edgesStr = edges.enumerated().map { " \($0.0) - \($0.1)" }.joined(separator: "\n") |
| 168 | + } |
| 169 | + return """ |
| 170 | + best edge: \(String(describing: bestEdge)), best score: \(bestScore), score: \(semiringScore.shortDescription) |
| 171 | + \(edgesStr) |
| 172 | + """ |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +extension Lattice.Edge: CustomStringConvertible { |
| 177 | + public var description: String { |
| 178 | + "[\(start)->\(end)] logp: \(logp), score: \(score.shortDescription), total score: \(totalScore.shortDescription), sentence: \(string)" |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +/// SE-0259-esque equality with tolerance |
| 183 | +extension Lattice { |
| 184 | + public func isAlmostEqual(to other: Self, tolerance: Float) -> Bool { |
| 185 | + guard self.positions.count == other.positions.count else { |
| 186 | + print("positions count mismatch: \(self.positions.count) != \(other.positions.count)") |
| 187 | + return false |
| 188 | + } |
| 189 | + return zip(self.positions, other.positions).enumerated() |
| 190 | + .map { (index, position) in |
| 191 | + let eq = position.0.isAlmostEqual(to: position.1, tolerance: tolerance) |
| 192 | + if !eq { |
| 193 | + print("mismatch at \(index): \(position.0) != \(position.1)") |
| 194 | + } |
| 195 | + return eq |
| 196 | + } |
| 197 | + .reduce(true) { $0 && $1 } |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +extension Lattice.Node { |
| 202 | + public func isAlmostEqual(to other: Self, tolerance: Float) -> Bool { |
| 203 | + guard self.edges.count == other.edges.count else { return false } |
| 204 | + |
| 205 | + if !self.bestScore.isAlmostEqual(to: other.bestScore, tolerance: tolerance) { |
| 206 | + return false |
| 207 | + } |
| 208 | + if let lhs = self.bestEdge, let rhs = other.bestEdge { |
| 209 | + if !lhs.isAlmostEqual(to: rhs, tolerance: tolerance) { |
| 210 | + return false |
| 211 | + } |
| 212 | + } |
| 213 | + if !self.semiringScore.isAlmostEqual(to: other.semiringScore, tolerance: tolerance) { |
| 214 | + return false |
| 215 | + } |
| 216 | + return zip(self.edges, other.edges) |
| 217 | + .map { $0.isAlmostEqual(to: $1, tolerance: tolerance) } |
| 218 | + .reduce(true) { $0 && $1 } |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +extension Lattice.Edge { |
| 223 | + public func isAlmostEqual(to other: Self, tolerance: Float) -> Bool { |
| 224 | + return self.start == other.start && self.end == other.end |
| 225 | + // TODO: figure out why the string equality is being ignored |
| 226 | + // self.string == other.string && |
| 227 | + && self.logp.isAlmostEqual(to: other.logp, tolerance: tolerance) |
| 228 | + && self.score.isAlmostEqual(to: other.score, tolerance: tolerance) |
| 229 | + && self.totalScore.isAlmostEqual(to: other.totalScore, tolerance: tolerance) |
| 230 | + } |
| 231 | +} |
0 commit comments