def test_uniquify_str(self): """Tests :func:`texar.tf.utils.uniquify_str`. """ str_set = ['str'] unique_str = utils.uniquify_str('str', str_set) self.assertEqual(unique_str, 'str_1') str_set.append('str_1') str_set.append('str_2') unique_str = utils.uniquify_str('str', str_set) self.assertEqual(unique_str, 'str_3')
def _build_layers(network, layers=None, layer_hparams=None): """Builds layers. Either :attr:`layer_hparams` or :attr:`layers` must be provided. If both are given, :attr:`layers` will be used. Args: network: An instance of a subclass of :class:`~texar.tf.modules.networks.network_base.FeedForwardNetworkBase` layers (optional): A list of layer instances. layer_hparams (optional): A list of layer hparams, each to which is fed to :func:`~texar.tf.core.layers.get_layer` to create the layer instance. """ with tf.variable_scope(network.variable_scope): if layers is not None: network._layers = layers else: if layer_hparams is None: raise ValueError( 'Either `layer` or `layer_hparams` is required.') network._layers = [] for _, hparams in enumerate(layer_hparams): network._layers.append(get_layer(hparams=hparams)) for layer in network._layers: layer_name = uniquify_str(layer.name, network._layer_names) network._layer_names.append(layer_name) network._layers_by_name[layer_name] = layer
def append_layer(self, layer): """Appends a layer to the end of the network. The method is only feasible before :attr:`_build` is called. Args: layer: A :tf_main:`tf.layers.Layer <layers/Layer>` instance, or a dict of layer hyperparameters. """ if self._built: raise TexarError("`FeedForwardNetwork.append_layer` can be " "called only before `_build` is called.") with tf.variable_scope(self.variable_scope): layer_ = layer if not isinstance(layer_, tf.layers.Layer): layer_ = get_layer(hparams=layer_) self._layers.append(layer_) layer_name = uniquify_str(layer_.name, self._layer_names) self._layer_names.append(layer_name) self._layers_by_name[layer_name] = layer_
def _build_conv1d_hparams(self, pool_hparams): """Creates the hparams for each of the conv layers usable for :func:`texar.tf.core.layers.get_layer`. """ nconv = self._hparams.num_conv_layers if len(pool_hparams) != nconv: raise ValueError("`pool_hparams` must be of length %d" % nconv) filters = _to_list(self._hparams.filters, 'filters', nconv) if nconv == 1: kernel_size = _to_list(self._hparams.kernel_size) if not isinstance(kernel_size[0], (list, tuple)): kernel_size = [kernel_size] elif nconv > 1: kernel_size = _to_list(self._hparams.kernel_size, 'kernel_size', nconv) kernel_size = [_to_list(ks) for ks in kernel_size] other_kwargs = self._hparams.other_conv_kwargs or {} if isinstance(other_kwargs, HParams): other_kwargs = other_kwargs.todict() if not isinstance(other_kwargs, dict): raise ValueError("hparams['other_conv_kwargs'] must be a dict.") conv_pool_hparams = [] activation_fn = get_activation_fn(self._hparams.conv_activation, self._hparams.conv_activation_kwargs) for i in range(nconv): hparams_i = [] names = [] for ks_ij in kernel_size[i]: name = uniquify_str("conv_%d" % (i + 1), names) names.append(name) conv_kwargs_ij = { "filters": filters[i], "kernel_size": ks_ij, "activation": activation_fn, "name": name } conv_kwargs_ij.update(other_kwargs) hparams_i.append({"type": "Conv1D", "kwargs": conv_kwargs_ij}) if len(hparams_i) == 1: conv_pool_hparams.append([hparams_i[0], pool_hparams[i]]) else: # creates MergeLayer mrg_kwargs_layers = [] for hparams_ij in hparams_i: seq_kwargs_j = {"layers": [hparams_ij, pool_hparams[i]]} mrg_kwargs_layers.append({ "type": "SequentialLayer", "kwargs": seq_kwargs_j }) mrg_hparams = { "type": "MergeLayer", "kwargs": { "layers": mrg_kwargs_layers, "name": "conv_pool_%d" % (i + 1) } } conv_pool_hparams.append(mrg_hparams) return conv_pool_hparams