<jupyter_start><jupyter_text>Import statements<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import sklearn
import random
import os,sys

# import keras
import keras
from keras import backend as K
backend_keras = keras.backend.backend()
print("keras is using", backend_keras, "as the backend")
from keras.layers import Input, Flatten, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model, load_model, Sequential

# opt
from keras.optimizers import SGD, Nadam, Adamax, Adam, Adadelta, Adagrad, RMSprop<jupyter_output>keras is using tensorflow as the backend<jupyter_text>Load esc frac data and labelsThese are the ones which have been created with the keras notebook<jupyter_code>f = os.listdir()
f.sort()
for i in f: print(i)
# load data
x_train_raw = np.load("data_train.npy",allow_pickle=True)
x_val_raw = np.load("data_val.npy")
x_test_raw = np.load("data_test.npy")

# load labels
labels_train = np.load("labels_train.npy",allow_pickle=True)
labels_val = np.load("labels_val.npy",allow_pickle=True)
labels_test = np.load("labels_test.npy",allow_pickle=True)

# we only care about the redshift labels here
lab_esc_train = np.array([i[0] for i in labels_train])
lab_esc_val = np.array([i[0] for i in labels_val])
lab_esc_test = np.array([i[0] for i in labels_test])

print("shapes :", x_train_raw.shape, x_val_raw.shape, x_test_raw.shape)
print("more shape :", lab_esc_train.shape, lab_esc_val.shape, lab_esc_test.shape)<jupyter_output>shapes : (4896, 200, 200, 1) (652, 200, 200, 1) (980, 200, 200, 1)
more shape : (4896, 12) (652, 12) (980, 12)<jupyter_text>Apply Autoencoder first load the autoencoder<jupyter_code>os.listdir("../../Auto-Encoder Training/")
autoencoder = load_model("../../Auto-Encoder Training/autoencoder_adam_95.model")
#autoencoder.summary()
encoder = Model(autoencoder.input, autoencoder.layers[-8].output)
encoder.summary()
x_train = encoder.predict(x_train_raw)
x_val = encoder.predict(x_val_raw)
x_test = encoder.predict(x_test_raw)<jupyter_output><empty_output><jupyter_text>Define the CNN architecture<jupyter_code>classifier = Sequential()

# extract features
classifier.add(Conv2D(8, (3,3), input_shape=(25,25,8),
                     activation='relu', padding='valid'))
# reduce dimensionality, keep most important info
classifier.add(MaxPooling2D(pool_size=(2,2)))

# add a second layer, again with no padding
classifier.add(Conv2D(4, (3,3), activation='relu', padding='valid'))

# pool again
#classifier.add(MaxPooling2D(pool_size=(2,2)))

# here i flatten and then guess the class
classifier.add(Flatten())

# fully connected layers ensures connections to all activations 
# in previous layers

classifier.add(Dense(units=12, activation='softmax'))<jupyter_output><empty_output><jupyter_text>Alternative Architecture<jupyter_code>classifier = Sequential()

# extract features
classifier.add(Conv2D(32, (3,3), input_shape=(25,25,8),
                     activation='relu', padding='same'))
# reduce dimensionality, keep most important info
classifier.add(MaxPooling2D(pool_size=(2,2)))

# extract features
#classifier.add(Conv2D(16, (3,3), input_shape=(25,25,8),
                     activation="relu", padding='same'))

# downsample
classifier.add(MaxPooling2D(pool_size=(2,2)))

classifier.add(Flatten())

classifier.add(Dense(units=12, activation='softmax'))<jupyter_output><empty_output><jupyter_text>Hyper params + compile network<jupyter_code># SGD, Nadam, Adamax, Adam, Adadelta, Adagrad, RMSprop

opt = RMSprop(learning_rate=0.001, rho=0.9)

classifier.compile(optimizer=opt,
                   loss='categorical_crossentropy',
                   metrics=["accuracy"])
print("printing summary of model")
classifier.summary()<jupyter_output>printing summary of model
Model: "sequential_8"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_14 (Conv2D)           (None, 23, 23, 8)         584       
_________________________________________________________________
max_pooling2d_13 (MaxPooling (None, 11, 11, 8)         0         
_________________________________________________________________
conv2d_15 (Conv2D)           (None, 9, 9, 4)           292       
_________________________________________________________________
flatten_7 (Flatten)          (None, 324)               0         
_________________________________________________________________
dense_7 (Dense)              (None, 12)                3900      
=================================================================
Total params: 4,776
Trainable params: 4,776
Non-trainable params: 0
__________________________[...]<jupyter_text>Train the network<jupyter_code>print(x_train.shape, lab_esc_train.shape)<jupyter_output>(4896, 25, 25, 8) (4896, 12)<jupyter_text>Display netowrk stats and save plots<jupyter_code>EPOCHS = 450

H = classifier.fit(x_train, lab_esc_train,
                  epochs = EPOCHS,
                  batch_size = 128,
                  shuffle = True,
                  validation_data=(x_val, lab_esc_val))<jupyter_output>Train on 4896 samples, validate on 652 samples
Epoch 1/450
4896/4896 [==============================] - 1s 253us/step - loss: 1.9319 - accuracy: 0.2878 - val_loss: 2.3601 - val_accuracy: 0.1840
Epoch 2/450
4896/4896 [==============================] - 1s 254us/step - loss: 1.9310 - accuracy: 0.2947 - val_loss: 2.2678 - val_accuracy: 0.1641
Epoch 3/450
4896/4896 [==============================] - 1s 281us/step - loss: 1.9313 - accuracy: 0.2884 - val_loss: 2.3795 - val_accuracy: 0.1488
Epoch 4/450
4896/4896 [==============================] - 1s 196us/step - loss: 1.9310 - accuracy: 0.2878 - val_loss: 2.1985 - val_accuracy: 0.1764
Epoch 5/450
4896/4896 [==============================] - 1s 242us/step - loss: 1.9211 - accuracy: 0.3017 - val_loss: 2.4369 - val_accuracy: 0.1840
Epoch 6/450
4896/4896 [==============================] - 1s 231us/step - loss: 1.9419 - accuracy: 0.2857 - val_loss: 2.1936 - val_accuracy: 0.1825
Epoch 7/450
4896/4896 [==============================] - 2s 343us/step [...]<jupyter_text>Evaluate the netowrk - display network stats and save plots<jupyter_code># plot the training loss and accuracy for each epoch
N = np.arange(0, EPOCHS)
plt.figure()
plt.style.use("ggplot")
plt.plot(N, H.history["loss"], label="train_loss")
plt.plot(N, H.history["val_loss"], label="val_loss")
plt.plot(N, H.history["accuracy"], label="train_accuracy")
plt.plot(N, H.history["val_accuracy"], label="val_accuracy")
plt.title("Predicting Redshift model 2")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend()
plt.savefig("training_perf_250_arch_03.4.75_pred_redshift_with_encoder.png")
plt.show()<jupyter_output><empty_output><jupyter_text>Save the weights<jupyter_code>classifier.save("keras_arch_05.4.75_with_encoder_esc_predict.model")<jupyter_output><empty_output><jupyter_text>Make prediction<jupyter_code>pred  = classifier.predict(x_test)
print(pred.shape, lab_esc_test.shape)
l = len(pred)
ri,wr=0,0
for i in range(len(pred)):
    a = list(pred[i]).index(max(pred[i]))
    b = list(lab_esc_test[i]).index(1)
    if a==b: ri+=1
    else: wr +=1
print(100.0*ri / (ri+wr))
print("percent correctly classified\n",
     100.0*float(right)/count)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16603466955579632, 'fraction_numerical': 0.06121343445287107, 'mean_word_length': 5.13881961762261, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '156696', 'license': 'MIT', 'n_tokens_mistral': 2802, 'n_tokens_neox': 2458, 'n_words': 527, 'path': 'CNN/All/CNN5_classify_esc_frac_with_autoencoder.ipynb', 'repo_name': 'dcxSt/MAIS202---21cm-ionization-maps-of-the-universe', 'size': '7384'}