parameters: (layer,
                                     weights,
                                     original_keras_version=None,
                                     original_backend=None)
return_statement: return weights
docstring: Converts layers weights from Keras 1 format to Keras 2.
  
    Arguments:
        layer: Layer instance.
        weights: List of weights values (Numpy arrays).
        original_keras_version: Keras version for the weights, as a string.
        original_backend: Keras backend the weights were trained with,
            as a string.
  
    Returns:
        A list of weights values (Numpy arrays).
docstring_summary: Converts layers weights from Keras 1 format to Keras 2.
docstring_tokens: Converts layers weights from Keras 1 format to Keras 2 .
function: def preprocess_weights_for_loading(layer,
                                     weights,
                                     original_keras_version=None,
                                     original_backend=None):
    """Converts layers weights from Keras 1 format to Keras 2.
  
    Arguments:
        layer: Layer instance.
        weights: List of weights values (Numpy arrays).
        original_keras_version: Keras version for the weights, as a string.
        original_backend: Keras backend the weights were trained with,
            as a string.
  
    Returns:
        A list of weights values (Numpy arrays).
    """
    if original_keras_version == '1':
      if layer.__class__.__name__ == 'Bidirectional':
        num_weights_per_layer = len(weights) // 2
  
        forward_weights = preprocess_weights_for_loading(
            layer.forward_layer, weights[:num_weights_per_layer],
            original_keras_version, original_backend)
        backward_weights = preprocess_weights_for_loading(
            layer.backward_layer, weights[num_weights_per_layer:],
            original_keras_version, original_backend)
        weights = forward_weights + backward_weights
  
      if layer.__class__.__name__ == 'TimeDistributed':
        weights = preprocess_weights_for_loading(
            layer.layer, weights, original_keras_version, original_backend)
  
      if layer.__class__.__name__ == 'Conv1D':
        shape = weights[0].shape
        # Handle Keras 1.1 format
        if shape[:2] != (layer.kernel_size[0], 1) or shape[3] != layer.filters:
          # Legacy shape:
          # (filters, input_dim, filter_length, 1)
          assert shape[0] == layer.filters and shape[2:] == (layer.kernel_size[0],
                                                             1)
          weights[0] = np.transpose(weights[0], (2, 3, 1, 0))
        weights[0] = weights[0][:, 0, :, :]
  
      if layer.__class__.__name__ == 'Conv2D':
        if layer.data_format == 'channels_first':
          # old: (filters, stack_size, kernel_rows, kernel_cols)
          # new: (kernel_rows, kernel_cols, stack_size, filters)
          weights[0] = np.transpose(weights[0], (2, 3, 1, 0))
  
      if layer.__class__.__name__ == 'Conv2DTranspose':
        if layer.data_format == 'channels_last':
          # old: (kernel_rows, kernel_cols, stack_size, filters)
          # new: (kernel_rows, kernel_cols, filters, stack_size)
          weights[0] = np.transpose(weights[0], (0, 1, 3, 2))
        if layer.data_format == 'channels_first':
          # old: (filters, stack_size, kernel_rows, kernel_cols)
          # new: (kernel_rows, kernel_cols, filters, stack_size)
          weights[0] = np.transpose(weights[0], (2, 3, 0, 1))
  
      if layer.__class__.__name__ == 'Conv3D':
        if layer.data_format == 'channels_first':
          # old: (filters, stack_size, ...)
          # new: (..., stack_size, filters)
          weights[0] = np.transpose(weights[0], (2, 3, 4, 1, 0))
  
      if layer.__class__.__name__ == 'GRU':
        if len(weights) == 9:
          kernel = np.concatenate([weights[0], weights[3], weights[6]], axis=-1)
          recurrent_kernel = np.concatenate(
              [weights[1], weights[4], weights[7]], axis=-1)
          bias = np.concatenate([weights[2], weights[5], weights[8]], axis=-1)
          weights = [kernel, recurrent_kernel, bias]
  
      if layer.__class__.__name__ == 'LSTM':
        if len(weights) == 12:
          # old: i, c, f, o
          # new: i, f, c, o
          kernel = np.concatenate(
              [weights[0], weights[6], weights[3], weights[9]], axis=-1)
          recurrent_kernel = np.concatenate(
              [weights[1], weights[7], weights[4], weights[10]], axis=-1)
          bias = np.concatenate(
              [weights[2], weights[8], weights[5], weights[11]], axis=-1)
          weights = [kernel, recurrent_kernel, bias]
  
      if layer.__class__.__name__ == 'ConvLSTM2D':
        if len(weights) == 12:
          kernel = np.concatenate(
              [weights[0], weights[6], weights[3], weights[9]], axis=-1)
          recurrent_kernel = np.concatenate(
              [weights[1], weights[7], weights[4], weights[10]], axis=-1)
          bias = np.concatenate(
              [weights[2], weights[8], weights[5], weights[11]], axis=-1)
          if layer.data_format == 'channels_first':
            # old: (filters, stack_size, kernel_rows, kernel_cols)
            # new: (kernel_rows, kernel_cols, stack_size, filters)
            kernel = np.transpose(kernel, (2, 3, 1, 0))
            recurrent_kernel = np.transpose(recurrent_kernel, (2, 3, 1, 0))
          weights = [kernel, recurrent_kernel, bias]
  
      if layer.__class__.__name__ in ['Model', 'Sequential']:
        new_weights = []
        # trainable weights
        for sublayer in layer.layers:
          num_weights = len(sublayer.trainable_weights)
          if num_weights > 0:
            new_weights.extend(
                preprocess_weights_for_loading(
                    layer=sublayer,
                    weights=weights[:num_weights],
                    original_keras_version=original_keras_version,
                    original_backend=original_backend))
            weights = weights[num_weights:]
  
        # non-trainable weights
        for sublayer in layer.layers:
          num_weights = len([
              l for l in sublayer.weights if l not in sublayer.trainable_weights
          ])
          if num_weights > 0:
            new_weights.extend(
                preprocess_weights_for_loading(
                    layer=sublayer,
                    weights=weights[:num_weights],
                    original_keras_version=original_keras_version,
                    original_backend=original_backend))
            weights = weights[num_weights:]
        weights = new_weights
  
    conv_layers = ['Conv1D', 'Conv2D', 'Conv3D', 'Conv2DTranspose', 'ConvLSTM2D']
    if layer.__class__.__name__ in conv_layers:
      if original_backend and K.backend() != original_backend:
        weights[0] = conv_utils.convert_kernel(weights[0])
        if layer.__class__.__name__ == 'ConvLSTM2D':
          weights[1] = conv_utils.convert_kernel(weights[1])
      if K.int_shape(layer.weights[0]) != weights[0].shape:
        weights[0] = np.transpose(weights[0], (3, 2, 0, 1))
        if layer.__class__.__name__ == 'ConvLSTM2D':
          weights[1] = np.transpose(weights[1], (3, 2, 0, 1))
    return weights
function_tokens: def preprocess_weights_for_loading ( layer , weights , original_keras_version = None , original_backend = None ) : if original_keras_version == '1' : if layer . __class__ . __name__ == 'Bidirectional' : num_weights_per_layer = len ( weights ) // 2 forward_weights = preprocess_weights_for_loading ( layer . forward_layer , weights [ : num_weights_per_layer ] , original_keras_version , original_backend ) backward_weights = preprocess_weights_for_loading ( layer . backward_layer , weights [ num_weights_per_layer : ] , original_keras_version , original_backend ) weights = forward_weights + backward_weights if layer . __class__ . __name__ == 'TimeDistributed' : weights = preprocess_weights_for_loading ( layer . layer , weights , original_keras_version , original_backend ) if layer . __class__ . __name__ == 'Conv1D' : shape = weights [ 0 ] . shape # Handle Keras 1.1 format if shape [ : 2 ] != ( layer . kernel_size [ 0 ] , 1 ) or shape [ 3 ] != layer . filters : # Legacy shape: # (filters, input_dim, filter_length, 1) assert shape [ 0 ] == layer . filters and shape [ 2 : ] == ( layer . kernel_size [ 0 ] , 1 ) weights [ 0 ] = np . transpose ( weights [ 0 ] , ( 2 , 3 , 1 , 0 ) ) weights [ 0 ] = weights [ 0 ] [ : , 0 , : , : ] if layer . __class__ . __name__ == 'Conv2D' : if layer . data_format == 'channels_first' : # old: (filters, stack_size, kernel_rows, kernel_cols) # new: (kernel_rows, kernel_cols, stack_size, filters) weights [ 0 ] = np . transpose ( weights [ 0 ] , ( 2 , 3 , 1 , 0 ) ) if layer . __class__ . __name__ == 'Conv2DTranspose' : if layer . data_format == 'channels_last' : # old: (kernel_rows, kernel_cols, stack_size, filters) # new: (kernel_rows, kernel_cols, filters, stack_size) weights [ 0 ] = np . transpose ( weights [ 0 ] , ( 0 , 1 , 3 , 2 ) ) if layer . data_format == 'channels_first' : # old: (filters, stack_size, kernel_rows, kernel_cols) # new: (kernel_rows, kernel_cols, filters, stack_size) weights [ 0 ] = np . transpose ( weights [ 0 ] , ( 2 , 3 , 0 , 1 ) ) if layer . __class__ . __name__ == 'Conv3D' : if layer . data_format == 'channels_first' : # old: (filters, stack_size, ...) # new: (..., stack_size, filters) weights [ 0 ] = np . transpose ( weights [ 0 ] , ( 2 , 3 , 4 , 1 , 0 ) ) if layer . __class__ . __name__ == 'GRU' : if len ( weights ) == 9 : kernel = np . concatenate ( [ weights [ 0 ] , weights [ 3 ] , weights [ 6 ] ] , axis = - 1 ) recurrent_kernel = np . concatenate ( [ weights [ 1 ] , weights [ 4 ] , weights [ 7 ] ] , axis = - 1 ) bias = np . concatenate ( [ weights [ 2 ] , weights [ 5 ] , weights [ 8 ] ] , axis = - 1 ) weights = [ kernel , recurrent_kernel , bias ] if layer . __class__ . __name__ == 'LSTM' : if len ( weights ) == 12 : # old: i, c, f, o # new: i, f, c, o kernel = np . concatenate ( [ weights [ 0 ] , weights [ 6 ] , weights [ 3 ] , weights [ 9 ] ] , axis = - 1 ) recurrent_kernel = np . concatenate ( [ weights [ 1 ] , weights [ 7 ] , weights [ 4 ] , weights [ 10 ] ] , axis = - 1 ) bias = np . concatenate ( [ weights [ 2 ] , weights [ 8 ] , weights [ 5 ] , weights [ 11 ] ] , axis = - 1 ) weights = [ kernel , recurrent_kernel , bias ] if layer . __class__ . __name__ == 'ConvLSTM2D' : if len ( weights ) == 12 : kernel = np . concatenate ( [ weights [ 0 ] , weights [ 6 ] , weights [ 3 ] , weights [ 9 ] ] , axis = - 1 ) recurrent_kernel = np . concatenate ( [ weights [ 1 ] , weights [ 7 ] , weights [ 4 ] , weights [ 10 ] ] , axis = - 1 ) bias = np . concatenate ( [ weights [ 2 ] , weights [ 8 ] , weights [ 5 ] , weights [ 11 ] ] , axis = - 1 ) if layer . data_format == 'channels_first' : # old: (filters, stack_size, kernel_rows, kernel_cols) # new: (kernel_rows, kernel_cols, stack_size, filters) kernel = np . transpose ( kernel , ( 2 , 3 , 1 , 0 ) ) recurrent_kernel = np . transpose ( recurrent_kernel , ( 2 , 3 , 1 , 0 ) ) weights = [ kernel , recurrent_kernel , bias ] if layer . __class__ . __name__ in [ 'Model' , 'Sequential' ] : new_weights = [ ] # trainable weights for sublayer in layer . layers : num_weights = len ( sublayer . trainable_weights ) if num_weights > 0 : new_weights . extend ( preprocess_weights_for_loading ( layer = sublayer , weights = weights [ : num_weights ] , original_keras_version = original_keras_version , original_backend = original_backend ) ) weights = weights [ num_weights : ] # non-trainable weights for sublayer in layer . layers : num_weights = len ( [ l for l in sublayer . weights if l not in sublayer . trainable_weights ] ) if num_weights > 0 : new_weights . extend ( preprocess_weights_for_loading ( layer = sublayer , weights = weights [ : num_weights ] , original_keras_version = original_keras_version , original_backend = original_backend ) ) weights = weights [ num_weights : ] weights = new_weights conv_layers = [ 'Conv1D' , 'Conv2D' , 'Conv3D' , 'Conv2DTranspose' , 'ConvLSTM2D' ] if layer . __class__ . __name__ in conv_layers : if original_backend and K . backend ( ) != original_backend : weights [ 0 ] = conv_utils . convert_kernel ( weights [ 0 ] ) if layer . __class__ . __name__ == 'ConvLSTM2D' : weights [ 1 ] = conv_utils . convert_kernel ( weights [ 1 ] ) if K . int_shape ( layer . weights [ 0 ] ) != weights [ 0 ] . shape : weights [ 0 ] = np . transpose ( weights [ 0 ] , ( 3 , 2 , 0 , 1 ) ) if layer . __class__ . __name__ == 'ConvLSTM2D' : weights [ 1 ] = np . transpose ( weights [ 1 ] , ( 3 , 2 , 0 , 1 ) ) return weights