<jupyter_start><jupyter_text>Classic CNN Model<jupyter_code>from tensorflow.keras import datasets, layers, models, optimizers, callbacks
# CNN model architecture
model = models.Sequential()
model.add(layers.Conv2D(64, (3,3), activation='relu', input_shape=(sl, 5,1),padding="SAME"))
model.add(layers.BatchNormalization())
model.add(layers.MaxPooling2D((3,1)))
model.add(layers.Conv2D(32, (3,3), activation='relu',padding="SAME"))
model.add(layers.BatchNormalization())
model.add(layers.MaxPooling2D((3,1)))
model.add(layers.Conv2D(16, (3,3), activation='relu',padding="SAME"))
model.add(layers.BatchNormalization())
model.add(layers.MaxPooling2D((3,1)))
# model.add(layers.Conv2D(16, (3,3), activation='relu',padding="SAME"))
# model.add(layers.BatchNormalization())
# model.add(layers.MaxPooling2D((3,1)))
model.add(layers.Flatten())
model.add(layers.BatchNormalization())
model.add(layers.Dense(500, activation='tanh'))
model.add(layers.Dense(y_train.shape[1]))
model.summary()
# Step-decay learning rate scheduler
def step_decay(epoch):
   initial_lrate = 0.001
   drop = 0.5
   epochs_drop = 2.0
   lrate = initial_lrate * np.power(drop, np.floor((1+epoch)/epochs_drop))
   return lrate

class LossHistory(callbacks.Callback):
    def on_train_begin(self, logs={}):
       self.losses = []
       self.lr = []
 
    def on_epoch_end(self, batch, logs={}):
       self.losses.append(logs.get('loss'))
       self.lr.append(step_decay(len(self.losses)))
        
loss_history = LossHistory()
lrate = callbacks.LearningRateScheduler(step_decay)
callbacks_list = [loss_history, lrate]
from tensorflow.keras.metrics import top_k_categorical_accuracy
#opt = tf.keras.optimizers.SGD(momentum=0.9, nesterov=True)
opt = tf.keras.optimizers.Adam()
model.compile(optimizer=opt, loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
              metrics=['accuracy','top_k_categorical_accuracy'])

# Validation time
history = model.fit(X_train, y_train, epochs=5, batch_size = 32, validation_data=(X_test, y_test), callbacks=callbacks_list, verbose=1)

# Final Test time
#history = model.fit(trainX, trainY, epochs=5, callbacks=callbacks_list)
print('Learning rates through epochs:', loss_history.lr)
from tensorflow.keras.models import Model

# Getting the DNA embeddings of all data from the last dense layer
layer_name = 'dense'
model2= Model(inputs=model.input, outputs=model.get_layer(layer_name).output)
dna_embeddings=model2.predict(allX)
print(dna_embeddings.shape)
# saving the results into csv file to be later read into matlab
np.savetxt("DNA_CNN_embeddings_500_5e_adam.csv", dna_embeddings, delimiter=",")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14679245283018869, 'fraction_numerical': 0.022641509433962263, 'mean_word_length': 6.751461988304094, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '22391', 'license': 'Apache-2.0', 'n_tokens_mistral': 968, 'n_tokens_neox': 935, 'n_words': 156, 'path': 'DNA Embeddings/DNA_embedddings_INSECT.ipynb', 'repo_name': 'sbadirli/Open-set-Insect-Discovery', 'size': '2650'}