Collection of Questions about Keras part 1

Ke Gui
2 min readJun 6, 2020

model.fit()

The model.fit() can be used to update models, right ? That’s what I’ve understood it to do. It doesn’t train from scratch each time it is called.

Yes. You’re starting from the previous model state (ie. the weights are not re-initialized).

The batch_size seems to be a really crucial parameter. Is there some rule of thumb for selecting the batch size ?

In general smaller batches will give better results, however larger batches will make training faster. There is a compromise to strike, somewhere in between. I generally use 16 or 32, unless there is very little data in which case I go full stochastic (batch_size = 1).

Code for multi_generators

Here’s a piece of code that formats the outputs of two generators. It can be extended to any number of generators. Assuming the output of both generators is of the form (x,y) and the wanted output is of the form ([x1, x2], y1):

def format_gen_outputs(gen1,gen2):
x1 = gen1[0]
x2 = gen2[0]
y1 = gen1[1]
return [x1, x2], y1
combo_gen = map(format_gen_outputs, gen1, gen2)same as [(a, b) for a in iterable_a for b in iterable_b]

flow_from_directory() class labels issue

from glob import glob
class_names = glob("*") # Reads all the folders in which images are present
class_names = sorted(class_names) # Sorting them
name_id_map = dict(zip(class_names, range(len(class_names))))

The variable name_id_map in the above code contains the same dictionary as the one obtained from class_indices function of flow_from_directory().

model.fit_generator() parameters: queue size, workers and use_multiprocessing

queue size:

Construction like this suggests, that authors thought about expensive data generators, which might take time to execture. For example consider downloading data over a network in generator call — then it makes sense to precache some next batches, and download next ones in parallel for the sake of efficiency and to be robust to network errors etc.

so ideally you never halt training on waiting for the generator to generate results — you have a thread fill the queue up silently in the back while the model is training on the prior fetched samples. 2 times the number of workers is a great start.

--

--

Ke Gui

An ordinary guy who wants to be the reason someone believes in the goodness of people. He is living at Brisbane, Australia, with a lovely backyard.