<jupyter_start><jupyter_text>Training CONVNET<jupyter_code>from packages import *
%matplotlib inline
import tensorflow as tf
from tensorflow.keras import backend as K
K.set_floatx('float32')
from tensorflow.keras.models import load_model
from sklearn.model_selection import train_test_split
fresh = True
subject = 1
session = 1
epochs = 100
batch_size = 2
model_version = 'n-drop'
data_version = ['c', 'd']
label_version = 'c'
# ADDED IN UTILS.PY NOW
#######################
# def r2_keras(y_true, y_pred):
#     SS_res =  K.sum(K.square(y_true - y_pred)) 
#     SS_tot = K.sum(K.square(y_true - K.mean(y_true))) 
#     return ( 1 - SS_res/(SS_tot + K.epsilon()) )
if fresh:
    test_model = load_model(os.path.join(UNTRAINED_MODELS_DIR, 'sub-{}_mod-{}'.format(subject, model_version)))
else:
    test_model = load_model(os.path.join(TRAINED_MODELS_DIR, 
                            'sub-{}_mod-{}_data-{}_lab-{}'.format(subject, model_version, data_version, label_version)))
    
test_model.summary()
X = {}
for v in data_version:
    X[v] = numpy_load('X_sub-{}_ses-{}_{}.npy'.format(subject, session, v), os.path.join(DATASET_X_DIR, v))
Y = numpy_load('Y_sub-{}_ses-{}_{}.npy'.format(subject, session, label_version), os.path.join(DATASET_Y_DIR, label_version))
X['c'] = np.expand_dims(X['c'], len(X['c'].shape))          
X_bold_train, X_bold_test, y_train, y_test = train_test_split(X['c'], Y, test_size=0.10, random_state=42)
X_voxel_train, X_voxel_test = train_test_split(X['d'], test_size=0.10, random_state=42)
if fresh:
    test_model.compile(optimizer = 'adam',
                  loss = 'mse',
                  metrics = ['mae', r2_keras])
Y.shape
test_model.fit([X_train], y_train, epochs=epochs, batch_size=batch_size, verbose=1)
train_loss = []
test_loss = []
for e in xrange(epochs):
    hist = test_model.fit([X_bold_train, X_voxel_train], y_train, epochs=1, batch_size=batch_size, verbose=1)
    train_loss.append(hist.history['loss'][0])
    Eval = test_model.evaluate([X_bold_test, X_voxel_test], y_test, batch_size=batch_size)
    print 'eval loss: {}'.format(Eval[0])
    test_loss.append(Eval[0])
epoch_series = np.arange(len(train_loss))
ax = plt.figure()
plt.plot(epoch_series, test_loss, epoch_series, train_loss)
metrics = test_model.evaluate(X_test, y_test, batch_size=batch_size)
metrics
stim_index = 2
img_shape = (50,50,3)
Y = y_test
x_d = np.expand_dims(X_voxel_test[stim_index], 0)
x_c = np.expand_dims(X_bold_test[stim_index], 0)
# x_c.shape

# x_d.shape
y = Y[stim_index]
p = test_model.predict([x_c, x_d])
print y.shape, p.shape
plt.imshow(y.reshape(img_shape))
plt.imshow(p.reshape(img_shape))<jupyter_output><empty_output><jupyter_text>Best so far model j<jupyter_code>plt.imshow(p.reshape(img_shape))
'-'.join(['c'])
test_model.save(os.path.join(TRAINED_MODELS_DIR, 
                             'sub-{}_ses-{}_mod-{}_data-{}_lab-{}'.format(subject, 
                            session, model_version, '-'.join(data_version), label_version)))
# X_batches = chunks(X, input_per_batch)
# Y_batches = chunks(Y, input_per_batch)
# for _ in xrange(epochs):
#     loss_list = []
#     accuracy_list = []
#     for i in range(len(X_batches)):
#         metrics = test_model.train_on_batch(X_batches[i], Y_batches[i])
#         loss_list.append(metrics[0])
#         accuracy_list.append(metrics[1])
#         print '*' ,
#     print 'epoch {}-loss:{}-acc:{}'.format(_, np.mean(loss_list), np.mean(accuracy_list))
def chunks(l, n):
    res = []
    for i in xrange(0, len(l), n):
        res.append(l[i:i+n])
    return res<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16317991631799164, 'fraction_numerical': 0.010599721059972107, 'mean_word_length': 4.425113464447806, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '165337', 'license': 'MIT', 'n_tokens_mistral': 1435, 'n_tokens_neox': 1394, 'n_words': 232, 'path': 'src/c4_training_convnet.ipynb', 'repo_name': 'kdonbekci/brain-decoder', 'size': '3585'}