Last active
December 12, 2019 19:18
-
-
Save OverLordGoldDragon/4d32dffd8f5d9123a886c21558da60a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.layers import Input, Dense, LSTM, Conv1D, Activation | |
from keras.layers import Dropout, AlphaDropout, BatchNormalization | |
from keras.layers import GlobalAveragePooling1D, Reshape, multiply | |
from keras.models import Model | |
import keras.backend as K | |
import numpy as np | |
import matplotlib.pyplot as plt | |
############################################################################# | |
def make_model(batch_shape): | |
ipt = Input(batch_shape=batch_shape) | |
x = Conv1D(20, 4, activation='tanh')(ipt) | |
x = Dropout(0.2, noise_shape=(batch_shape[0], 1, K.int_shape(x)[-1]))(x) | |
x = GlobalAveragePooling1D()(x) | |
out = Dense(1, activation='sigmoid')(x) | |
model = Model(ipt, out) | |
model.compile('adam', 'binary_crossentropy') | |
return model | |
def make_data(batch_shape): | |
return (np.random.randn(*batch_shape), | |
np.random.randint(0, 2, (batch_shape[0], 1))) | |
def get_layer_outputs(model, layer_idx, input_data, learning_phase=0): | |
layer = model.layers[layer_idx] | |
layers_fn = K.function([model.input, K.learning_phase()], [layer.output]) | |
return layers_fn([input_data, learning_phase])[0] | |
############################################################################# | |
batch_shape = (32, 100, 16) | |
model = make_model(batch_shape) | |
x, y = make_data(batch_shape) | |
model.train_on_batch(x, y) | |
############################################################################# | |
outs = get_layer_outputs(model, 2, x, 1) | |
plt.imshow(outs[0].T, cmap='bwr', vmin=-1, vmax=1) | |
plt.title("Conv1D-Dropout w/ noise_shape=(batch_size, 1, channels)", weight='bold') | |
plt.xlabel("Timesteps", weight='bold') | |
plt.ylabel("Channels", weight='bold') | |
plt.gcf().set_size_inches(10, 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment