Skip to content

Instantly share code, notes, and snippets.

View kongzii's full-sized avatar
🤡
Focusing

Peter Jung kongzii

🤡
Focusing
View GitHub Profile
@kongzii
kongzii / contrastive_loss.py
Created November 5, 2020 14:38
Contrastive Loss function in PyTorch
def criterion(x1, x2, label, margin: float = 1.0):
"""
Computes Contrastive Loss
"""
dist = torch.nn.functional.pairwise_distance(x1, x2)
loss = (1 - label) * torch.pow(dist, 2) \
+ (label) * torch.pow(torch.clamp(margin - dist, min=0.0), 2)
loss = torch.mean(loss)
@kongzii
kongzii / Dockerfile
Last active July 21, 2020 17:29
Dockerfile with installted coursier
FROM ubuntu:20.04
RUN apt-get update \
&& apt-get upgrade -y
RUN apt-get install -y curl
RUN cd /root \
&& curl -Lo cs https://git.io/coursier-cli-linux \
&& chmod +x cs \
@kongzii
kongzii / Scala dockerfile
Last active September 15, 2020 12:40
Simple dockerfile for scala
FROM ubuntu:20.04
RUN apt-get update \
&& apt-get upgrade -y
RUN apt-get install -y curl scala
WORKDIR /src
@kongzii
kongzii / DefaultDict.swift
Created June 15, 2020 17:56
Python defaultdict in Swift
public extension Dictionary {
/// - Parameter key: Key to retrieve from self.
/// - Parameter or: Value that will be set for `key` if `self[key]` is nil.
subscript(key: Key, or def: Value) -> Value {
mutating get {
self[key] ?? {
self[key] = def
return def
}()
}
@kongzii
kongzii / s3.swift
Created May 1, 2020 05:41
Download from S3 to local file in Swift
import Foundation
import Logging
import S3
let LOGGER = Logger(label: "S3")
enum S3Error: Error {
case runtimeError(String)
}
@kongzii
kongzii / shell.swift
Created May 1, 2020 05:40
Execute shell commands in Swift from String or Array
import Foundation
public extension Array where Element == String {
@discardableResult
func exec() -> String {
joined(separator: " ").exec()
}
}
public extension String {
@kongzii
kongzii / model.swift
Last active January 30, 2020 07:40
Example of saving trained weights of model in the Swift For Tensorflow
import Foundation
import Python
import TensorFlow
public struct MyModel : Layer {
public var conv1d: Conv1D<Float>
public var dense1: Dense<Float>
public var dropout: Dropout<Float>
public var denseOut: Dense<Float>
@kongzii
kongzii / Queue.swift
Created October 20, 2019 11:04
Thread safe Queue implementation in Swift
import Foundation
public enum QueueType {
case lifo
}
public class Queue<T> {
var maxSize: Int? = nil
var elements: Array<T> = []
var type: QueueType = QueueType.lifo