Skip to content

Instantly share code, notes, and snippets.

@jovianlin
jovianlin / keras_multiple_inputs_n_outputs.py
Created November 23, 2017 16:38
Keras: multiple inputs & outputs
from keras.models import Model
from keras.layers import Input, Dense
input_1 = Input(..., name='input_1')
x = Dense(...)(input_1)
x = Dense(...)(x)
...
output_1 = Dense(dim_output_1, ..., name='output_1')
input_2 = Input(..., name='input_2')
@jovianlin
jovianlin / ghost-custom
Created November 22, 2017 02:33
Ghost Injection for Header & Footer
===== BLOG HEADER =====
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/styles/agate.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/highlight.min.js"></script>
<style>
.inner { max-width:50em; }
pre .lines { margin-top:-12px; }
</style>
n = int(input())
L = list(map(int, input().strip().split()))
X = [0] * n
count = 0
for i, x in enumerate(L):
start = (i+1)%n # OK to START collection at the pos after the "i"
end = (start - x + n)%n # WORST place to start the collection
X[start] += 1
X[end] -= 1
@jovianlin
jovianlin / check_tqvm.py
Created October 20, 2017 03:28
Use TQDM if installed
# Use TQDM if installed
tqdm_installed = False
try:
from tqdm import tqdm
tqdm_installed = True
except:
pass
@jovianlin
jovianlin / .bash_profile
Last active August 5, 2017 07:36
~/.bash_profile
##############################
# Tell ``ls`` to be colorful #
##############################
export CLICOLOR=1
COLOR_BOLD="\[\e[1m\]"
COLOR_DEFAULT="\[\e[0m\]"
#PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$\[\033[00m\]'
#PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$\[\033[00m\]'
PS1="\[\033[32m\]\u\[\033[31m\]:\[\033[34m\]\w⚡ \[\033[00m\] "
@jovianlin
jovianlin / .tmux.conf
Last active February 4, 2023 16:06
~/.tmux.conf for iTerm2
###########################
# ~/.tmux.conf for iTerm2 #
# #
# by Jovian Lin #
###########################
##########
# Basics #
##########
@jovianlin
jovianlin / fix_urllib.py
Last active May 10, 2017 15:21
Fix urllib for Python 2 and 3
from __future__ import print_function
import sys
if sys.version_info[0] >= 3:
from urllib.request import urlretrieve
else:
from urllib import urlretrieve
local_filename, headers = urlretrieve('http://jovianlin.com')
html = open(local_filename)
@jovianlin
jovianlin / colorful_numbers.py
Created May 6, 2017 15:05
Colourful Numbers with random, sys, time, and termcolor
import random, sys, time
from termcolor import colored
colors = ['grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', None]
generated = ''
sys.stdout.write(generated)
for i in range(100):
sys.stdout.write(colored('%02d ' % i, random.choice(colors)))
sys.stdout.flush()
@jovianlin
jovianlin / is_kernel.py
Created April 27, 2017 14:58
Determine if we're in an IPython notebook session
import sys
def is_kernel():
"""
Determine if we're in an IPython notebook session
Source: Source: http://stackoverflow.com/a/34092072
-------------------------------------------------
You can't detect that the frontend is a notebook with perfect precision,
because an IPython Kernel can have one or more different Jupyter frontends
with different capabilities (terminal console, qtconsole, notebook, etc.).
@jovianlin
jovianlin / pyspark_sample.py
Created April 21, 2017 13:34
sample code for pyspark
from pyspark import SparkContext
from pyspark.sql import HiveContext
from graphframes.examples import Graphs
sc = SparkContext()
sc.setLogLevel("ERROR")
sqlContext = HiveContext(sc)
g = Graphs(sqlContext).friends() # Get example graph