parameters: (f, layers)
docstring: Implements name-based weight loading.
  
    (instead of topological weight loading).
  
    Layers that have no matching name are skipped.
  
    Arguments:
        f: A pointer to a HDF5 group.
        layers: a list of target layers.
  
    Raises:
        ValueError: in case of mismatch between provided layers
            and weights file.
docstring_summary: Implements name-based weight loading.
docstring_tokens: Implements name - based weight loading .
function: def load_weights_from_hdf5_group_by_name(f, layers):
    """Implements name-based weight loading.
  
    (instead of topological weight loading).
  
    Layers that have no matching name are skipped.
  
    Arguments:
        f: A pointer to a HDF5 group.
        layers: a list of target layers.
  
    Raises:
        ValueError: in case of mismatch between provided layers
            and weights file.
    """
    if 'keras_version' in f.attrs:
      original_keras_version = f.attrs['keras_version'].decode('utf8')
    else:
      original_keras_version = '1'
    if 'backend' in f.attrs:
      original_backend = f.attrs['backend'].decode('utf8')
    else:
      original_backend = None
  
    # New file format.
    layer_names = [n.decode('utf8') for n in f.attrs['layer_names']]
  
    # Reverse index of layer name to list of layers with name.
    index = {}
    for layer in layers:
      if layer.name:
        index.setdefault(layer.name, []).append(layer)
  
    # We batch weight value assignments in a single backend call
    # which provides a speedup in TensorFlow.
    weight_value_tuples = []
    for k, name in enumerate(layer_names):
      g = f[name]
      weight_names = [n.decode('utf8') for n in g.attrs['weight_names']]
      weight_values = [g[weight_name] for weight_name in weight_names]
  
      for layer in index.get(name, []):
        symbolic_weights = layer.weights
        weight_values = preprocess_weights_for_loading(
            layer, weight_values, original_keras_version, original_backend)
        if len(weight_values) != len(symbolic_weights):
          raise ValueError('Layer #' + str(k) + ' (named "' + layer.name +
                           '") expects ' + str(len(symbolic_weights)) +
                           ' weight(s), but the saved weights' + ' have ' +
                           str(len(weight_values)) + ' element(s).')
        # Set values.
        for i in range(len(weight_values)):
          weight_value_tuples.append((symbolic_weights[i], weight_values[i]))
    K.batch_set_value(weight_value_tuples)
function_tokens: def load_weights_from_hdf5_group_by_name ( f , layers ) : if 'keras_version' in f . attrs : original_keras_version = f . attrs [ 'keras_version' ] . decode ( 'utf8' ) else : original_keras_version = '1' if 'backend' in f . attrs : original_backend = f . attrs [ 'backend' ] . decode ( 'utf8' ) else : original_backend = None # New file format. layer_names = [ n . decode ( 'utf8' ) for n in f . attrs [ 'layer_names' ] ] # Reverse index of layer name to list of layers with name. index = { } for layer in layers : if layer . name : index . setdefault ( layer . name , [ ] ) . append ( layer ) # We batch weight value assignments in a single backend call # which provides a speedup in TensorFlow. weight_value_tuples = [ ] for k , name in enumerate ( layer_names ) : g = f [ name ] weight_names = [ n . decode ( 'utf8' ) for n in g . attrs [ 'weight_names' ] ] weight_values = [ g [ weight_name ] for weight_name in weight_names ] for layer in index . get ( name , [ ] ) : symbolic_weights = layer . weights weight_values = preprocess_weights_for_loading ( layer , weight_values , original_keras_version , original_backend ) if len ( weight_values ) != len ( symbolic_weights ) : raise ValueError ( 'Layer #' + str ( k ) + ' (named "' + layer . name + '") expects ' + str ( len ( symbolic_weights ) ) + ' weight(s), but the saved weights' + ' have ' + str ( len ( weight_values ) ) + ' element(s).' ) # Set values. for i in range ( len ( weight_values ) ) : weight_value_tuples . append ( ( symbolic_weights [ i ] , weight_values [ i ] ) ) K . batch_set_value ( weight_value_tuples )