Skip to content

Instantly share code, notes, and snippets.

View ceshine's full-sized avatar

CeShine Lee ceshine

View GitHub Profile
@ceshine
ceshine / python_in_visual_studio_code.md
Last active August 5, 2019 09:30
How To Develop Python Programs in Visual Studio Code

How To Develop Python Programs in Visual Studio Code

Prerequisites

You have to already have these in your system:

@ceshine
ceshine / bactrader_sample.py
Last active May 4, 2023 12:38
A Simple Strategy Trading Two Stocks (back trader)
"""A Simple Strategy Trading Two Stocks
Original code: https://blog.csdn.net/qq_26948675/article/details/80016633
Modified based on: https://www.backtrader.com/blog/posts/2018-04-22-improving-code/improving-code.html
Replaced the local CSV files with online data from IEX.
Unfortunately, this strategy is not profitable for the two stocks picked.
"""
@ceshine
ceshine / stlr.py
Last active March 5, 2023 12:13
Pytorch Slanted Triangular Learning Rate Scheduler
class STLR(torch.optim.lr_scheduler._LRScheduler):
def __init__(self, optimizer, max_mul, ratio, steps_per_cycle, decay=1, last_epoch=-1):
self.max_mul = max_mul - 1
self.turning_point = steps_per_cycle // (ratio + 1)
self.steps_per_cycle = steps_per_cycle
self.decay = decay
super().__init__(optimizer, last_epoch)
def get_lr(self):
residual = self.last_epoch % self.steps_per_cycle
@ceshine
ceshine / resize_and_pad.py
Created July 7, 2018 05:35
Resize and Pad the image to Square Size
from torchvision import transforms
from PIL import ImageOps
class ResizeAndPad(object):
def __init__(self, size, interpolation=Image.BILINEAR):
assert isinstance(size, int)
self.size = size
self.interpolation = interpolation
def __call__(self, img):
tf.reset_default_graph()
graph = tf.Graph()
with graph.as_default():
tf.set_random_seed(10)
# tf Graph input
X = tf.placeholder("float", [None, timesteps, num_input])
Y = tf.placeholder("float", [None, num_classes])
is_training = tf.placeholder("bool")
# Define weights
@ceshine
ceshine / tcn.py
Created April 2, 2018 02:23
Temporal Convolutional Networks
class TemporalConvNet(tf.layers.Layer):
def __init__(self, num_channels, kernel_size=2, dropout=0.2,
trainable=True, name=None, dtype=None,
activity_regularizer=None, **kwargs):
super(TemporalConvNet, self).__init__(
trainable=trainable, dtype=dtype,
activity_regularizer=activity_regularizer,
name=name, **kwargs
)
self.layers = []
@ceshine
ceshine / temporal_block.py
Last active November 25, 2018 15:03
Temporal Block (for TCNs)
class TemporalBlock(tf.layers.Layer):
def __init__(self, n_outputs, kernel_size, strides, dilation_rate, dropout=0.2,
trainable=True, name=None, dtype=None,
activity_regularizer=None, **kwargs):
super(TemporalBlock, self).__init__(
trainable=trainable, dtype=dtype,
activity_regularizer=activity_regularizer,
name=name, **kwargs
)
self.dropout = dropout
@ceshine
ceshine / causal_conv1d.py
Created April 2, 2018 01:03
Causal Convolution 1D
import tensorflow as tf
class CausalConv1D(tf.layers.Conv1D):
def __init__(self, filters,
kernel_size,
strides=1,
dilation_rate=1,
activation=None,
use_bias=True,
kernel_initializer=None,
@ceshine
ceshine / tvl.R
Last active April 6, 2018 06:17
Talent vs Luck simulation
library(checkpoint)
checkpoint("2018-02-25")
library(ggplot2)
# number of people
N <- 1000
# probability of event interception
P_E <- 0.075
# probability of lucky event
P_L <- 0.5
@ceshine
ceshine / toxic_dataset_v2.py
Last active February 24, 2018 20:49
Improved dataset loader for Toxic Comment dataset from Kaggle
"""Improved dataset loader for Toxic Comment dataset from Kaggle
Tested against:
* Python 3.6
* Numpy 1.14.0
* Pandas 0.22.0
* PyTorch 0.4.0a0+f83ca63 (should be very close to 0.3.0)
* torchtext 0.2.1
* spacy 2.0.5
* joblib 0.11
"""