Это не просто, так как каждый слой обучается в течение каждой эпохи.Вы можете использовать обратные вызовы, чтобы получить время обучения по всей сети, однако вам нужно сделать что-то вроде пэчворка, чтобы получить то, что вам нужно (приблизительное время обучения для каждого слоя).
Шаги -
- создание обратного вызова для записи времени выполнения каждой эпохи
- Установите каждый слой в сети на необучаемый и только один слой на обучаемый.
- Модель поезда на небольшом количествеэпохи и получают среднее время выполнения
- Выполните шаги с 2 по 3 для каждого независимого слоя в сети
- Верните результаты
Однако это НЕ фактическое время выполнения,Вы можете сделать относительный анализ того, какой слой занимает пропорционально больше времени, чем другой.
#Callback class for time history (picked up this solution directly from stackoverflow)
class TimeHistory(Callback):
def on_train_begin(self, logs={}):
self.times = []
def on_epoch_begin(self, batch, logs={}):
self.epoch_time_start = time.time()
def on_epoch_end(self, batch, logs={}):
self.times.append(time.time() - self.epoch_time_start)
time_callback = TimeHistory()
# Model definition
inp = Input((inp_dims,))
embed_out = Embedding(vocab_size, 256, input_length=inp_dims)(inp)
x = Conv1D(filters=32, kernel_size=3, activation='relu')(embed_out)
x = MaxPooling1D(pool_size=2)(x)
x = Flatten()(x)
x = Dense(64, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(32, activation='relu')(x)
x = Dropout(0.5)(x)
out = Dense(out_dims, activation='softmax')(x)
model = Model(inp, out)
model.summary()
# Function for approximate training time with each layer independently trained
def get_average_layer_train_time(epochs):
#Loop through each layer setting it Trainable and others as non trainable
results = []
for i in range(len(model.layers)):
layer_name = model.layers[i].name #storing name of layer for printing layer
#Setting all layers as non-Trainable
for layer in model.layers:
layer.trainable = False
#Setting ith layers as trainable
model.layers[i].trainable = True
#Compile
model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy', metrics=['acc'])
#Fit on a small number of epochs with callback that records time for each epoch
model.fit(X_train_pad, y_train_lbl,
epochs=epochs,
batch_size=128,
validation_split=0.2,
verbose=0,
callbacks = [time_callback])
results.append(np.average(time_callback.times))
#Print average of the time for each layer
print(f"{layer_name}: Approx (avg) train time for {epochs} epochs = ", np.average(time_callback.times))
return results
runtimes = get_average_layer_train_time(5)
plt.plot(runtimes)
#input_2: Approx (avg) train time for 5 epochs = 0.4942781925201416
#embedding_2: Approx (avg) train time for 5 epochs = 0.9014601230621337
#conv1d_2: Approx (avg) train time for 5 epochs = 0.822748851776123
#max_pooling1d_2: Approx (avg) train time for 5 epochs = 0.479401683807373
#flatten_2: Approx (avg) train time for 5 epochs = 0.47864508628845215
#dense_4: Approx (avg) train time for 5 epochs = 0.5149370670318604
#dropout_3: Approx (avg) train time for 5 epochs = 0.48329877853393555
#dense_5: Approx (avg) train time for 5 epochs = 0.4966880321502686
#dropout_4: Approx (avg) train time for 5 epochs = 0.48073616027832033
#dense_6: Approx (avg) train time for 5 epochs = 0.49605698585510255
