Skip to content

Commit

Permalink
Preparation for multi-branches architectures
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwinpin committed Sep 19, 2018
1 parent 51c7740 commit 49e9ca8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 15 deletions.
36 changes: 30 additions & 6 deletions neuvol/architecture/individ_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,10 @@ def _random_init(self):
self._data_processing = self._random_init_data_processing()
self._training_parameters = self._random_init_training()

def _random_init_architecture(self):
def _random_init_branch(self, ):
"""
At first, we set probabilities pool and the we change
this uniform distribution according to previous layer
Here we create only branches without input and output layers
"""
if self._architecture:
self._architecture = []

architecture = []

# choose number of layers
Expand Down Expand Up @@ -110,6 +106,20 @@ def _random_init_architecture(self):
block = Block(layer, layers_in_block_number, previous_layer, next_layer, **self.options)
architecture.append(block)

return architecture

def _random_init_architecture(self):
"""
At first, we set probabilities pool and the we change
this uniform distribution according to previous layer
"""
if self._architecture:
self._architecture = []

architecture = []

architecture.extend(self._random_init_branch)

# Push input layer for functional keras api
block = Block('input', layers_number=1, **self.options)
architecture.insert(0, block)
Expand Down Expand Up @@ -435,6 +445,13 @@ def schema(self):

return schema

@property
def stage(self):
"""
Get the last stage of evaluation
"""
return self._stage

@property
def data_processing(self):
"""
Expand Down Expand Up @@ -470,6 +487,13 @@ def history(self, event):
"""
self._history.append(event)

@stage.setter
def stage(self, stage):
"""
Change stage
"""
self._stage = stage

def random_init(self):
"""
Public method for calling the random initialisation
Expand Down
6 changes: 1 addition & 5 deletions neuvol/architecture/individ_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np

from .individ_base import IndividBase
from ..layer.block import Block
from ..probabilty_pool import Distribution


class IndividImage(IndividBase):
Expand Down Expand Up @@ -62,4 +58,4 @@ def _random_init_data_processing(self):
# individ._result = serial['result']
# individ.shape_structure = serial['shape_structure']

# return individ
# return individ
7 changes: 6 additions & 1 deletion neuvol/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@
'vocabular': [30000],
'sentences_length': [i for i in range(1, 150, 1)],
'embedding_dim': [i for i in range(32, 300, 2)],
'trainable': [False, True]}}
'trainable': [False, True]},
'zeropadding1D': { # set manually
'padding': []},
'reshape': { # set manually
'target_shape': []
}}

LAYERS_POOL = {
'bi': {
Expand Down
29 changes: 26 additions & 3 deletions neuvol/evolution/evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class Evolution():
"""
Simple class that performs evolution process
"""

def __init__(
self,
stages,
Expand All @@ -37,6 +36,8 @@ def __init__(
freeze=None,
active_distribution=True,
loaded=False,
multiple_gpu=False,
vizualize=False,
**kwargs):
self._evaluator = evaluator
self._mutator = mutator
Expand All @@ -55,6 +56,8 @@ def __init__(
self._mortality_rate = 0.2
self._current_stage = 1

self._viz_data = []

if self._data_type == 'text':
Distribution.set_layer_status('cnn2', active=False)
Distribution.set_layer_status('max_pool2', active=False)
Expand Down Expand Up @@ -91,16 +94,18 @@ def mutation_step(self):
# TODO: more accurate error handling
try:
self._population[index] = self._mutator.mutate(self._population[index], self._current_stage)
except:
except Exception:
pass

def step(self):
"""
Perform one step of evolution, that consists of evaluation and death
"""
# TODO: parallel execution for multiple gpus
for network in self._population:
try:
network.result = self._evaluator.fit(network)
network.stage = self._current_stage
# NOTE: maybe ArithmeticError ?
except Exception:
# sorry, but here i dont care about type of exception
Expand All @@ -125,7 +130,7 @@ def crossing_step(self):
new_individ = self._crosser.cross(
deepcopy(self._population[index_father]),
deepcopy(self._population[index_mother]), self._current_stage)
except:
except Exception:
new_individ = cradle(0, self._data_type, self._task_type, freeze=self._freeze, **self._options)

self._population.append(new_individ)
Expand Down Expand Up @@ -173,6 +178,24 @@ def save(self):
def dump(self, path):
dump(self.save(), path)

def viz(self):
for network in self._population:
tmp = deepcopy(network)
tmp._name = tmp._name + str(self._current_stage)

# if individ was created rigth now - we dont remove parents to connect them
# if individ was created early - set parents as None to avoid crossconnections
if tmp.history[0].type == 'Birth':
if len(tmp.history) != 1:
tmp._parents = None
else:
tmp._parents[0]._name = tmp._parents[0]._name + str(self._current_stage - 1)
tmp._parents[1]._name = tmp._parents[1]._name + str(self._current_stage - 1)

self._viz_data.append(tmp.save())



@staticmethod
def load(serial, evaluator, mutator, crosser):
evolution = Evolution(serial['stages'], evaluator, mutator, crosser, loaded=True)
Expand Down

0 comments on commit 49e9ca8

Please sign in to comment.