Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

i have an model< and difference input shape, if it's univariate so the inputshape is…

  1. User

    i have an model< and difference input shape, if it's univariate so the input_shape is (128,1)< for multivariate it could be (128,4)

    code for model

    энкодер трансформера с регуляризацией и батч-нормализацией

    def gru_branch(inputs, input_shape, units, dropout, l2_reg=0.01): x = layers.LayerNormalization(epsilon=1e-6)(inputs) x = layers.GRU(units, activation='relu', return_sequences=True, kernel_regularizer=regularizers.l2(l2_reg))(x) x = layers.Dropout(dropout)(x) x = layers.Dense(input_shape[0], activation='relu', kernel_regularizer=regularizers.l2(l2_reg))(x) return x + inputs

    def transformer_encoder(inputs, head_size, num_heads, ff_dim, dropout=0): # Normalization and Attention x = layers.LayerNormalization(epsilon=1e-6)(inputs) x = layers.MultiHeadAttention( key_dim=head_size, num_heads=num_heads, dropout=dropout )(x, x) x = layers.Dropout(dropout)(x) res = x + inputs

    # Feed Forward Part
    x = layers.LayerNormalization(epsilon=1e-6)(res)
    x = layers.Conv1D(filters=ff_dim, kernel_size=1, activation="relu")(x)
    x = layers.Dropout(dropout)(x)
    x = layers.Conv1D(filters=inputs.shape[-1], kernel_size=1)(x)
    return x + res
    

    def build_model( input_shape, head_size, num_heads, ff_dim, num_transformer_blocks, mlp_units, gru_units, l2_reg, dropout, mlp_dropout ): inputs = keras.Input(shape=input_shape) x = inputs y = inputs

    for _ in range(num_transformer_blocks):
        x = transformer_encoder(x, head_size, num_heads, ff_dim, dropout)
    
    y = gru_branch(y, input_shape, gru_units, dropout, l2_reg) #new
    
    x = layers.Concatenate()([x, y]) #new
    
    x = layers.GlobalAveragePooling1D(data_format="channels_first")(x)
    
    for dim in mlp_units:
        x = layers.Dense(dim, activation="relu")(x)
        x = layers.Dropout(mlp_dropout)(x)
    
    #univariate
    outputs = layers.Dense(64)(x)
    
    return keras.Model(inputs, outputs)
    

    adapt this code for both multi and uni variate inputs

  2. Assistant

    Certainly! The key to adapting the model for both univariate and multivariate inputs lies in handling the difference in input shapes and ensuring the final layer provides an appropriate output shape for both cases. Below, the provided model is adapted to handle both univariate and multivariate inputs based on a parameter num_features.

    from tensorflow.keras import layers, regularizers, models
    import tensorflow as tf
    
    def gru_branch(inputs, input_shape, units, dropout, l2_reg=0.01):
        x = layers.LayerNormalization(epsilon=1e-6)(inputs)
        x = layers.GRU(units, activation='relu', return_sequences=True, kernel_regularizer=regularizers.l2(l2_reg))(x)
        x = layers.Dropout(dropout)(x)
        x = layers.Dense(input_shape[0], activation='relu', kernel_regularizer=regularizers.l2(l2_reg))(x)
        return x + inputs
    
    def transformer_encoder(inputs, head_size, num_heads, ff_dim, dropout=0):
        # Normalization and Attention
        x = layers.LayerNormalization(epsilon=1e-6)(inputs)
        x = layers.MultiHeadAttention(
            key_dim=head_size, num_heads=num_heads, dropout=dropout
        )(x, x)
        x = layers.Dropout(dropout)(x)
        res = x + inputs
    
        # Feed Forward Part
        x = layers.LayerNormalization(epsilon=1e-6)(res)
        x = layers.Conv1D(filters=ff_dim, kernel_size=1, activation="relu")(x)
        x = layers.Dropout(dropout)(x)
        x = layers.Conv1D(filters=inputs.shape[-1], kernel_size=1)(x)
        return x + res
    
    def build_model(
        input_shape,
        head_size,
        num_heads,
        ff_dim,
        num_transformer_blocks,
        mlp_units,
        gru_units,
        l2_reg,
        dropout,
        mlp_dropout,
        num_outputs,  # separate univariate/multivariate by different outputs
    ):
        inputs = keras.Input(shape=input_shape)
        x = inputs
        y = inputs
        
        for _ in range(num_transformer_blocks):
            x = transformer_encoder(x, head_size, num_heads, ff_dim, dropout)
    
        y = gru_branch(y, input_shape, gru_units, dropout, l2_reg) #new
        
        x = layers.Concatenate()([x, y]) #new
    
        x = layers.GlobalAveragePooling1D(data_format="channels_first")(x)
        
        for dim in mlp_units:
            x = layers.Dense(dim, activation="relu")(x)
            x = layers.Dropout(mlp_dropout)(x)
        
        outputs = layers.Dense(num_outputs)(x)  # Adjusted to handle different output dimensions
        
        return models.Model(inputs, outputs)
    

    Example Usage

    For Univariate Input:

    univariate_shape = (128, 1)
    num_outputs = 1  # For univariate time series output
    model = build_model(
        input_shape=univariate_shape,
        head_size=256,
        num_heads=4,
        ff_dim=4,
        num_transformer_blocks=4,
        mlp_units=[128],
        gru_units=128,
        l2_reg=0.01,
        dropout=0.1,
        mlp_dropout=0.1,
        num_outputs=num_outputs
    )
    

    For Multivariate Input:

    multivariate_shape = (128, 4)
    num_outputs = 4  # For multivariate time series output
    model = build_model(
        input_shape=multivariate_shape,
        head_size=256,
        num_heads=4,
        ff_dim=4,
        num_transformer_blocks=4,
        mlp_units=[128],
        gru_units=128,
        l2_reg=0.01,
        dropout=0.1,
        mlp_dropout=0.1,
        num_outputs=num_outputs
    )
    

    By adjusting input_shape and num_outputs as needed, this model can cater to both univariate and multivariate time series data.

  3. User

    no outputs don't only depend on num_features, output should be as (forecast_length, num_features)

  4. Assistant

    I understand. The output shape needs to be (forecast_length, num_features) where forecast_length is the length of the prediction horizon, and num_features corresponds to the number of features in the time series (e.g., 1 for univariate, or n for multivariate).

    Below is the adapted code that handles both cases as you described:

    from tensorflow.keras import layers, regularizers, models
    import tensorflow as tf
    
    def gru_branch(inputs, input_shape, units, dropout, l2_reg=0.01):
        x = layers.LayerNormalization(epsilon=1e-6)(inputs)
        x = layers.GRU(units, activation='relu', return_sequences=True, kernel_regularizer=regularizers.l2(l2_reg))(x)
        x = layers.Dropout(dropout)(x)
        x = layers.Dense(input_shape[0], activation='relu', kernel_regularizer=regularizers.l2(l2_reg))(x)
        return x + inputs
    
    def transformer_encoder(inputs, head_size, num_heads, ff_dim, dropout=0):
        # Normalization and Attention
        x = layers.LayerNormalization(epsilon=1e-6)(inputs)
        x = layers.MultiHeadAttention(
            key_dim=head_size, num_heads=num_heads, dropout=dropout
        )(x, x)
        x = layers.Dropout(dropout)(x)
        res = x + inputs
    
        # Feed Forward Part
        x = layers.LayerNormalization(epsilon=1e-6)(res)
        x = layers.Conv1D(filters=ff_dim, kernel_size=1, activation="relu")(x)
        x = layers.Dropout(dropout)(x)
        x = layers.Conv1D(filters=inputs.shape[-1], kernel_size=1)(x)
        return x + res
    
    def build_model(
        input_shape,
        head_size,
        num_heads,
        ff_dim,
        num_transformer_blocks,
        mlp_units,
        gru_units,
        l2_reg,
        dropout,
        mlp_dropout,
        forecast_length
    ):
        num_features = input_shape[1]
    
        inputs = keras.Input(shape=input_shape)
        x = inputs
        y = inputs
        
        for _ in range(num_transformer_blocks):
            x = transformer_encoder(x, head_size, num_heads, ff_dim, dropout)
    
        y = gru_branch(y, input_shape, gru_units, dropout, l2_reg) #new
        
        x = layers.Concatenate()([x, y]) #new
    
        x = layers.GlobalAveragePooling1D(data_format="channels_first")(x)
        
        for dim in mlp_units:
            x = layers.Dense(dim, activation="relu")(x)
            x = layers.Dropout(mlp_dropout)(x)
        
        # Outputs has shape (forecast_length, num_features)
        output_shape = (forecast_length, num_features)
        x = layers.RepeatVector(forecast_length)(x)
        outputs = layers.TimeDistributed(layers.Dense(num_features))(x)
        
        return models.Model(inputs, outputs)
    
    
    # Example Usage
    univariate_shape = (128, 1)
    multivariate_shape = (128, 4)
    forecast_length = 10  # Length of the forecast horizon
    
    # For Univariate Input
    univariate_model = build_model(
        input_shape=univariate_shape,
        head_size=256,
        num_heads=4,
        ff_dim=4,
        num_transformer_blocks=4,
        mlp_units=[128],
        gru_units=128,
        l2_reg=0.01,
        dropout=0.1,
        mlp_dropout=0.1,
        forecast_length=forecast_length
    )
    
    # For Multivariate Input
    multivariate_model = build_model(
        input_shape=multivariate_shape,
        head_size=256,
        num_heads=4,
        ff_dim=4,
        num_transformer_blocks=4,
        mlp_units=[128],
        gru_units=128,
        l2_reg=0.01,
        dropout=0.1,
        mlp_dropout=0.1,
        forecast_length=forecast_length
    )
    
    # Print model summaries for validation
    univariate_model.summary()
    multivariate_model.summary()
    

    This code constructs a model that can handle both univariate and multivariate inputs and produces outputs with the shape (forecast_length, num_features). The key is the RepeatVector and TimeDistributed layers, which ensure the correct forecasting shape and structure for the multi-step predictions.

  5. User

    check what's wrong

    энкодер трансформера с регуляризацией и батч-нормализацией

    def gru_branch(inputs, input_shape, units, dropout, l2_reg=0.01): x = layers.LayerNormalization(epsilon=1e-6)(inputs) x = layers.GRU(units, activation='tanh', return_sequences=True, kernel_regularizer=regularizers.l2(l2_reg))(x) x = layers.Dropout(dropout)(x) x = layers.Dense(input_shape[0], activation='relu', kernel_regularizer=regularizers.l2(l2_reg))(x) return x + inputs

    def transformer_encoder(inputs, head_size, num_heads, ff_dim, dropout=0): # Normalization and Attention x = layers.LayerNormalization(epsilon=1e-6)(inputs) x = layers.MultiHeadAttention( key_dim=head_size, num_heads=num_heads, dropout=dropout )(x, x) x = layers.Dropout(dropout)(x) res = x + inputs

    # Feed Forward Part
    x = layers.LayerNormalization(epsilon=1e-6)(res)
    x = layers.Conv1D(filters=ff_dim, kernel_size=1, activation="relu")(x)
    x = layers.Dropout(dropout)(x)
    x = layers.Conv1D(filters=inputs.shape[-1], kernel_size=1)(x)
    return x + res
    

    энкодер трансформера с регуляризацией и батч-нормализацией

    def gru_branch(inputs, input_shape, units, dropout, l2_reg=0.01): x = layers.LayerNormalization(epsilon=1e-6)(inputs) x = layers.GRU(units, activation='tanh', return_sequences=True, kernel_regularizer=regularizers.l2(l2_reg))(x) x = layers.Dropout(dropout)(x) x = layers.Dense(input_shape[0], activation='relu', kernel_regularizer=regularizers.l2(l2_reg))(x) return x + inputs

    def transformer_encoder(inputs, head_size, num_heads, ff_dim, dropout=0): # Normalization and Attention x = layers.LayerNormalization(epsilon=1e-6)(inputs) x = layers.MultiHeadAttention( key_dim=head_size, num_heads=num_heads, dropout=dropout )(x, x) x = layers.Dropout(dropout)(x) res = x + inputs

    # Feed Forward Part
    x = layers.LayerNormalization(epsilon=1e-6)(res)
    x = layers.Conv1D(filters=ff_dim, kernel_size=1, activation="relu")(x)
    x = layers.Dropout(dropout)(x)
    x = layers.Conv1D(filters=inputs.shape[-1], kernel_size=1)(x)
    return x + res
    

    class CombinedLoss(Loss): def init(self, alpha=0.5): super(CombinedLoss, self).init() self.alpha = alpha

    def call(self, y_true, y_pred):
        epsilon = tf.keras.backend.epsilon()  # Малое значение для избежания деления на ноль
        diff = tf.abs(y_true - y_pred) + epsilon  # Разница между предсказанным и истинным значением
        log_diff = -tf.math.log(diff)  # Логарифм от разницы, чтобы минимизировать её
        log_loss = tf.reduce_mean(log_diff)
    
        mse_loss = tf.reduce_mean(tf.square(y_true - y_pred))  # MSE loss
    
        combined_loss = self.alpha * log_loss + (1 - self.alpha) * mse_loss
        return combined_loss
    

    class CombinedLoss(Loss): def init(self, alpha=0.5): super(CombinedLoss, self).init() self.alpha = alpha

    def call(self, y_true, y_pred):
        epsilon = tf.keras.backend.epsilon()  # Малое значение для избежания деления на ноль
        diff = tf.abs(y_true - y_pred) + epsilon  # Разница между предсказанным и истинным значением
        log_diff = -tf.math.log(diff)  # Логарифм от разницы, чтобы минимизировать её
        log_loss = tf.reduce_mean(log_diff)
    
        mse_loss = tf.reduce_mean(tf.square(y_true - y_pred))  # MSE loss
    
        combined_loss = self.alpha * log_loss + (1 - self.alpha) * mse_loss
        return combined_loss
    

    Обучение

    if TRAINING: with tf.device('/GPU:0'): # Старт логирования в MLflow mlflow.keras.autolog()

        with mlflow.start_run(experiment_id=experiment_id, run_name=run_name):
            model.fit(
                train_dataset,
                validation_data=val_dataset,
                epochs=num_epochs,
                batch_size=batch_size,
                callbacks=[reduce_lr_callback, early_stopping_callback, checkpoint_callback, lr_scheduler]
            )
    
            #Сохранение модели в MLflow
            model.save_weights(checkpoint_filepath)
            mlflow.log_artifact(checkpoint_filepath)
            
        #Завершение логирования в Mlflow
        mlflow.end_run()
    

    InvalidArgumentError Traceback (most recent call last) Cell In[21], line 9 6 mlflow.keras.autolog() 8 with mlflow.start_run(experiment_id=experiment_id, run_name=run_name): ----> 9 model.fit( 10 train_dataset, 11 validation_data=val_dataset, 12 epochs=num_epochs, 13 batch_size=batch_size, 14 callbacks=[reduce_lr_callback, early_stopping_callback, checkpoint_callback, lr_scheduler] 15 ) 17 #Сохранение модели в MLflow 18 model.save_weights(checkpoint_filepath)

    File ~\miniconda3\envs\pronin\lib\site-packages\mlflow\utils\autologging_utils\safety.py:574, in safe_patch..safe_patch_function(*args, **kwargs) 564 try_log_autologging_event( 565 AutologgingEventLogger.get_logger().log_patch_function_start, 566 session, (...) 570 kwargs, 571 ) 573 if patch_is_class: --> 574 patch_function.call(call_original, *args, **kwargs) 575 else: 576 patch_function(call_original, *args, **kwargs)

    File ~\miniconda3\envs\pronin\lib\site-packages\mlflow\utils\autologging_utils\safety.py:165, in PatchFunction.call(cls, original, *args, **kwargs) 163 @classmethod 164 def call(cls, original, *args, **kwargs): --> 165 return cls().call(original, *args, **kwargs)

    File ~\miniconda3\envs\pronin\lib\site-packages\mlflow\utils\autologging_utils\safety.py:176, in PatchFunction.call(self, original, *args, **kwargs) 172 self._on_exception(e) 173 finally: 174 # Regardless of what happens during the _on_exception callback, reraise 175 # the original implementation exception once the callback completes --> 176 raise e

    File ~\miniconda3\envs\pronin\lib\site-packages\mlflow\utils\autologging_utils\safety.py:169, in PatchFunction.call(self, original, *args, **kwargs) 167 def call(self, original, *args, **kwargs): 168 try: --> 169 return self._patch_implementation(original, *args, **kwargs) 170 except (Exception, KeyboardInterrupt) as e: 171 try:

    File ~\miniconda3\envs\pronin\lib\site-packages\mlflow\utils\autologging_utils\safety.py:227, in with_managed_run..PatchWithManagedRun._patch_implementation(self, original, *args, **kwargs) 224 if not mlflow.active_run(): 225 self.managed_run = create_managed_run() --> 227 result = super()._patch_implementation(original, *args, **kwargs) 229 if self.managed_run: 230 mlflow.end_run(RunStatus.to_string(RunStatus.FINISHED))

    File ~\miniconda3\envs\pronin\lib\site-packages\mlflow\tensorflow_init_.py:1333, in autolog..FitPatch._patch_implementation(self, original, inst, *args, **kwargs) 1326 except Exception as e: 1327 _logger.warning( 1328 "Failed to log training dataset information to " 1329 "MLflow Tracking. Reason: %s", 1330 e, 1331 ) -> 1333 history = original(inst, *args, **kwargs) 1335 if log_models: 1336 _log_keras_model(history, args)

    File ~\miniconda3\envs\pronin\lib\site-packages\mlflow\utils\autologging_utils\safety.py:557, in safe_patch..safe_patch_function..call_original(og_args, **og_kwargs) 554 original_result = original(_og_args, **_og_kwargs) 555 return original_result --> 557 return call_original_fn_with_event_logging(_original_fn, og_args, og_kwargs)

    File ~\miniconda3\envs\pronin\lib\site-packages\mlflow\utils\autologging_utils\safety.py:492, in safe_patch..safe_patch_function..call_original_fn_with_event_logging(original_fn, og_args, og_kwargs) 483 try: 484 try_log_autologging_event( 485 AutologgingEventLogger.get_logger().log_original_function_start, 486 session, (...) 490 og_kwargs, 491 ) --> 492 original_fn_result = original_fn(*og_args, **og_kwargs) 494 try_log_autologging_event( 495 AutologgingEventLogger.get_logger().log_original_function_success, 496 session, (...) 500 og_kwargs, 501 ) 502 return original_fn_result

    File ~\miniconda3\envs\pronin\lib\site-packages\mlflow\utils\autologging_utils\safety.py:554, in safe_patch..safe_patch_function..call_original.._original_fn(_og_args, **_og_kwargs) 546 # Show all non-MLflow warnings as normal (i.e. not as event logs) 547 # during original function execution, even if silent mode is enabled 548 # (silent=True), since these warnings originate from the ML framework 549 # or one of its dependencies and are likely relevant to the caller 550 with set_non_mlflow_warnings_behavior_for_current_thread( 551 disable_warnings=False, 552 reroute_warnings=False, 553 ): --> 554 original_result = original(_og_args, **_og_kwargs) 555 return original_result

    File ~\miniconda3\envs\pronin\lib\site-packages\keras\utils\traceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs) 67 filtered_tb = _process_traceback_frames(e.traceback) 68 # To get the full stack trace, call: 69 # tf.debugging.disable_traceback_filtering() ---> 70 raise e.with_traceback(filtered_tb) from None 71 finally: 72 del filtered_tb

    File ~\miniconda3\envs\pronin\lib\site-packages\tensorflow\python\eager\execute.py:54, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name) 52 try: 53 ctx.ensure_initialized() ---> 54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, 55 inputs, attrs, num_outputs) 56 except core._NotOkStatusException as e: 57 if name is not None:

    InvalidArgumentError: Graph execution error:

    Detected at node 'CombinedLoss/sub' defined at (most recent call last): File "C:\Users\tecon\miniconda3\envs\pronin\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\tecon\miniconda3\envs\pronin\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\ipykernel_launcher.py", line 18, in app.launch_new_instance() File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\traitlets\config\application.py", line 1075, in launch_instance app.start() File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\ipykernel\kernelapp.py", line 739, in start self.io_loop.start() File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\tornado\platform\asyncio.py", line 205, in start self.asyncio_loop.run_forever() File "C:\Users\tecon\miniconda3\envs\pronin\lib\asyncio\base_events.py", line 600, in run_forever self._run_once() File "C:\Users\tecon\miniconda3\envs\pronin\lib\asyncio\base_events.py", line 1896, in _run_once handle._run() File "C:\Users\tecon\miniconda3\envs\pronin\lib\asyncio\events.py", line 80, in _run self._context.run(self._callback, *self._args) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\ipykernel\kernelbase.py", line 545, in dispatch_queue await self.process_one() File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\ipykernel\kernelbase.py", line 534, in process_one await dispatch(*args) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\ipykernel\kernelbase.py", line 437, in dispatch_shell await result File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\ipykernel\ipkernel.py", line 362, in execute_request await super().execute_request(stream, ident, parent) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\ipykernel\kernelbase.py", line 778, in execute_request reply_content = await reply_content File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\ipykernel\ipkernel.py", line 449, in do_execute res = shell.run_cell( File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\ipykernel\zmqshell.py", line 549, in run_cell return super().run_cell(*args, **kwargs) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\IPython\core\interactiveshell.py", line 3075, in run_cell result = self._run_cell( File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\IPython\core\interactiveshell.py", line 3130, in _run_cell result = runner(coro) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\IPython\core\async_helpers.py", line 129, in pseudo_sync_runner coro.send(None) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\IPython\core\interactiveshell.py", line 3334, in run_cell_async has_raised = await self.run_ast_nodes(code_ast.body, cell_name, File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\IPython\core\interactiveshell.py", line 3517, in run_ast_nodes if await self.run_code(code, result, async=asy): File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\IPython\core\interactiveshell.py", line 3577, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "C:\Users\tecon\AppData\Local\Temp\ipykernel_112156\338443566.py", line 9, in model.fit( File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\mlflow\utils\autologging_utils\safety.py", line 574, in safe_patch_function patch_function.call(call_original, *args, **kwargs) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\mlflow\utils\autologging_utils\safety.py", line 165, in call return cls().call(original, *args, **kwargs) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\mlflow\utils\autologging_utils\safety.py", line 169, in call return self._patch_implementation(original, *args, **kwargs) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\mlflow\utils\autologging_utils\safety.py", line 227, in _patch_implementation result = super().patch_implementation(original, *args, **kwargs) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\mlflow\tensorflow_init.py", line 1333, in _patch_implementation history = original(inst, *args, **kwargs) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\mlflow\utils\autologging_utils\safety.py", line 557, in call_original return call_original_fn_with_event_logging(_original_fn, og_args, og_kwargs) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\mlflow\utils\autologging_utils\safety.py", line 492, in call_original_fn_with_event_logging original_fn_result = original_fn(og_args, **og_kwargs) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\mlflow\utils\autologging_utils\safety.py", line 554, in _original_fn original_result = original(_og_args, **_og_kwargs) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler return fn(*args, **kwargs) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\keras\engine\training.py", line 1564, in fit tmp_logs = self.train_function(iterator) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\keras\engine\training.py", line 1160, in train_function return step_function(self, iterator) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\keras\engine\training.py", line 1146, in step_function outputs = model.distribute_strategy.run(run_step, args=(data,)) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\keras\engine\training.py", line 1135, in run_step outputs = model.train_step(data) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\keras\engine\training.py", line 994, in train_step loss = self.compute_loss(x, y, y_pred, sample_weight) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\keras\engine\training.py", line 1052, in compute_loss return self.compiled_loss( File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\keras\engine\compile_utils.py", line 265, in call loss_value = loss_obj(y_t, y_p, sample_weight=sw) File "C:\Users\tecon\miniconda3\envs\pronin\lib\site-packages\keras\losses.py", line 152, in call losses = call_fn(y_true, y_pred) File "C:\Users\tecon\AppData\Local\Temp\ipykernel_112156\2206673266.py", line 26, in call diff = tf.abs(y_true - y_pred) + epsilon # Разница между предсказанным и истинным значением Node: 'CombinedLoss/sub' required broadcastable shapes [[{{node CombinedLoss/sub}}]] [Op:__inference_train_function_9647]

  6. Assistant

    It looks like the error you're encountering is related to incompatible shapes during the computation of the loss function. Specifically, the line diff = tf.abs(y_true - y_pred) + epsilon is causing an issue because the shapes of y_true and y_pred are not broadcastable (i.e., they can't be aligned for an element-wise subtraction).

    To diagnose and resolve this, you should:

    1. Ensure that the shapes of y_true and y_pred match before they are processed in the loss function.
    2. Verify the shapes of the model outputs and the true labels used during training.

    Here are the changes needed:

    1. Ensure that y_true and y_pred have the same shape:

      • Both y_true and y_pred should have the shape (batch_size, forecast_length, num_features).
    2. Ensure correct shape in the model’s build function:

      • The final output layer should output a tensor of shape (batch_size, forecast_length, num_features).

    Let's update the model and the loss function accordingly:

    Updated CombinedLoss Class

    import tensorflow as tf
    from tensorflow.keras.losses import Loss
    
    class CombinedLoss(Loss):
        def __init__(self, alpha=0.5):
            super(CombinedLoss, self).__init__()
            self.alpha = alpha
    
        def call(self, y_true, y_pred):
            epsilon = tf.keras.backend.epsilon()  # Small value to avoid dividing by zero
            diff = tf.abs(tf.cast(y_true, tf.float32) - tf.cast(y_pred, tf.float32)) + epsilon  # Cast to float32
            log_diff = -tf.math.log(diff)  # Logarithm of the difference to minimize it
            log_loss = tf.reduce_mean(log_diff)
    
            mse_loss = tf.reduce_mean(tf.square(y_true - y_pred))  # MSE loss
    
            combined_loss = self.alpha * log_loss + (1 - self.alpha) * mse_loss
            return combined_loss
    

    Updated build_model Function

    from tensorflow.keras import layers, regularizers, models
    import keras
    
    def gru_branch(inputs, input_shape, units, dropout, l2_reg=0.01):
        x = layers.LayerNormalization(epsilon=1e-6)(inputs)
        x = layers.GRU(units, activation='tanh', return_sequences=True, kernel_regularizer=regularizers.l2(l2_reg))(x)
        x = layers.Dropout(dropout)(x)
        x = layers.Dense(input_shape[0], activation='relu', kernel_regularizer=regularizers.l2(l2_reg))(x)
        return x + inputs
    
    def transformer_encoder(inputs, head_size, num_heads, ff_dim, dropout=0):
        x = layers.LayerNormalization(epsilon=1e-6)(inputs)
        x = layers.MultiHeadAttention(
            key_dim=head_size, num_heads=num_heads, dropout=dropout
        )(x, x)
        x = layers.Dropout(dropout)(x)
        res = x + inputs
    
        x = layers.LayerNormalization(epsilon=1e-6)(res)
        x = layers.Conv1D(filters=ff_dim, kernel_size=1, activation="relu")(x)
        x = layers.Dropout(dropout)(x)
        x = layers.Conv1D(filters=inputs.shape[-1], kernel_size=1)(x)
        return x + res
    
    def build_model(
        input_shape,
        head_size,
        num_heads,
        ff_dim,
        num_transformer_blocks,
        mlp_units,
        gru_units,
        l2_reg,
        dropout,
        mlp_dropout,
        forecast_length
    ):
        num_features = input_shape[1]
    
        inputs = keras.Input(shape=input_shape)
        x = inputs
        y = inputs
        
        for _ in range(num_transformer_blocks):
            x = transformer_encoder(x, head_size, num_heads, ff_dim, dropout)
    
        y = gru_branch(y, input_shape, gru_units, dropout, l2_reg)
        
        x = layers.Concatenate()([x, y])
    
        x = layers.GlobalAveragePooling1D()(x)  # Note: No data_format argument
        
        for dim in mlp_units:
            x = layers.Dense(dim, activation="relu")(x)
            x = layers.Dropout(mlp_dropout)(x)
        
        x = layers.RepeatVector(forecast_length)(x)
        outputs = layers.TimeDistributed(layers.Dense(num_features))(x)
        
        return models.Model(inputs, outputs)
    
    # Example Usage
    univariate_shape = (128, 1)
    multivariate_shape = (128, 4)
    forecast_length = 10
    
    # For Univariate Input
    univariate_model = build_model(
        input_shape=univariate_shape,
        head_size=256,
        num_heads=4,
        ff_dim=4,
        num_transformer_blocks=4,
        mlp_units=[128],
        gru_units=128,
        l2_reg=0.01,
        dropout=0.1,
        mlp_dropout=0.1,
        forecast_length=forecast_length
    )
    
    # For Multivariate Input
    multivariate_model = build_model(
        input_shape=multivariate_shape,
        head_size=256,
        num_heads=4,
        ff_dim=4,
        num_transformer_blocks=4,
        mlp_units=[128],
        gru_units=128,
        l2_reg=0.01,
        dropout=0.1,
        mlp_dropout=0.1,
        forecast_length=forecast_length
    )
    
    # Print model summaries for validation
    univariate_model.summary()
    multivariate_model.summary()
    

    Training Loop

    Ensure you compile your model with the correct loss function, and double-check your dataset to ensure shapes match:

    from keras import optimizers
    
    # Use the custom loss
    loss_function = CombinedLoss()
    
    # Compile the model
    model.compile(
        optimizer=optimizers.Adam(),
        loss=loss_function,
        metrics=['mae']
    )
    
    # Training: Ensure train_dataset and val_dataset have shapes (batch_size, 128, num_features)
    if TRAINING:
        with tf.device('/GPU:0'):
            mlflow.keras.autolog()
            with mlflow.start_run(experiment_id=experiment_id, run_name=run_name):
                history = model.fit(
                    train_dataset,
                    validation_data=val_dataset,
                    epochs=num_epochs,
                    batch_size=batch_size,
                    callbacks=[reduce_lr_callback, early_stopping_callback, checkpoint_callback, lr_scheduler]
                )
                model.save_weights(checkpoint_filepath)
                mlflow.log_artifact(checkpoint_filepath)
            mlflow.end_run()
    

    Ensure your datasets (both train_dataset and val_dataset) yield data in the shape (batch_size, 128, num_features) where 128 is the sequence length and num_features is either 1 (univariate) or more (multivariate). This way, during loss computation, y_true and y_pred will have compatible shapes.