Skip to content

Instantly share code, notes, and snippets.

View changkun's full-sized avatar
🏠
https://changkun.de

Changkun Ou changkun

🏠
https://changkun.de
View GitHub Profile
@changkun
changkun / mlnotifications.py
Created March 21, 2017 16:06 — forked from baliw/mlnotifications.py
Mountain Lion Notification Center via Python
import Foundation
import objc
import AppKit
import sys
NSUserNotification = objc.lookUpClass('NSUserNotification')
NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
def notify(title, subtitle, info_text, delay=0, sound=False, userInfo={}):
notification = NSUserNotification.alloc().init()
@changkun
changkun / wordcount_mapreduce.py
Created June 5, 2017 22:29
Word Count MapReduce
# wordcount
import string
from functools import reduce
str = """\
How much ground would a groundhog hog, \
if a groundhog could hog ground? \
A gfoundhog would hog all the ground he could hog, \
if a groundhog could hog ground. \
"""
@changkun
changkun / settings.json
Created June 16, 2017 06:07
My VSCode Settings
{
// general editor settings
"terminal.external.osxExec": "iTerm.app",
"workbench.colorTheme": "Material Theme",
"workbench.iconTheme": "material-theme-icons",
"vsicons.projectDetection.autoReload": true,
"editor.lineNumbers": "on",
"editor.fontSize": 12,
"editor.rulers": [90, 120],
"editor.minimap.enabled": true,
@changkun
changkun / linear-regression.py
Created June 27, 2017 06:11
Analytic solution for Linear Regression, implemented by Python
# 使用 numpy 手动实现实现
import numpy as np
# 生成随机数据集
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)
# 函数说明:
# 1. np.c_[np.array([1,2,3]), np.array([4,5,6])] --> array([[1, 4],[2, 5],[3, 6]])
# 2. np.ones((2, 1)) --> array([[ 1.], [ 1.]])
@changkun
changkun / gradient-descent.py
Created June 27, 2017 06:45
批量梯度下降、随机梯度下降、小批量梯度下降法
# -- coding: utf-8 --
import numpy as np
import matplotlib.pyplot as plt
# 生成随机数据集
np.random.seed(42)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)
X_b = np.c_[np.ones((100, 1)), X]
X_new = np.array([[0], [2]])
@changkun
changkun / pca.py
Last active July 7, 2017 15:02
PCA
#!/usr/bin/env python3
import numpy as np
from numpy import linalg as LA
import matplotlib.pyplot as plt
# 1. calculate mu
# X = np.array([
# [-1, 1],
# [ 0, 0],
@changkun
changkun / Attention.py
Created December 28, 2017 17:47 — forked from luthfianto/Attention.py
Keras Layer that implements an Attention mechanism for temporal data. Supports Masking. Follows the work of Raffel et al. [https://arxiv.org/abs/1512.08756]
from keras.layers.core import Layer
from keras import initializers, regularizers, constraints
from keras import backend as K
class Attention(Layer):
def __init__(self,
kernel_regularizer=None, bias_regularizer=None,
kernel_constraint=None, bias_constraint=None,
use_bias=True, **kwargs):
"""
@changkun
changkun / darc1_loss.py
Created December 30, 2017 17:00
DARC1 Loss on MNIST, Keras Implementation
from keras.datasets import mnist
from keras.utils import to_categorical
from keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense, Activation, BatchNormalization
from keras.models import Model
from keras import backend as K
# 1. load data
def load_data():
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
@changkun
changkun / test.sh
Created June 28, 2018 15:46
Zero downtime checking
while true; do curl -o /dev/null -sw "%{http_code}\n" https://labex.io; sleep .5; done
@changkun
changkun / monitor.go
Created August 7, 2018 08:27
go monitor
package main
import (
"encoding/json"
"fmt"
"runtime"
"time"
)
type Monitor struct {