Ejemplo n.º 1
0
 def __init__(self,
              layer_class=None,
              name="",
              network=None,
              train_flag=False,
              eval_flag=False,
              depth=1,
              consensus="flat",
              forward_weights_init=None,
              bias_init=None,
              weight_clip=0.0,
              cost=None,
              recurrent_weights_init=None,
              substitute_param_expr=None):
     """
 :param str layer_class: name of layer type, e.g. "hidden", "recurrent", "lstm" or so. see LayerClasses.
 :param str name: custom layer name, e.g. "hidden_2"
 :param Network.LayerNetwork network: the network which we will be part of
 :param str forward_weights_init: see self.create_forward_weights()
 :param str bias_init: see self.create_bias()
 """
     self.params = {}
     """ :type: dict[str,theano.compile.sharedvalue.SharedVariable] """
     self.attrs = {}
     """ :type: dict[str,str|float|int|bool|dict] """
     self.device = None
     if layer_class:
         self.layer_class = as_str(layer_class.encode("utf8"))
     self.name = as_str(name.encode("utf8"))
     self.train_flag = train_flag
     self.eval_flag = eval_flag
     self.depth = depth
     if depth != 1:
         self.set_attr('depth', depth)
     if consensus != "flat":
         self.set_attr('consensus', consensus)
     self.network = network
     if forward_weights_init:
         self.set_attr("forward_weights_init", forward_weights_init)
     self.forward_weights_init = forward_weights_init or "random_normal()"
     if recurrent_weights_init:
         self.set_attr("recurrent_weights_init", recurrent_weights_init)
     self.recurrent_weights_init = recurrent_weights_init or "random_uniform()"
     if bias_init:
         self.set_attr("bias_init", bias_init)
     self.bias_init = bias_init or "zeros()"
     if substitute_param_expr:
         self.set_attr("substitute_param_expr", substitute_param_expr)
     self.substitute_param_expr = substitute_param_expr
     if weight_clip:
         self.set_attr('weight_clip', weight_clip)
     if cost:
         self.set_attr('cost', cost)
Ejemplo n.º 2
0
 def from_hdf(cls, filename=None, model=None, load_params=True, **kwargs):
   """
   Gets the JSON from the hdf file, initializes the network and loads the network params.
   :param str|None filename: filename of hdf
   :param h5py.File|None model: hdf, if no filename is provided
   :param bool load_params: whether to load the params
   """
   if model is None:
     assert filename
     model = h5py.File(filename, "r")
     close_at_end = True
   else:
     assert not filename
     close_at_end = False
   assert "json" in model.attrs, "Maybe old network model where JSON was not stored. Use version before 2016-10-11."
   json_content_s = as_str(model.attrs['json'])
   assert json_content_s and json_content_s != "{}"
   json_content = json.loads(json_content_s)
   kwargs = kwargs.copy()
   if "n_out" not in kwargs:
     n_in, n_out = cls._n_in_out_from_hdf_model(model)
     n_out['__final'] = True
     kwargs["n_in"] = n_in
     kwargs["n_out"] = n_out
   network = cls.from_json(json_content, **kwargs)
   if load_params:
     network.load_hdf(model)
   if close_at_end:
     model.close()
   return network
Ejemplo n.º 3
0
    def load(self, head):
        """
    :type head: h5py.File
    """
        try:
            grp = head[self.name]
        except Exception:
            print("warning: unable to load parameters for layer",
                  self.name,
                  file=log.v3)
            return

        grp_class = as_str(grp.attrs['class'])
        if grp_class == "<unknown_softmax>":
            grp_class = "softmax"  # bug in some RETURNN version. can be ignored.
        if grp_class != self.layer_class:
            from .basic import get_layer_class
            if not get_layer_class(grp_class,
                                   raise_exception=False) is get_layer_class(
                                       self.layer_class):
                print("warning: invalid layer class (expected " +
                      self.layer_class + " got " + grp.attrs['class'] + ")",
                      file=log.v3)
        for p in self.params:
            if p not in grp:
                print("unable to load parameter %s in %s" % (p, self.name),
                      file=log.v4)
        for p in grp:
            if p in self.params:
                if self.params[p].get_value(
                        borrow=True,
                        return_internal_type=True).shape == grp[p].shape:
                    array = grp[p][...]
                    assert not (numpy.isinf(array).any()
                                or numpy.isnan(array).any())
                    self.params[p].set_value(array)
                else:
                    print("warning: invalid layer parameter shape for parameter " + p + " of layer " + self.name + \
                      " (expected  " + str(self.params[p].get_value(borrow=True, return_internal_type=True).shape) + \
                      " got " + str(grp[p].shape) + ")", file=log.v2)
                    #assert self.params[p].get_value(borrow=True, return_internal_type=True).shape == grp[p].shape, \
                    #  "invalid layer parameter shape for parameter " + p + " of layer " + self.name + \
                    #  " (expected  " + str(self.params[p].get_value(borrow=True, return_internal_type=True).shape) + \
                    #  " got " + str(grp[p].shape) + ")"
            else:
                print("unable to match parameter %s in %s" % (p, self.name),
                      file=log.v4)