<jupyter_start><jupyter_text>Selection of the optimal size of the dense layer of the neural network To study the effect of the dense layer size in VGG7 for MNIST classification task. * [The parameters studied](The-parameters-studied)* [Parameters Value Area](Parameters-Value-Area)* [Metrics](Metrics)* [Dataset loading](Dataset-loading)* [Creating a model template](Creating-a-model-template)* [Creating a research object](Creating-a-research-object)* [Results](Results)* [Conclusion](Conclusion)  The parameters studiedSize of the last dense layer in VGG7.  Parameters Value AreaThe output dimension of the last dense layer is determined by number of classes.Therefore we can vary only the first dimension in the dense layer: 4096, 1024, 256, 10 MetricsAccuracy on the test set of the MNIST data.<jupyter_code>import os
import sys
sys.path.insert(0, os.path.join("..", "..", ".."))

from batchflow import B, V, C
from batchflow.opensets import MNIST
from batchflow.models.tf import VGG7
from batchflow.research import Option, Research, Results

from src import show_research

%env CUDA_VISIBLE_DEVICES='6,7'<jupyter_output>env: CUDA_VISIBLE_DEVICES='6,7'<jupyter_text>Dataset loading<jupyter_code>mnist_ds = MNIST()<jupyter_output><empty_output><jupyter_text>Creating a model templateWe use VGG7 model with to dense layers in the end of the network.Define model config:<jupyter_code>model_config = {
    'inputs/images/shape': B('image_shape'),
    'inputs/labels/classes': 10,
    'initial_block/inputs': 'images',
    'head': dict(layout='dfa f',
                 units=[C('n_filters'), 10]),
    'loss': 'ce',
    'optimizer': 'Adam',
}<jupyter_output><empty_output><jupyter_text>Define train and test pipeline and set parameters of lazy run with BATCH_SIZE = 60:<jupyter_code>BATCH_SIZE = 60
train_pipeline = (
    mnist_ds.train.p
    .init_variable('loss', init_on_each_run=list)
    .init_model('dynamic', VGG7, 'mnist_model', config=model_config)
    .to_array()
    .train_model('mnist_model', fetches='loss', 
                 images=B('images'), labels=B('labels'),
                 save_to=V('loss', mode='w'))
    .run_later(BATCH_SIZE, shuffle=True, n_epochs=None)
)

test_pipeline = (
    mnist_ds.test.p
    .import_model('mnist_model', C('import_from'))
    .init_variable('predictions', init_on_each_run=list)
    .init_variable('metrics', init_on_each_run=None)
    .to_array()
    .predict_model('mnist_model', fetches='predictions', 
                   images=B('images'), save_to=V('predictions'))
    .gather_metrics('class', targets=B('labels'), predictions=V('predictions'),
                    fmt='logits', axis=-1, save_to=V('metrics', mode='u'))
    .run_later(BATCH_SIZE, shuffle=True, n_epochs=1)
)<jupyter_output><empty_output><jupyter_text>Creating a research objectSet the parameters for research execution:For each parameter set perform NUM_ITERS=1000 iterations of train pipeline with model performance evaluation every 50 iterations. Repeat this procedure NUM_REPEAT=3 times.We add to the research a function get_accuracy that gets results of the test pipeline and will be executed every 50 iterations.Results of the research execution will be saved in the directory __n_filters_research__<jupyter_code>hyper = Option('n_filters', [4096, 1024, 256, 10])

research = (Research()
            .add_pipeline(train_pipeline, variables='loss', name='train')
            .add_pipeline(test_pipeline, name='test', execute=50, run=True, import_from='train')
            .get_metrics(pipeline='test', metrics_var='metrics', metrics_name='accuracy',
                         returns='accuracy', execute=50)
            .add_grid(hyper)
)<jupyter_output><empty_output><jupyter_text>Run research:<jupyter_code>N_REPS = 3
N_ITERS = 1000
research_name = 'n_filters_research'
research.run(n_reps=N_REPS, n_iters=N_ITERS, name=research_name, bar=True,  workers=2, gpu=[6,7])<jupyter_output>Research n_filters_research is starting...<jupyter_text>Results Train loss and test accuracy functions vs a number of iterations for each parameters set:<jupyter_code>data = Results(path=research_name).load(use_alias=True)
show_research(data, average_repetitions=True, log_scale=[True, False],
              layout=['train/loss', 'test_metrics/accuracy'], rolling_window=[30, None])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11725254394079555, 'fraction_numerical': 0.015957446808510637, 'mean_word_length': 5.223021582733813, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '526269', 'license': 'Apache-2.0', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1333, 'n_words': 343, 'path': 'model_description/additional_examples/parameters_estimation/num_filters.ipynb', 'repo_name': 'gazprom-neft/standards', 'size': '4324'}