This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SeparableConv2d(torch.nn.Module): | |
| def __init__(self, | |
| in_channels, | |
| out_channels, | |
| kernel_size=3, | |
| stride=1, | |
| padding=0, | |
| dilation=1, | |
| bias=True, | |
| padding_mode='zeros', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| class DepthwiseConv2d(torch.nn.Conv2d): | |
| def __init__(self, | |
| in_channels, | |
| depth_multiplier=1, | |
| kernel_size=3, | |
| stride=1, | |
| padding=0, | |
| dilation=1, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Array where Element: Comparable { | |
| func argmax() -> Index? { | |
| return indices.max(by: { self[$0] < self[$1] }) | |
| } | |
| func argmin() -> Index? { | |
| return indices.min(by: { self[$0] < self[$1] }) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension ObservableType { | |
| func takeWhileInclusive(predicate: @escaping (Element) throws -> Bool) -> Observable<Element> { | |
| let source = self.share() | |
| let pass = source.map(predicate).startWith(true) | |
| return Observable.zip(source, pass){ ($0, $1) } | |
| .takeWhile { $0.1 } | |
| .map { $0.0 } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Array { | |
| func group<Key: Hashable>(by transform: (Element) -> Key) -> [Key: [Element]]{ | |
| return self.reduce(into: [:]) { dict, current -> () in | |
| let key = transform(current) | |
| var bucket: [Element] = dict[key] ?? [] | |
| bucket.append(current) | |
| dict[key] = bucket | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| According to the documentation of Swift's standard library, areInIncreasingOrder closure argument of sorted method of sequence type must hold some properties. One of them is that areInIncreasingOrder(a,a) must be false. | |
| From documentation, | |
| "areInIncreasingOrder: A predicate that returns `true` if its first argument should be ordered before its second argument ..." | |
| Swift can check equality by applying this predicate twice with arguments swapped. If it gives false in both case, then elements are equal. | |
| */ | |
| enum Comparison { | |
| case less, equal, greater | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // from this stackoverflow post | |
| // https://stackoverflow.com/a/48272567/6641096 | |
| extension Array { | |
| public func stablePartition(by condition: (Element) throws -> Bool) rethrows -> ([Element], [Element]) { | |
| var indexes = Set<Int>() | |
| for (index, element) in self.enumerated() { | |
| if try condition(element) { | |
| indexes.insert(index) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public final class WeakRef<T: AnyObject> { | |
| weak var object: T? | |
| init(_ object: T) { | |
| self.object = object | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| /** | |
| viewModel.message.observe(owner: self, Observer { (message: String)->() in | |
| DispatchQueue.main.async { | |
| self.label.text = message | |
| } | |
| DispatchQueue.main.asyncAfter(deadline: .now() + DispatchTimeInterval.seconds(1)) { | |
| self.label.text = "tick: \(self.viewModel.ticker.value)" | |
| } | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import RxSwift | |
| extension ObservableType { | |
| func filterWithLatestFrom<Source: ObservableType>(_ second: Source) -> Observable<Element> where Source.Element == Bool { | |
| return self.withLatestFrom(second) { ($0, $1) } | |
| .filter { $0.1 } | |
| .map { $0.0 } | |
| } | |
| } |