Error after first epoch

I am trying to train a Conv1D network, and this is my code:

# %% Imports

import time as time
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras.layers import Conv1D
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import InputLayer
from keras.layers import MaxPooling1D
from tensorflow.keras import Model
from keras.models import Sequential
from keras import backend as K
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelBinarizer
from sklearn.ensemble import RandomForestClassifier

# %% Defining classification metrics (precision, recall and f1_score)

def recall_m(y_true_m, y_pred_m):
    true_positives = K.sum(K.round(K.clip(y_true_m * y_pred_m, 0, 1)))
    possible_positives = K.sum(K.round(K.clip(y_true_m, 0, 1)))
    recall_metrica = true_positives / (possible_positives + K.epsilon())
    return recall_metrica

def precision_m(y_true_m, y_pred_m):
    true_positives = K.sum(K.round(K.clip(y_true_m * y_pred_m, 0, 1)))
    predicted_positives = K.sum(K.round(K.clip(y_pred_m, 0, 1)))
    precision_metrica = true_positives / (predicted_positives + K.epsilon())
    return precision_metrica

def f1_m(y_true_m, y_pred_m):
    precision_met = precision_m(y_true_m, y_pred_m)
    recall_met = recall_m(y_true_m, y_pred_m)
    return 2*((precision_met*recall_met)/(precision_met+recall_met+K.epsilon()))

# %% Loading dataset

# percentuais_amostras = [0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08,
#                         0.09, 0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 1]

percentuais_amostras = [1]

rede = 'Conv1D'
rede_rf = 'Conv1D_RF'
dataset = 'M7'
modelo = 'M9'
for perc_amostras in percentuais_amostras:
    df = pd.read_csv(f'C:/Users/ldsp_/OneDrive/Área de Trabalho/Irish/Dataset_{dataset}/Dataset_{dataset}_{perc_amostras}.txt', sep=' ')
    saida = df['FLAG']

    # %% Split into inputs and outputs

    x = df.iloc[:, :-1].values
    y = df['FLAG'].values
    del df

    # %% Encoding outputs

    label_bin = LabelBinarizer()
    classes_unicas = np.unique(y)
    label_bin.fit_transform(classes_unicas)

    # %% Train/test split

    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
    del x, y

    # One-hot-encoding output
    y_train_one_hot = label_bin.transform(y_train)
    # Reshaping input to Conv1D network format
    x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)

    # %% Conv1D

    feature_extractor = Sequential(name=f'{rede}_{modelo}_{perc_amostras}')
    feature_extractor.add(InputLayer((x_train.shape[1], 1), name='Camada_Entrada'))
    feature_extractor.add(Conv1D(filters=64, kernel_size=2, activation='relu', name='1_Camada_Conv'))
    feature_extractor.add(MaxPooling1D(pool_size=2))
    feature_extractor.add(Conv1D(filters=128, kernel_size=2, activation='relu', name='2_Camada_Conv'))
    feature_extractor.add(MaxPooling1D(pool_size=3))
    feature_extractor.add(Conv1D(filters=64, kernel_size=2, activation='tanh', name='3_Camada_Conv'))
    feature_extractor.add(MaxPooling1D(pool_size=3))
    feature_extractor.add(Flatten())
    fe_output = feature_extractor.output  # Goes to  RF_Classifier
    dense_1 = Dense(units=4 * y_train_one_hot.shape[1], activation='relu', name='1_Dense')(fe_output)
    output_layer = Dense(units=y_train_one_hot.shape[1], activation='softmax', name='Camada_Saida')(dense_1)
    # Compile
    cnn_model = Model(inputs=feature_extractor.input, outputs=output_layer)
    cnn_model.compile(optimizer='adam', loss='categorical_crossentropy',
                      metrics=['accuracy',precision_m, recall_m, f1_m])
    cnn_model.summary()

# %% Checkpoint and Earlystop

    cp = ModelCheckpoint(
        filepath=f'C:/Users/ldsp_/OneDrive/Área de Trabalho/Irish/{rede}/Resultados {rede}_{modelo}/{rede}_{modelo}_{perc_amostras}.hdf5',
        monitor='val_loss',
        verbose=1,
        save_best_only=True,
        mode='min')
    earlystop = EarlyStopping(monitor='val_loss',
                              patience=50,
                              verbose=1,
                              mode='min',
                              restore_best_weights=True)

    # %% Treinando a CNN

    val_size = 0.2
    t = time.time()
    history = cnn_model.fit(x_train,
                            y_train_one_hot,
                            batch_size=256,
                            epochs=250,
                            callbacks=[cp, earlystop],
                            validation_split=val_size)
    tempo_modelo = round((time.time() - t) / 60, 4)

What i am trying to do here, is to train this Conv1D network, and use feature_extractor as input to a RandomForestClassifier from Sklearn.

But after the first epoch, i am getting this error:

Epoch 1/250
10372/10372 [==============================] - 40s 3ms/step - loss: 0.5776 - accuracy: 0.7931 - precision_m: 0.8696 - recall_m: 0.7234 - f1_m: 0.7868 - val_loss: 0.4846 - val_accuracy: 0.8222 - val_precision_m: 0.8831 - val_recall_m: 0.7699 - val_f1_m: 0.8225

Epoch 00001: val_loss improved from inf to 0.48459, saving model to C:/Users/ldsp_/OneDrive/Área de Trabalho/Irish/Conv1D/Resultados Conv1D_M9\Conv1D_M9_1.hdf5
Traceback (most recent call last):

  File ~\miniconda3\envs\tf-gpu\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\ldsp_\onedrive\área de trabalho\irish\conv1d\conv1d_m9.py:189
    history = cnn_model.fit(x_train,

  File ~\miniconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\keras\engine\training.py:1229 in fit
    callbacks.on_epoch_end(epoch, epoch_logs)

  File ~\miniconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\keras\callbacks.py:435 in on_epoch_end
    callback.on_epoch_end(epoch, logs)

  File ~\miniconda3\envs\tf-gpu\lib\site-packages\keras\callbacks.py:1376 in on_epoch_end
    self._save_model(epoch=epoch, logs=logs)

  File ~\miniconda3\envs\tf-gpu\lib\site-packages\keras\callbacks.py:1428 in _save_model
    self.model.save(filepath, overwrite=True, options=self._options)

  File ~\miniconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\keras\engine\training.py:2111 in save
    save.save_model(self, filepath, overwrite, include_optimizer, save_format,

  File ~\miniconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\keras\saving\save.py:146 in save_model
    hdf5_format.save_model_to_hdf5(

  File ~\miniconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py:110 in save_model_to_hdf5
    model_metadata = saving_utils.model_metadata(model, include_optimizer)

  File ~\miniconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\keras\saving\saving_utils.py:150 in model_metadata
    model_config['config'] = model.get_config()

  File ~\miniconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\keras\engine\functional.py:649 in get_config
    return copy.deepcopy(get_network_config(self))

  File ~\miniconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\keras\engine\functional.py:1368 in get_network_config
    new_node_index = node_conversion_map[node_key]

KeyError: 'Camada_Entrada_ib-0'

One thing i have noticed is that i get this from model.summary():

Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
Total params: 36,860
Trainable params: 36,860
Non-trainable params: 0
_________________________________________________________________

The parameteres are there, but no output shape?

I have tried this same code in another PC, and it works. What could be the reason i am getting this error in this case?

Hi @MURILO_ARAUJO_SOUZA
What is Camada_Entrada_ib-0 mentioned in the rerror message KeyError: 'Camada_Entrada_ib-0', please?

Hello @tagoma ,

The string 'Camada_Entrada' comes from this line:

feature_extractor.add(InputLayer((x_train.shape[1], 1), name='Camada_Entrada'))

My x_train has shape (3318748, 28, 1). I don’t understand what is that _ib-0 and where it comes from.

There may be difference of python/Tensorflow/Keras version between your 2 computers.
Can you please replace this import from tensorflow.keras import Model with an import directly from keras (as other imports like from keras.layers import Conv1D), the idea being to avoid mixing import from Tensorflow and those from Keras. Note thought my " suggestion here is highly speculative!

Thank you!

That was the issue, i replaced from tensorflow.keras import Model for from keras import Model.

1 Like