<jupyter_start><jupyter_text>Recurrent Neural Network representation of C. elegans neural timeseries dataScript to model C. elegans neural timeseries data,and generate similar data, using a RecurrentNeural Network (specifically, a long short term memory or LSTM network)Inspired byhttp://karpathy.github.io/2015/05/21/rnn-effectiveness/Based on code fromhttps://github.com/fchollet/keras/blob/master/examples/lstm_text_generation.py<jupyter_code>from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.utils.data_utils import get_file
import numpy as np
import random
import sys
import os

%matplotlib inline
import matplotlib.pyplot as plt

os.environ["THEANO_FLAGS"] = "mode=FAST_RUN,device=gpu,floatX=float32"

#Add the src folder to the sys.path list
sys.path.append('../src/')
import data_config as dc
wormData = dc.kato.retrieve()
wormData
# Choose a trial arbitrarily
# Let's try with derivative data

seq = wormData[0]["deltaFOverF_deriv"].T
plt.figure(figsize=(20,20))
plt.pcolor(seq.T)
plt.tight_layout()
# Cut the timeseries data (variable name 'seq') into semi-redundant sequence chunks of maxlen
maxlen = 10

X = []
y = []

for i in range(0, len(seq) - maxlen):
    X.append(seq[i:i+maxlen])
    y.append(seq[i+maxlen])

dim = len((X[0][0]))

print("sequence chunks:", len(X))
print("chunk width:", len(X[0]))
print("vector dimension:", dim)

X = np.array(X)
y = np.array(y)
# build the model: 2 stacked LSTM
print('Build model...')
model = Sequential()
model.add(LSTM(512, return_sequences=True, input_shape=(maxlen, dim)))
model.add(Dropout(0.2))
model.add(LSTM(512, return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(dim))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
# Train the model
model.fit(X, y, batch_size=128, nb_epoch=50)
# Generate timeseries
x_seed = X[len(X)-1] #choose final in-sample data point to initialize model
x_array = []
x_array.append(x_seed)
x = np.array(x_array)

generated = []
for i in range(50):
    pred = model.predict(x, verbose=0)[0].tolist()

    #drop oldest data in x, and append predicted data for feedforward into model
    j = []
    x = []
    for i in range(1, len(x_seed)):    
        j.append(x_seed[i])
    j.append(pred)
    x = []
    x.append(j)
    x = np.array(x)
    generated.append(pred)

generated = np.array(generated)
plt.figure(figsize=(20,20))
plt.pcolor(generated.T)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13409810126582278, 'fraction_numerical': 0.018591772151898733, 'mean_word_length': 5.138349514563107, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '381662', 'license': 'MIT', 'n_tokens_mistral': 928, 'n_tokens_neox': 880, 'n_words': 226, 'path': 'laboratory/.ipynb_checkpoints/RecurrentNeuralNetwork-checkpoint.ipynb', 'repo_name': 'openworm/neuronal-analysis', 'size': '2528'}