예제 #1
0
def add_lstm_cell(network, name, size, xin, cin=None, hin=None):
    '''
    Add a LSTM cell named `name` to the `network`, with the dimension `size`.
    `xin`, `cin`, `hin` are the layers' names whose outputs are x_t, C_{t-1},
    h_{t-1}, respectively. Return the layers' names whose outputs are C_t, h_t.
    '''
    from nn_dataflow.core import Network
    from nn_dataflow.core import InputLayer, FCLayer, EltwiseLayer

    if not isinstance(network, Network):
        raise TypeError('add_lstm_cell: network must be a Network instance.')

    if cin is None:
        cin = '{}_cinit'.format(name)
        network.add_ext(cin, InputLayer(size, 1))
    if hin is None:
        hin = '{}_hinit'.format(name)
        network.add_ext(hin, InputLayer(size, 1))

    if (cin not in network) or (hin not in network) or (xin not in network):
        raise ValueError('add_lstm_cell: cin {}, hin {}, xin {} must all be '
                         'in the network.'.format(cin, hin, xin))

    def gate_name(gate):
        ''' Name of a gate. '''
        return '{}_{}gate'.format(name, gate)

    # Candidate.
    cand_name = '{}_cand'.format(name)
    prevs = (hin, xin) if hin else (xin, )
    network.add(cand_name, FCLayer(len(prevs) * size, size), prevs=prevs)

    # Three gates.
    prevs = (hin, xin) if hin else (xin, )
    for g in ['i', 'f', 'o']:
        network.add(gate_name(g),
                    FCLayer(len(prevs) * size, size),
                    prevs=prevs)

    # C_t.
    cout_name = '{}_cout'.format(name)
    cout_f_name = cout_name + '_f'
    prevs = (cin, gate_name('f')) if cin else (gate_name('f'), )
    network.add(cout_f_name, EltwiseLayer(size, 1, len(prevs)), prevs=prevs)
    cout_i_name = cout_name + '_i'
    prevs = (cand_name, gate_name('i'))
    network.add(cout_i_name, EltwiseLayer(size, 1, 2), prevs=prevs)
    prevs = (cout_i_name, cout_f_name)
    network.add(cout_name, EltwiseLayer(size, 1, 2), prevs=prevs)

    # h_t.
    hout_name = '{}_hout'.format(name)
    prevs = (cout_name, gate_name('o'))
    network.add(hout_name, EltwiseLayer(size, 1, 2), prevs=prevs)

    return cout_name, hout_name
예제 #2
0
    def test_add(self):
        ''' Modifier add. '''
        self.assertEqual(len(self.network), 3)

        self.network.add('f2', FCLayer(64, 2000, 7), prevs='p1')
        self.network.add('f3', FCLayer(3000, 1000), prevs=('f1', 'f2'))
        self.network.add('e4', EltwiseLayer(1000, 1, 2), prevs=('f1', 'f3'))
        self.network.add('f4', FCLayer(1000, 1000), prevs='e4')
        self.assertEqual(len(self.network), 7)
예제 #3
0
    def test_repr(self):
        ''' __repr__. '''
        # pylint: disable=eval-used
        for l in [
                LocalRegionLayer(64, 28, 2, 1),
                LocalRegionLayer(64, [28, 14], 1, [2, 4]),
                LocalRegionLayer(64, [28, 14], 1, [2, 4], 7),
                LocalRegionLayer(64, 28, 1, 4, 7)
        ]:
            self.assertIn('LocalRegionLayer', repr(l))
            self.assertEqual(eval(repr(l)), l)

        for l in [
                PoolingLayer(64, 28, 2),
                PoolingLayer(64, 28, 3, strd=2),
                PoolingLayer(64, [28, 14], [3, 4], strd=[2, 3])
        ]:
            self.assertIn('PoolingLayer', repr(l))
            self.assertEqual(eval(repr(l)), l)

        for l in [EltwiseLayer(64, 32, 3), EltwiseLayer(64, 28, 4)]:
            self.assertIn('EltwiseLayer', repr(l))
            self.assertEqual(eval(repr(l)), l)
예제 #4
0
    def test_nexts(self):
        ''' Get nexts. '''
        self.network.add('f2', FCLayer(64, 2000, 7), prevs='p1')
        self.network.add('f3', FCLayer(3000, 1000), prevs=('f1', 'f2'))
        self.network.add('e4', EltwiseLayer(1000, 1, 2), prevs=('f1', 'f3'))
        self.network.add('f4', FCLayer(1000, 1000), prevs='e4')

        nexts = self.network.nexts('p1')
        self.assertTupleEqual(nexts, ('f1', 'f2'))

        nexts = self.network.nexts('f1')
        self.assertTupleEqual(nexts, ('f3', 'e4'))

        nexts = self.network.nexts('f2')
        self.assertTupleEqual(nexts, ('f3',))

        nexts = self.network.nexts('f3')
        self.assertTupleEqual(nexts, ('e4',))
예제 #5
0
    def test_len(self):
        ''' Accessor len. '''
        self.assertEqual(len(self.network), 3)

        network = Network('test_net')
        self.assertEqual(len(network), 0)
        network.set_input_layer(InputLayer(3, 224))
        self.assertEqual(len(network), 0)
        network.add('c1', ConvLayer(3, 4, 224, 1))
        self.assertEqual(len(network), 1)

        self.network.add('f2', FCLayer(64, 2000, 7), prevs='p1')
        self.assertEqual(len(self.network), 4)
        self.network.add('f3', FCLayer(3000, 1000), prevs=('f1', 'f2'))
        self.assertEqual(len(self.network), 5)
        self.network.add('e4', EltwiseLayer(1000, 1, 2), prevs=('f1', 'f3'))
        self.assertEqual(len(self.network), 6)
        self.network.add('f4', FCLayer(1000, 1000), prevs='e4')
        self.assertEqual(len(self.network), 7)
예제 #6
0
NN.add('conv1', ConvLayer(3, 64, 112, 7, 2))
NN.add('pool1', PoolingLayer(64, 56, 3, 2))

RES_PREV = 'pool1'

for i in range(3):
    NN.add('conv2_{}_a'.format(i), ConvLayer(64 if i == 0 else 256, 64, 56, 1))
    NN.add('conv2_{}_b'.format(i), ConvLayer(64, 64, 56, 3))
    NN.add('conv2_{}_c'.format(i), ConvLayer(64, 256, 56, 1))

    # With residual shortcut.
    if i == 0:
        NN.add('conv2_br', ConvLayer(64, 256, 56, 1), prevs=(RES_PREV, ))
        RES_PREV = 'conv2_br'
    NN.add('conv2_{}_res'.format(i),
           EltwiseLayer(256, 56, 2),
           prevs=(RES_PREV, 'conv2_{}_c'.format(i)))
    RES_PREV = 'conv2_{}_res'.format(i)

for i in range(8):
    NN.add(
        'conv3_{}_a'.format(i),
        ConvLayer(256, 128, 28, 1, 2) if i == 0 else ConvLayer(
            512, 128, 28, 1))
    NN.add('conv3_{}_b'.format(i), ConvLayer(128, 128, 28, 3))
    NN.add('conv3_{}_c'.format(i), ConvLayer(128, 512, 28, 1))

    # With residual shortcut.
    if i == 0:
        NN.add('conv3_br', ConvLayer(256, 512, 28, 1, 2), prevs=(RES_PREV, ))
        RES_PREV = 'conv3_br'
예제 #7
0
from nn_dataflow.core import Network
from nn_dataflow.core import InputLayer, EltwiseLayer

from nn_dataflow.nns import add_lstm_cell
'''
LSTM from GNMT.

Sutskever, Vinyals, Le, Google, NIPS 2014
'''

NN = Network('GNMT')

NN.set_input_layer(InputLayer(1000, 1))

NL = 4

# Word embedding is a simple lookup.
# Exclude or ignore embedding processing.
WE = NN.INPUT_LAYER_KEY

# layered LSTM.
X = WE
for l in range(NL):
    cell = 'cell_l{}'.format(l)
    C, H = add_lstm_cell(NN, cell, 1000, X)
    X = H

# log(p), softmax.
NN.add('Wd', EltwiseLayer(1000, 1, 1), prevs=(X, ))
예제 #8
0
NN.add('conv1', ConvLayer(3, 64, 112, 7, 2))
NN.add('pool1', PoolingLayer(64, 56, 3, 2))

RES_PREV = 'pool1'

for i in range(3):
    NN.add('conv2_{}_a'.format(i), ConvLayer(64 if i == 0 else 256, 64, 56, 1))
    NN.add('conv2_{}_b'.format(i), ConvLayer(64, 64, 56, 3))
    NN.add('conv2_{}_c'.format(i), ConvLayer(64, 256, 56, 1))

    # With residual shortcut.
    if i == 0:
        NN.add('conv2_br', ConvLayer(64, 256, 56, 1), prevs=(RES_PREV,))
        RES_PREV = 'conv2_br'
    NN.add('conv2_{}_res'.format(i), EltwiseLayer(256, 56, 2),
           prevs=(RES_PREV, 'conv2_{}_c'.format(i)))
    RES_PREV = 'conv2_{}_res'.format(i)

for i in range(4):
    NN.add('conv3_{}_a'.format(i),
           ConvLayer(256, 128, 28, 1, 2) if i == 0
           else ConvLayer(512, 128, 28, 1))
    NN.add('conv3_{}_b'.format(i), ConvLayer(128, 128, 28, 3))
    NN.add('conv3_{}_c'.format(i), ConvLayer(128, 512, 28, 1))

    # With residual shortcut.
    if i == 0:
        NN.add('conv3_br', ConvLayer(256, 512, 28, 1, 2), prevs=(RES_PREV,))
        RES_PREV = 'conv3_br'
    NN.add('conv3_{}_res'.format(i), EltwiseLayer(512, 28, 2),
예제 #9
0
 def test_eltwiselayer(self):
     ''' EltwiseLayer init. '''
     elayer = EltwiseLayer(64, 28, 3)
     self.assertEqual(elayer.ops_per_neuron(), 3)
     self.assertEqual(elayer.nifm, 3 * elayer.nofm)
     self.assertEqual(elayer.ifmap_size(), elayer.ofmap_size())
예제 #10
0
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the BSD-3 License for more details.

You should have received a copy of the Modified BSD-3 License along with this
program. If not, see <https://opensource.org/licenses/BSD-3-Clause>.
"""

from nn_dataflow.core import Network
from nn_dataflow.core import InputLayer, EltwiseLayer

from nn_dataflow.nns import add_lstm_cell
'''
LSTM from Show and Tell.

Vinyals et al., Google, CVPR 2015
'''

NN = Network('ShowTell')

NN.set_input_layer(InputLayer(512, 1))

# Word embedding is a simple lookup.
# Exclude or ignore embedding processing.
WE = NN.INPUT_LAYER_KEY

# LSTM.
C, H = add_lstm_cell(NN, 'cell', 512, WE)

# log(p), softmax.
NN.add('Wd', EltwiseLayer(512, 1, 1), prevs=(H, ))