# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import multiprocessing

import numpy as np
import matplotlib.pyplot as plt

import cnn
import utils
import hmm
# -

cpus = multiprocessing.cpu_count() // 2
print(f'CPUs: {cpus}')
cpus = 2

# +
x = 3
y = np.array(['a', 'b', 'c'])
s = np.zeros(x)
s[0] = 1.0

real_hmm_rand = hmm.random_hmm(
    x=x,
    y=''.join(y),
    s=s
)

print(f'real_hmm_rand A:\n{real_hmm_rand.a}\n')
print(f'real_hmm_rand B:\n{real_hmm_rand.b}')

# +
epoch_size = 100
batch_size = 100
seq_len = 20

rand_data_gen = utils.HMMDataGenerator(
    real_hmm_rand,
    epoch_size,
    batch_size,
    seq_len
)
# -

t_hmm = real_hmm_rand
t_gen = rand_data_gen

# ## CNN 1

model = cnn.CNNModel(t_gen.input_shape())
model.summary()

utils.plot_model(model, 'images/cnn1_arch.png')

# +
epochs = 20

history = model.fit_generator(
    generator=t_gen,
    epochs=epochs,
    callbacks=utils.callbacks('cnn1'),
    use_multiprocessing=True,
    workers=cpus
)
# -

utils.plot_acc(history, 'images/cnn1_acc.png')

utils.plot_loss(history, 'images/cnn1_loss.png')

test_X = t_hmm.simulate(seq_len, reset_before=True)[1]
print(test_X)
test_X = np.array([t_gen._encode_hmm_outputs(test_X)])
print(test_X)

p = model.predict(test_X)
print(p)

pred_real = bool(round(p[0][0]))
print(f'Predict - Real?: {pred_real}')

model2 = cnn.CNNModel2(t_gen.input_shape())
model2.summary()

utils.plot_model(model2, to_file='images/cnn2_arch.png')

# +
epochs = 20

history2 = model2.fit_generator(
    generator=t_gen,
    epochs=epochs,
    callbacks=utils.callbacks('cnn2'),
#     validation_data=v_gen,
    use_multiprocessing=True,
    workers=cpus
)
# -

utils.plot_acc(history2, 'images/cnn2_acc.png')

utils.plot_loss(history2, 'images/cnn2_loss.png')

# # Model 3

model3 = cnn.CNNModel3(t_gen.input_shape())
model3.summary()

utils.plot_model(model3, 'images/cnn3_arch.png')

# +
epochs = 20

history3 = model3.fit_generator(
    generator=t_gen,
    epochs=epochs,
    callbacks=utils.callbacks('cnn3'),
#     validation_data=v_gen,
    use_multiprocessing=True,
    workers=cpus
)
# -

utils.plot_acc(history3, 'images/cnn3_acc.png')

utils.plot_loss(history3, 'images/cnn3_loss.png')



StarcoderdataJupyterScriptsDedupFiltered