Answers.Body: <p>As far as I know, you can't get multiple outputs from keras.sequential and need as many output layers as outputs, creating branching into your NN. Something like this :</p>
  <pre><code>def build_model(n_steps, n_features):
  
      inputs = tf.keras.Input(shape=(n_steps, n_features))
      x = tf.keras.layers.Conv1D(filters=64, kernel_size=3,padding='Same', activation='relu')(inputs)
      x = tf.keras.layers.Conv1D(filters=64, kernel_size=3, padding='Same', activation='relu')(x)
      x = tf.keras.layers.MaxPooling1D(pool_size=2)(x)
  
      x = tf.keras.layers.Conv1D(filters=128, kernel_size=3, padding='Same', activation='relu')(x)
      x = tf.keras.layers.Conv1D(filters=128, kernel_size=3, padding='Same', activation='relu')(x)
     x = tf.keras.layers.MaxPooling1D(pool_size=2)(x)
  
     x = tf.keras.layers.Conv1D(filters=256, kernel_size=3, padding='Same', activation='relu')(x)
      x = tf.keras.layers.Conv1D(filters=256, kernel_size=3, padding='Same', activation='relu')(x)
      x = tf.keras.layers.Conv1D(filters=256, kernel_size=3, padding='Same', activation='relu')(x)
      x = tf.keras.layers.MaxPooling1D(pool_size=2)(x)
  
  
      x = tf.keras.layers.Flatten()(x)
      x = tf.keras.layers.Dense(1096, activation='relu')(x)
  
      outupt_1 = tf.keras.layers.Dense(1)(x)
      outupt_2 = tf.keras.layers.Dense(1)(x)
  
      model = tf.keras.Model(inputs=inputs, outputs=[outupt_1,outupt_2])
  
      optimizer = Adam(learning_rate=0.0005)
  
      model.compile(optimizer=optimizer, loss={'outupt_1': 'mse', 'outupt_1''mse'}, metrics={'outupt_1': 'mae', 'outupt_1''mae'})
      return model
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-04-03T21:18:10.907Z
Answers.LastActivityDate: 2024-04-03T21:18:10.907Z
Answers.UserDisplayName: Samael
Body: <p>I am working on a regression task where the model has to predict two values at the same time. The idea is that the dataset consists of 16 features, where the first 8 features represent the first value that the model needs to predict and the next 8 features represent the second position. How should I go about doing this and adapt my model? Initially the model predicted just one single value from a dataset of 8 features</p>
  <p>This is how one row of the data set looks like for example:</p>
  <p>0,003345971 0,003345971 0,003345971 0,003345971 0,003345971 0,003346    0,003346    0,003346    0,003525485 0,00445148  0,00349107  0,00417936  0,00370763  0,00390843  0,00400138  0,00362335</p>
  <p>This is how part of the output file looks like, The two columns are the same</p>
  <div class="s-table-container"><table class="s-table">
  <thead>
  <tr>
  <th>First column</th>
  <th>Second column</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>0</td>
  <td>0</td>
  </tr>
  <tr>
  <td>0.002</td>
  <td>0.002</td>
  </tr>
  <tr>
  <td>0.004</td>
  <td>0.004</td>
  </tr>
  <tr>
  <td>0.006</td>
  <td>0.006</td>
  </tr>
  <tr>
  <td>0.008</td>
  <td>0.008</td>
  </tr>
  <tr>
  <td>0.01</td>
  <td>0.01</td>
  </tr>
  <tr>
  <td>0.012</td>
  <td>0.012</td>
  </tr>
  </tbody>
  </table></div>
  <p>This is how the model architecture for predicting one single value looks like</p>
  <pre><code>def build_model(n_steps, n_features):
      model = Sequential()
  
      model.add(Conv1D(filters=64, kernel_size=3, input_shape=(n_steps, n_features), padding='Same', activation='relu'))
      model.add(Conv1D(filters=64, kernel_size=3, padding='Same', activation='relu'))
      model.add(MaxPooling1D(pool_size=2))
  
      model.add(Conv1D(filters=128, kernel_size=3, padding='Same', activation='relu'))
      model.add(Conv1D(filters=128, kernel_size=3, padding='Same', activation='relu'))
      model.add(MaxPooling1D(pool_size=2))
  
      model.add(Conv1D(filters=256, kernel_size=3, padding='Same', activation='relu'))
      model.add(Conv1D(filters=256, kernel_size=3, padding='Same', activation='relu'))
      model.add(Conv1D(filters=256, kernel_size=3, padding='Same', activation='relu'))
      model.add(MaxPooling1D(pool_size=2))
  
  
      model.add(Flatten())
      model.add(Dense(1096, activation='relu'))
  
      model.add(Dense(1))
  
      optimizer = Adam(learning_rate=0.0005)
  
      model.compile(optimizer=optimizer, loss='mse', metrics=['mae'])
      return model
  
  </code></pre>
  <p>This is how I train the model and the callbacks used. Using EarlyStopping does not seem to make the model better</p>
  <pre><code>
  def train_model(model, x_train, y_train, x_val, y_val, batch_size):
      early_stopping = EarlyStopping(monitor='val_loss', patience=100, verbose=1)
      learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss', patience=3, verbose=1, factor=0.5, min_lr=0.00001)
  
      log_dir = &quot;logs/fit/&quot; + datetime.now().strftime(&quot;%Y%m%d-%H%M%S&quot;)
      tensorboard_callback = TensorBoard(log_dir=log_dir, histogram_freq=1)
  
      history = model.fit(
          x_train, y_train, epochs=1000,
          verbose=1,
          validation_data=(x_val, y_val),
          callbacks=[learning_rate_reduction, early_stopping],
          batch_size=batch_size
      )
  
      avg_mae = np.mean(history.history['mae'])
      print(f&quot;Average MAE: {avg_mae}&quot;)
  
      plot_history(history)
  
      # return history
      return history, history.history['val_mae'], history.history['val_loss']
  
  </code></pre>
  <p>This is the function that splits the data</p>
  <pre><code>def split_data(idata, odata):
      x = idata.iloc[:, 0:].values
      y = odata.iloc[:, np.r_[0, 1]].values
  
  
      x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1, random_state=0)
  
      return np.asarray(x_train).astype(np.float32), np.asarray(y_train).astype(np.float32), np.asarray(x_test).astype(
          np.float32), np.asarray(y_test).astype(np.float32)
  <span class="math-container">```</span>
  </code></pre>
  
Tags: <neural-networks><deep-learning><convolutional-neural-networks><deep-neural-networks><regression>
Title: CNN multioutput regression architecture modification
UserDisplayName: lukachu03
ThreadText: # CNN multioutput regression architecture modification
  
  (Asked by: username_0 on 2024-03-22)
  
  I am working on a regression task where the model has to predict two values at the same time. The idea is that the dataset consists of 16 features, where the first 8 features represent the first value that the model needs to predict and the next 8 features represent the second position. How should I go about doing this and adapt my model? Initially the model predicted just one single value from a dataset of 8 features
  This is how one row of the data set looks like for example:
  0,003345971 0,003345971 0,003345971 0,003345971 0,003345971 0,003346    0,003346    0,003346    0,003525485 0,00445148  0,00349107  0,00417936  0,00370763  0,00390843  0,00400138  0,00362335
  This is how part of the output file looks like, The two columns are the same
  
  
  
  First column
  Second column
  
  
  
  
  0
  0
  
  
  0.002
  0.002
  
  
  0.004
  0.004
  
  
  0.006
  0.006
  
  
  0.008
  0.008
  
  
  0.01
  0.01
  
  
  0.012
  0.012
  
  
  
  This is how the model architecture for predicting one single value looks like
  def build_model(n_steps, n_features):
      model = Sequential()
  
      model.add(Conv1D(filters=64, kernel_size=3, input_shape=(n_steps, n_features), padding='Same', activation='relu'))
      model.add(Conv1D(filters=64, kernel_size=3, padding='Same', activation='relu'))
      model.add(MaxPooling1D(pool_size=2))
  
      model.add(Conv1D(filters=128, kernel_size=3, padding='Same', activation='relu'))
      model.add(Conv1D(filters=128, kernel_size=3, padding='Same', activation='relu'))
      model.add(MaxPooling1D(pool_size=2))
  
      model.add(Conv1D(filters=256, kernel_size=3, padding='Same', activation='relu'))
      model.add(Conv1D(filters=256, kernel_size=3, padding='Same', activation='relu'))
      model.add(Conv1D(filters=256, kernel_size=3, padding='Same', activation='relu'))
      model.add(MaxPooling1D(pool_size=2))
  
  
      model.add(Flatten())
      model.add(Dense(1096, activation='relu'))
  
      model.add(Dense(1))
  
      optimizer = Adam(learning_rate=0.0005)
  
      model.compile(optimizer=optimizer, loss='mse', metrics=['mae'])
      return model
  
  
  This is how I train the model and the callbacks used. Using EarlyStopping does not seem to make the model better
  
  def train_model(model, x_train, y_train, x_val, y_val, batch_size):
      early_stopping = EarlyStopping(monitor='val_loss', patience=100, verbose=1)
      learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss', patience=3, verbose=1, factor=0.5, min_lr=0.00001)
  
      log_dir = "logs/fit/" + datetime.now().strftime("%Y%m%d-%H%M%S")
      tensorboard_callback = TensorBoard(log_dir=log_dir, histogram_freq=1)
  
      history = model.fit(
          x_train, y_train, epochs=1000,
          verbose=1,
          validation_data=(x_val, y_val),
          callbacks=[learning_rate_reduction, early_stopping],
          batch_size=batch_size
      )
  
      avg_mae = np.mean(history.history['mae'])
      print(f"Average MAE: {avg_mae}")
  
      plot_history(history)
  
      # return history
      return history, history.history['val_mae'], history.history['val_loss']
  
  
  This is the function that splits the data
  def split_data(idata, odata):
      x = idata.iloc[:, 0:].values
      y = odata.iloc[:, np.r_[0, 1]].values
  
  
      x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1, random_state=0)
  
      return np.asarray(x_train).astype(np.float32), np.asarray(y_train).astype(np.float32), np.asarray(x_test).astype(
          np.float32), np.asarray(y_test).astype(np.float32)
  ```
  
  **Tags:** neural-networks, deep-learning, convolutional-neural-networks, deep-neural-networks, regression
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-04-03. Score: 0
  
  As far as I know, you can't get multiple outputs from keras.sequential and need as many output layers as outputs, creating branching into your NN. Something like this :
  def build_model(n_steps, n_features):
  
      inputs = tf.keras.Input(shape=(n_steps, n_features))
      x = tf.keras.layers.Conv1D(filters=64, kernel_size=3,padding='Same', activation='relu')(inputs)
      x = tf.keras.layers.Conv1D(filters=64, kernel_size=3, padding='Same', activation='relu')(x)
      x = tf.keras.layers.MaxPooling1D(pool_size=2)(x)
  
      x = tf.keras.layers.Conv1D(filters=128, kernel_size=3, padding='Same', activation='relu')(x)
      x = tf.keras.layers.Conv1D(filters=128, kernel_size=3, padding='Same', activation='relu')(x)
     x = tf.keras.layers.MaxPooling1D(pool_size=2)(x)
  
     x = tf.keras.layers.Conv1D(filters=256, kernel_size=3, padding='Same', activation='relu')(x)
      x = tf.keras.layers.Conv1D(filters=256, kernel_size=3, padding='Same', activation='relu')(x)
      x = tf.keras.layers.Conv1D(filters=256, kernel_size=3, padding='Same', activation='relu')(x)
      x = tf.keras.layers.MaxPooling1D(pool_size=2)(x)
  
  
      x = tf.keras.layers.Flatten()(x)
      x = tf.keras.layers.Dense(1096, activation='relu')(x)
  
      outupt_1 = tf.keras.layers.Dense(1)(x)
      outupt_2 = tf.keras.layers.Dense(1)(x)
  
      model = tf.keras.Model(inputs=inputs, outputs=[outupt_1,outupt_2])
  
      optimizer = Adam(learning_rate=0.0005)
  
      model.compile(optimizer=optimizer, loss={'outupt_1': 'mse', 'outupt_1''mse'}, metrics={'outupt_1': 'mae', 'outupt_1''mae'})
      return model
  
  
  ---
  
  