QMIND-Team / QMIND-Music

All the cool projects from the 2019 QMIND Music team :musical_keyboard: :musical_note: :musical_score: :saxophone: :guitar: :violin: :microphone: !
http://qmusic.org
5 stars 0 forks source link

[Data Vis] Create a visualization of a Keras LSTM #36

Closed joeytepp closed 5 years ago

joeytepp commented 5 years ago

Summary

Use ANN_visualizer to visualize an LSTM neural network in Keras

Basic Example

You can use the basic LSTM in the snippet below to test, before creating a generic method to visualize any model (ie. a function called visualize_model)

import numpy as np

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, LSTM

input_data = np.array([
    [[1, 0], [2, 0], [3, 0], [4, 0]],
    [[1, 0], [2, 0], [3, 0], [4, 0]],
    [[1, 0], [2, 0], [3, 0], [4, 0]],
    [[1, 0], [2, 0], [3, 0], [4, 0]],
    [[1, 0], [2, 0], [3, 0], [4, 0]],
    [[1, 0], [2, 0], [3, 0], [4, 0]],
    [[1, 0], [2, 0], [3, 0], [4, 0]],
    [[1, 0], [2, 0], [3, 0], [4, 0]],
    [[1, 0], [2, 0], [3, 0], [4, 0]],
    [[1, 0], [2, 0], [3, 0], [4, 0]]
])

output_data = np.array([
    [1, 2, 3, 4],
    [1, 2, 3, 4],
    [1, 2, 3, 4],
    [1, 2, 3, 4],
    [1, 2, 3, 4],
    [1, 2, 3, 4],
    [1, 2, 3, 4],
    [1, 2, 3, 4],
    [1, 2, 3, 4],
    [1, 2, 3, 4],
])

model = Sequential()
model.add(LSTM(
    512,
    input_shape=(4, 2),
    return_sequences=True
))
model.add(Dropout(0.3))
model.add(LSTM(512, return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(512))
model.add(Dense(256))
model.add(Dropout(0.3))
model.add(Dense(4))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

model.fit(input_data, output_data, batch_size=64, epochs=200)

Motivation

To get an idea of what's actually going on behind the scenes 🤖

joeytepp commented 5 years ago

Can close this now 👌