Skip to content

Instantly share code, notes, and snippets.

View cshjin's full-sized avatar

Hongwei Jin cshjin

View GitHub Profile
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 28 15:28:15 2015
"""
from sklearn.cluster import KMeans
import numpy as np
import pandas
dataset = np.array([1, 4, 5, 6, 9])
km = KMeans(n_clusters=3)
clear;
has_quadprog = exist( 'quadprog' ) == 2 | exist( 'quadprog' ) == 3;
has_linprog = exist( 'linprog' ) == 2 | exist( 'linprog' ) == 3;
rnstate = randn( 'state' ); randn( 'state', 1 );
s_quiet = cvx_quiet(true);
s_pause = cvx_pause(false);
cvx_clear; echo on
b = [2; 0; 2; 0];
cvx_begin
@cshjin
cshjin / gradient_descent_convergence.m
Last active September 10, 2017 01:49
Gradient descent example
%% demonstrate the linear convergence rate of gradient descent
stepsize = 0.05;
x = rand(2, 1);
f_diff = 1;
f = @(x) 1/2*(x(1)^2 + 10 * x(2)^2);
first_grad = @(x) [x(1); 10*x(2)];
iter = 0;
fprintf('ITER \t F_VAL \t F_VAL_U \t F_DIFF \n');
% fprintf('ITER \t\t F_VAL \t F_VAL_U \t F_DIFF \t F_GRAD \t F_GRAD_U \n');
@cshjin
cshjin / saddle_point.m
Created September 9, 2017 21:32
Saddle point example
v = -5:0.5:5;
[x, y] = meshgrid(v);
z = x .* exp(x.^2 - y.^2) + y.*exp(-x.^2 + y.^2);
[px, py] = gradient(z);
figure
surf(x, y, z)
figure
contour(x, y, z);
hold on
quiver(x, y, px, py);
@cshjin
cshjin / subl.reg
Created September 17, 2017 06:26
Create the right click for sublime 3 on Windows, adjust your subl path accordingly.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\Edit with Sublime Text]
@="Edit with &Sublime Text"
"Icon"="C:\\Program Files\\Sublime Text 3\\sublime_text.exe,0"
"MuiVerb"="Edit with Sublime Text"
[HKEY_CLASSES_ROOT\*\shell\Edit with Sublime Text\command]
@="C:\\Program Files\\Sublime Text 3\\sublime_text.exe \"%1\""
@cshjin
cshjin / power_method.py
Created September 22, 2018 23:28
Power method
import numpy as np
import sys
# reference: https://en.wikipedia.org/wiki/Power_iteration
def power_method(M):
b = np.random.rand(M.shape[1])
diff = np.linalg.norm(b)
while diff > 10 ** (-6):
b_new = np.dot(M, b)
b_norm = np.linalg.norm(b_new)
@cshjin
cshjin / tf_autograd.py
Created January 16, 2019 01:13
Verify the tensorflow autogradient and the gradient w.r.t matrix
import numpy as np
import pickle
import matplotlib.pyplot as plt
import scipy.io as sio
import tensorflow as tf
tf.enable_eager_execution()
tmm = tf.matmul
def _test():
@cshjin
cshjin / matfac.m
Last active September 27, 2019 02:23
Matrix factorization problem implemented in Tensorflow and Matlab (with yalmip)
yalmip('clear')
T = [0 1 0 1; 1 0 1 0; 0 1 0 1; 1 0 1 0];
x = sdpvar(4, 1);
y = sdpvar(4, 1);
assign(x, randn(4, 1));
assign(y, randn(4, 1));
const = [x'*x <= 1; y'*y <=1];
% const = [];
@cshjin
cshjin / minimax.py
Last active December 1, 2021 03:13
Solve a minimax problem in tensorflow
""" Solve a minimax problem
The problme is defined as
$$ \min_{x} \max_{y} f(x, y) = x^2(y+1) + y^2(x+1)$$
The first order gradient is
$$ \frac{\partial f}{\partial x} = 2x(y+1) + y^2 $$
$$ \frac{\partial f}{\partial y} = x^2 + 2y(x+1) $$
From the first order optimality condition, the alternatively solver
should solve the problem and converge to a stationary point.
@cshjin
cshjin / kl.py
Last active July 11, 2019 17:12
KL divergence in Numpy
# -*- coding: utf-8 -*-
#!/bin/env python
import numpy as np
import unittest
def softmax(x):
""" Given a vector, apply the softmax activation function
Parameters