def test_get_input_from_current_scope(self): """get_input retrieve existing input""" scope, name = self.get_scope(), 'foo' with nn.variable_scope(scope): op = nn.Input(shape=[], name=name) self.assertIs(op, nn.get_input(name)) self.assertIs(op, nn.get_input('{}/{}'.format(scope, name))) with self.assertRaises(ValueError): nn.get_input(name)
def test_conv2dtranspose(self): """Compnents consisting Conv2DTranspose layer are retrieved""" scope = self.get_scope() with nn.variable_scope(scope) as vs: input_ = nn.Input(shape=(32, 4, 8, 8), name='input') layer = nn.get_layer('Conv2D')(filter_height=4, filter_width=4, n_filters=4, strides=1, with_bias=True, name='Conv2D') output = layer(input_) layer = nn.get_layer('Conv2DTranspose')(filter_height=4, filter_width=4, n_filters=4, strides=1, with_bias=True, output_shape=input_.shape, name='Conv2DT') output = layer(output) filters = layer.get_parameter_variable('filter') bias = layer.get_parameter_variable('bias') with nn.variable_scope(vs, reuse=True): self.assertIs(filters, nn.get_variable('Conv2DT/filter')) self.assertIs(bias, nn.get_variable('Conv2DT/bias')) self.assertIs(output, nn.get_tensor('Conv2DT/output')) self.assertIs(input_, nn.get_input('input'))
def test_true_div(self): """Compnents consisting truediv layer are retrieved""" scope = self.get_scope() with nn.variable_scope(scope): input_ = nn.Input(shape=(32, 4, 8, 8), name='input') layer = nn.fetch_layer('TrueDiv')(denom=1.0, scope='TrueDiv') output = layer(input_) self.assertIs(output, nn.get_tensor('TrueDiv/output')) self.assertIs(input_, nn.get_input('input'))
def test_tile(self): """Compnents consisting Tile layer are retrieved""" scope = self.get_scope() with nn.variable_scope(scope): input_ = nn.Input(shape=(32, ), name='input') layer = nn.get_layer('Tile')(pattern=(1, 2), name='Tile') output = layer(input_) self.assertIs(output, nn.get_tensor('Tile/output')) self.assertIs(input_, nn.get_input('input'))
def test_mean(self): """Compnents consisting Mean layer are retrieved""" scope = self.get_scope() with nn.variable_scope(scope): input_ = nn.Input(shape=(32, 4, 8, 8), name='input') layer = nn.get_layer('Mean')(axis=[1, 2], name='Mean') output = layer(input_) self.assertIs(output, nn.get_tensor('Mean/output')) self.assertIs(input_, nn.get_input('input'))
def _test_layer_io(self, layer_name, input_shape): scope = '{}/{}'.format(self.get_scope(), layer_name) with nn.variable_scope(scope) as vs: input_ = nn.Input(shape=input_shape, name='input') layer = nn.fetch_layer(layer_name)(scope=layer_name) output = layer(input_) with nn.variable_scope(vs, reuse=True): output_tensor_name = '{}/output'.format(layer_name) self.assertIs(input_, nn.get_input('input')) self.assertIs(output, nn.get_tensor(output_tensor_name))
def test_map_nodes(self): """Can make/fetch dict of nodes""" names = ['input_state_1', 'input_state_2'] config0 = { 'typename': 'Input', 'args': { 'shape': (32, 5), 'name': names[0], }, } config1 = { 'typename': 'Input', 'args': { 'shape': (32, 5), 'name': names[1], }, } with nn.variable_scope(self.get_scope()): inputs = nn.make_io_node({'config0': config0, 'config1': config1}) input0 = nn.get_input(name=names[0]) input1 = nn.get_input(name=names[1]) self.assertIs(input0, inputs['config0']) self.assertIs(input1, inputs['config1'])
def test_dense(self): """Compnents consisting Dense layer are retrieved""" with nn.variable_scope(self.get_scope()) as vs: input_ = nn.Input(shape=(32, 5), name='input') layer = nn.fetch_layer('Dense')( n_nodes=4, with_bias=True, scope='Dense') output = layer(input_) weight = layer.get_parameter_variable('weight') bias = layer.get_parameter_variable('bias') with nn.variable_scope(vs, reuse=True): self.assertIs(weight, nn.get_variable('Dense/weight')) self.assertIs(bias, nn.get_variable('Dense/bias')) self.assertIs(output, nn.get_tensor('Dense/output')) self.assertIs(input_, nn.get_input('input'))
def test_conv2d(self): """Compnents consisting Conv2D layer are retrieved""" scope = self.get_scope() with nn.variable_scope(scope) as vs: input_ = nn.Input(shape=(32, 4, 8, 8), name='input') layer = nn.fetch_layer('Conv2D')( filter_height=4, filter_width=4, n_filters=4, strides=1, with_bias=True, name='Conv2D') output = layer(input_) filters = layer.get_parameter_variable('filter') bias = layer.get_parameter_variable('bias') with nn.variable_scope(vs, reuse=True): self.assertIs(filters, nn.get_variable('Conv2D/filter')) self.assertIs(bias, nn.get_variable('Conv2D/bias')) self.assertIs(output, nn.get_tensor('Conv2D/output')) self.assertIs(input_, nn.get_input('input'))
def test_single_node(self): """Can make/fetch single node""" name = 'input_state' input_config = { 'typename': 'Input', 'args': { 'shape': (32, 5), 'name': name, }, } input_config_reuse = { 'typename': 'Input', 'reuse': True, 'name': name, } with nn.variable_scope(self.get_scope()): input1 = nn.make_io_node(input_config) input2 = nn.make_io_node(input_config_reuse) input_ = nn.get_input(name=name) self.assertIs(input1, input2) self.assertIs(input1, input_)
def test_get_input(self): """Test if get_input correctly fetch Input object""" scope = self.get_scope() with nn.variable_scope(scope): input_0 = nn.Input(shape=[], name='input_a') input_1 = nn.get_input('input_a') self.assertIs(input_0, input_1) with self.assertRaises(ValueError): nn.get_input('input_b') input_2 = nn.get_input('{}/input_a'.format(scope)) self.assertIs(input_0, input_2) with self.assertRaises(ValueError): nn.get_input('{}/input_b'.format(scope))