Skip to content

Instantly share code, notes, and snippets.

View maxrohleder's full-sized avatar
🏠
Working from home

Maximilian Rohleder maxrohleder

🏠
Working from home
View GitHub Profile
@maxrohleder
maxrohleder / keras-sequential.py
Last active February 7, 2021 22:28
Three different approaches to design a model in keras.
from tensorflow.keras import layers, models
# define the model
model = models.Sequential(name="Keras Test CNN")
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
@maxrohleder
maxrohleder / ditch-for-loops.py
Last active March 11, 2021 08:56
Square signal without for loops
import numpy as np
# first define our input values and all values for k
t = np.linspace(0, 1, 200)
k = np.arange(0, 1000)
f = 10 # frequency in Hertz
# from the formula, isolate all factors in the sinus term which include k
k = 2 * np.pi * (2 * k - 1) * f
@maxrohleder
maxrohleder / advanced-indexing.py
Created November 28, 2020 14:38
advanced indexing lets you manipulate arrays elegantly
import numpy as np
# some sample data in shaped cubic (100, 100, 100)
img = np.random.sample((100, 100, 100))
# set all values between 0.5 and 0.6 to zero
img[(img < 0.6) & (img > 0.5)] = 0
# in a small sub cube (10, 10, 10)..
# .. get index arrays of all elements above 0.6
@maxrohleder
maxrohleder / simplify-loops.py
Created November 28, 2020 13:37
use array enumeration to simplify for loops
import numpy as np
img = np.arange(6).reshape(2, 3)
# looping over all elements like this ...
for x in range(img.shape[0]):
for y in range(img.shape[1]):
print(img[x, y])
# ... is easier and more readable with numpy
@maxrohleder
maxrohleder / basic-indexing.py
Last active July 23, 2021 06:58
smart indexing can save you a lot of looping
a = np.array([[0, 1, 2],
[3, 4, 5]])
b = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# subsetting of the array (start is included, end is excluded)
print(a[0:2, 0:2])
#> [[0 1]
#> [3 4]]
# striding / stepping "every second entry"