Completed
Push — master ( 394368...090fba )
by Raphael
01:33
created

Runtime.__init__()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
import theano
5
import theano.tensor as T
6
from theano.ifelse import ifelse
7
from tensor_conversion import neural_computation
8
9
class Runtime(object):
10
    """
11
    Manage runtime variables in deepy.
12
    """
13
14
    def __init__(self):
15
        self._training_flag = theano.shared(0, name="training_flag")
16
        self._is_training = False
17
18
19
    @neural_computation
20
    def iftrain(self, then_branch, else_branch):
21
        """
22
        Execute `then_branch` when training.
23
        """
24
        return ifelse(self._training_flag, then_branch, else_branch, name="iftrain")
25
26
    def switch_training(self, flag):
27
        """
28
        Switch training mode.
29
        :param flag: switch on training mode when flag is True.
30
        """
31
        if self._is_training == flag: return
32
        self._is_training = flag
33
        if flag:
34
            self._training_flag.set_value(1)
35
        else:
36
            self._training_flag.set_value(0)
37
38
39
if "runtime" not in globals():
40
    runtime = Runtime()