<jupyter_start><jupyter_text><jupyter_code>note_seq = ['g8', 'e8', 'e4', 'f8', 'd8', 'd4', 'c8', 'd8', 'e8', 'f8', 'g8', 'g8', 'g4',
       'g8', 'e8', 'e8', 'e8', 'f8', 'd8', 'd4', 'c8', 'e8', 'g8', 'g8', 'e8', 'e8', 'e4',
       'd8', 'd8', 'd8', 'd8', 'd8', 'e8', 'f4', 'e8', 'e8', 'e8', 'e8', 'e8', 'f8', 'g4',
       'g8', 'e8', 'e4', 'f8', 'd8', 'd4', 'c8', 'e8', 'g8', 'g8', 'e8', 'e8', 'e4']
note_seq[0:5], note_seq[1:6], note_seq[2:7]
code2idx = {'c4':0, 'd4':1, 'e4':2, 'f4':3, 'g4':4, 'a4':5, 'b4':6,
            'c8':7, 'd8':8, 'e8':9, 'f8':10, 'g8':11, 'a8':12, 'b8':13}
len(note_seq), range(len(note_seq)-5)
dataset = list()
for i in range(len(note_seq)-4):
  # print(note_seq[i:i+5])
  subset = note_seq[i:i+5]
  items = list()
  for item in subset:
    code2idx[item]
    items.append(code2idx[item])
  # print(items)
  dataset.append(items)
print(dataset)
import numpy as np
datasets = np.array(dataset)
x_train = datasets[:,:4]
x_train.shape, x_train
y_train = datasets[:,4]
y_train.shape, y_train
x_train = x_train / 13
x_train[2] 
X_train = np.reshape(x_train, (50, 4, 1))
X_train.shape, X_train[2]
import tensorflow as tf
model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(4, 1)))
model.add(tf.keras.layers.LSTM(128))
model.add(tf.keras.layers.Dense(14, activation='softmax'))

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc'])
model.fit(X_train, y_train, epochs=1000, batch_size=10)<jupyter_output>Epoch 1/1000
5/5 [==============================] - 2s 6ms/step - loss: 2.6242 - acc: 0.2200
Epoch 2/1000
5/5 [==============================] - 0s 5ms/step - loss: 2.5727 - acc: 0.3400
Epoch 3/1000
5/5 [==============================] - 0s 5ms/step - loss: 2.5099 - acc: 0.3400
Epoch 4/1000
5/5 [==============================] - 0s 5ms/step - loss: 2.4410 - acc: 0.3400
Epoch 5/1000
5/5 [==============================] - 0s 5ms/step - loss: 2.3591 - acc: 0.3400
Epoch 6/1000
5/5 [==============================] - 0s 6ms/step - loss: 2.2395 - acc: 0.3400
Epoch 7/1000
5/5 [==============================] - 0s 5ms/step - loss: 2.1212 - acc: 0.3400
Epoch 8/1000
5/5 [==============================] - 0s 5ms/step - loss: 2.0121 - acc: 0.3400
Epoch 9/1000
5/5 [==============================] - 0s 5ms/step - loss: 2.0164 - acc: 0.3400
Epoch 10/1000
5/5 [==============================] - 0s 7ms/step - loss: 1.9728 - acc: 0.3400
Epoch 11/1000
5/5 [==============================] - 0s 5ms/step - lo[...]<jupyter_text>evaluation<jupyter_code>model.evaluate(X_train, y_train)
X_train[0:1]
pred = model.predict(X_train[0:1])
np.argmax(pred)
pred1 = model.predict(X_train[1:2])
np.argmax(pred1)
pred2 = model.predict(X_train[2:3])
np.argmax(pred2)
from sklearn.metrics import classification_report, confusion_matrix
y_pred = model.predict(X_train)
y_pred.shape, y_pred[5]
y_pred_argmax = np.argmax(y_pred, axis=1)
y_pred_argmax.shape, y_pred_argmax[5]
y_train.shape, y_train[5]
print(classification_report(y_train, y_pred_argmax))
y_train
confusion_matrix(y_train, y_pred_argmax)
import seaborn as sns
sns.heatmap(confusion_matrix(y_train, y_pred_argmax), annot=True)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup