<jupyter_start><jupyter_text>Transform CSVs to Keras Input<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pickle
from pathlib import Path
import seaborn as sns
from tqdm.notebook import trange, tqdm

import wandb

from data import get_voltage_series

from keras.layers import Dense, LSTM
from keras.models import Sequential
from keras.utils import to_categorical

sns.set()

DATA_DIR = Path('../data')
seawater = pd.read_csv(DATA_DIR / 'seawater.csv', index_col=0)
seawater<jupyter_output><empty_output><jupyter_text>Save Index/Voltage Mappings<jupyter_code>index_to_voltage_mapping = seawater.voltage.to_dict()
voltage_to_index_mapping = {v: i for i, v in index_to_voltage_mapping.items()}
with open(DATA_DIR / 'index_to_voltage_mapping.pkl', 'wb') as f:
    pickle.dump(index_to_voltage_mapping, f)
    
with open(DATA_DIR / 'voltage_to_index_mapping.pkl', 'wb') as f:
    pickle.dump(voltage_to_index_mapping, f)
with open(DATA_DIR / 'voltage_to_index_mapping.pkl', 'rb') as f:
    vi = pickle.load(f)
    
with open(DATA_DIR / 'index_to_voltage_mapping.pkl', 'rb') as f:
    iv = pickle.load(f)
iv<jupyter_output><empty_output><jupyter_text>Transform To Keras Input  Using Full Sequence as input (len 1002)<jupyter_code>seawater.T.values[1:].shape
# Drop the first row as that is the 'voltage' row
full_input_arrays = np.expand_dims(seawater.T.values[1:], 2)
full_input_arrays.shape
def build_model():
    model = Sequential([
        LSTM(10, batch_input_shape=(10, 1002, 1)),
        Dense(4, activation='sigmoid')
    ])
    model.compile(loss='categorical_crossentropy',
                 optimizer='rmsprop',
                 metrics=['accuracy'])
    return model
y_train = np.array([np.random.choice(4) for _ in range(len(full_input_arrays))])
y_train_ohe = to_categorical(y_train)
model = build_model()
model.fit(full_input_arrays,
         y_train_ohe,
         epochs=3,
         batch_size=10,
         shuffle=False)<jupyter_output>Epoch 1/3
80/80 [==============================] - 2s 19ms/step - loss: 1.3811 - accuracy: 0.3375
Epoch 2/3
80/80 [==============================] - 1s 16ms/step - loss: 1.3755 - accuracy: 0.3375
Epoch 3/3
80/80 [==============================] - 1s 16ms/step - loss: 1.3725 - accuracy: 0.3375<jupyter_text>Not Using Full Seq Length as Input<jupyter_code>seawater.shape
seawater_values_we_want_in_rows = seawater.T[1:]
seawater_values_we_want_in_rows.shape
SEQ_LENGTH = 1002
num_chunks = 3 # or 1, 2, 6 (as these perfectly divide 1002)
num_full_length_sequences = seawater_values_we_want_in_rows.shape[0]
# We multiply the number of rows by num_chunks as we are dividing 
num_new_rows = num_full_length_sequences * num_chunks
num_new_cols = int(SEQ_LENGTH / num_chunks)
# num_new_cols must be a whole number
assert SEQ_LENGTH % num_chunks == 0
final_shape = (num_new_rows, num_new_cols)
final_shape
seawater_values_we_want_in_rows.head()
# This works... but how can I feed it into the model?
seawater_3_rows_per_seq = seawater_values_we_want_in_rows.values.reshape(-1, 334, 1)
seawater_3_rows_per_seq.shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12695874640230254, 'fraction_numerical': 0.03229932842980492, 'mean_word_length': 5.097465886939571, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '157136', 'license': 'Apache-2.0', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1144, 'n_words': 254, 'path': 'electrochem/transform_csvs_to_keras_input.ipynb', 'repo_name': 'codeananda/sequence_classification_tensorflow', 'size': '3127'}