Skip to content

Instantly share code, notes, and snippets.

View steven-mi's full-sized avatar
🍗
Eating

Steven steven-mi

🍗
Eating
View GitHub Profile
def softmax(class_scores):
"""
Calculate class probability distribution for each digit from given class scores.
:param class_scores: class scores of your function
:return: probability distribution
"""
class_scores -= np.max(class_scores)
return np.exp(class_scores) / np.sum(np.exp(class_scores),axis=1, keepdims=True)
@steven-mi
steven-mi / fully-connected-network.py
Last active April 13, 2019 18:07
A 3 layered neural network with ReLU as activation function.
# this implementation was given as assignment 3 of the course
# B55.2 WT Ausgewählte Kapitel sozialer Webtechnologien at HTW Berlin
# third party
import numpy as np
import matplotlib.pyplot as plt
# internal
from deep_teaching_commons.data.fundamentals.mnist import Mnist
@steven-mi
steven-mi / video_to_image.py
Created April 9, 2019 12:16
convert a video to images with opencv
import cv2
# open video
video = cv2.VideoCapture('video.mp4')
# get fps and duration
fps = video.get(cv2.CAP_PROP_FPS) # OpenCV2 version 2 used "CV_CAP_PROP_FPS"
frameCount = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
duration = frameCount/fps
@steven-mi
steven-mi / minibatcher.py
Last active October 12, 2020 14:36
a python minibatcher
def minibatcher(inputs, targets, batchsize, shuffle=False):
assert len(inputs) == len(targets)
if shuffle:
indices = np.arange(len(inputs))
np.random.shuffle(indices)
for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
if shuffle:
excerpt = indices[start_idx:start_idx + batchsize]
else:
excerpt = slice(start_idx, start_idx + batchsize)
@steven-mi
steven-mi / layers.py
Created April 13, 2019 18:03
tensorflow conv layer with activation function and bn
import tensorflow as tf
def get_weight(shape, name, trainable=True):
#initial = tf.random_uniform(shape, minval=-0.1, maxval = 0.1)
initial = tf.contrib.layers.xavier_initializer()(shape)
return tf.Variable(initial, trainable=trainable, name=name+'_W', dtype=tf.float32)
def get_bias(shape, name, trainable=True):
"""
filter_height, filter_width, in_channels, out_channels]
@steven-mi
steven-mi / pyplot_loop.py
Created April 16, 2019 07:42
plotting a pyplot in a for loop
import matplotlib.pyplot as plt
# plot 0 plot 1 plot 2 plot 3
x=[[1,2,3,4],[1,4,3,4],[1,2,3,4],[9,8,7,4]]
y=[[3,2,3,4],[3,6,3,4],[6,7,8,9],[3,2,2,4]]
plots = zip(x,y)
figs={}
axs={}
for idx,plot in enumerate(plots):
figs[idx]=plt.figure()
export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_9_HOME=$(/usr/libexec/java_home -v9)
alias java7='export JAVA_HOME=$JAVA_7_HOME'
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java9='export JAVA_HOME=$JAVA_9_HOME'
#default java8
export JAVA_HOME=$JAVA_8_HOME
@steven-mi
steven-mi / docker.md
Created April 23, 2019 13:00
docker cheatsheet

remove Docker container

docker rm tf

stop the container

docker stop tf

How to install virtualenv:

Install pip first

sudo apt-get install python3-pip

Then install virtualenv using pip3

sudo pip3 install virtualenv 
@steven-mi
steven-mi / gist:8bc4d685597a10b0b3e9809474d9c2c7
Created October 15, 2019 08:38
pyplot for loop without index
plt.figure(figsize=(20, 20))
num_classes = 10
for c in range(num_classes):
# Select samples_per_class random keys of the labels == current class
keys = np.random.choice(np.where(label == c)[0], examples_each_row)
images = data[keys]
labels = label[keys]
for i in range(examples_each_row):
f = plt.subplot(examples_each_row, num_classes, i * num_classes + c + 1)