Created
November 21, 2018 07:59
-
-
Save miracleyoo/66c8d05059d3dc592d065d0c868f5b52 to your computer and use it in GitHub Desktop.
[Simple keras BiLSTM Attention Model Pipeline] #python #keras
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 import Sequential | |
from keras.preprocessing.sequence import pad_sequences | |
from sklearn.model_selection import train_test_split | |
from keras.models import Sequential,Model | |
from keras.layers import LSTM, Dense, Bidirectional, Input,Dropout,BatchNormalization | |
from keras import backend as K | |
from keras.engine.topology import Layer | |
from keras import initializers, regularizers, constraints | |
# Definition of model | |
model = Sequential() | |
model.add(BatchNormalization(input_shape=(10, 128))) | |
model.add(Bidirectional(LSTM(256, dropout=0.4, recurrent_dropout=0.4, activation='relu', return_sequences=True))) | |
model.add(Attention(10)) | |
model.add(Dense(1,activation='sigmoid')) | |
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) | |
print(model.summary()) | |
#fit on a portion of the training data, and validate on the rest | |
model.fit(x_train, y_train, | |
batch_size=300, | |
epochs=50, | |
validation_data=(x_val, y_val)) | |
# Evaluation | |
score, acc = model.evaluate(x_val, y_val, batch_size=256) | |
print('Test accuracy:', acc) | |
# Prediction | |
test_data = test['audio_embedding'].tolist() | |
submission = model.predict(pad_sequences(test_data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment