Skip to content

Instantly share code, notes, and snippets.

View RobertTLange's full-sized avatar
🎯
Focusing

Robert Tjarko Lange RobertTLange

🎯
Focusing
View GitHub Profile
@RobertTLange
RobertTLange / twitter.py
Last active November 15, 2024 17:09
Twitter API Example
import tweepy
import os
from typing import List, Optional
class TwitterClient:
def __init__(
self,
consumer_key: str,
consumer_secret: str,
@RobertTLange
RobertTLange / jax_ou_process.py
Created March 5, 2021 19:32
Ornstein-Uhlenbeck Process in JAX
import jax
import jax.numpy as jnp
def ou_process(key, steps, dt, mu, tau, sigma):
""" Generate an Ornstein-Uhlenbeck process sample. """
ou_init = jnp.zeros((steps + 1, ))
noise = jax.random.normal(key, (steps,))
def ou_step(t, val):
dx = (-(val[t-1]-mu)/tau * dt
+ sigma*jnp.sqrt(2/tau)*
def binary_cross_entropy(y_true, y_pred):
y_true = np.append(1 - y_true, y_true, axis=1)
y_pred = np.append(1 - y_pred, y_pred, axis=1)
bce = -(y_true * np.log(y_pred)).sum(axis=1)
return bce
def train_logistic_regression(n, d, n_epoch, batch_size, b_init, l_rate):
# Generate the data for a coefficient vector & init progress tracker!
data_loader = DataLoader(n, d, batch_size, binary=True)
b_hist, func_val_hist, param_error, acc_hist = [], [], [], []
# Get the coefficients as solution to optimized sklearn function
logreg = LogisticRegression(penalty='none', solver='lbfgs', multi_class='multinomial')
logreg.fit(data_loader.X, data_loader.y)
norm_coeff = np.linalg.norm(logreg.coef_.ravel())
@RobertTLange
RobertTLange / dual_logistic_regression.py
Created August 29, 2019 14:33
Logistic Regression with SGD using Forward cumulative mode + Dual Numbers
import numpy as np
from sklearn.linear_model import LogisticRegression
class DataLoader(object):
# Small class object for dual number - Two real numbers (real & dual part)
def __init__(self, n, d, batch_size, binary=False):
self.total_dim = d + 1
self.X, self.y = self.generate_regression_data(n, d, binary)
# Set batch_id for different indices