Answers.Body: <p>Choose an appropriate accuracy metric for regression tasks, such as MAE or MAPE.</p>
  <p>Use the <code>batch_size</code> parameter to specify the batch size during training, and ensure the <code>validation_split</code> parameter is used for validation data splitting.</p>
  <p>Consider adjusting the model architecture and trying different optimizers to optimize your model's performance.</p>
  <p>It's always beneficial to experiment and fine-tune these parameters based on your specific problem and dataset characteristics.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-11T08:45:06.650Z
Answers.LastActivityDate: 2023-12-11T08:45:06.650Z
Answers.UserDisplayName: han
Body: <p>I want to predict the penalty/punishment given for fraud cases, with inputs in the form of (damage amount(\$), if recidivism), and targets in the format of (fine(\$), jail(months), community service(hours), probation(months)) - the following is my code:</p>
  <pre><code>fname = &quot;filepath.tsv&quot;
  all_in = []
  all_out = []
  
  with open(fname) as f:
      for i, line in enumerate(f):
          if i &lt; 3:#first line
              print(&quot;Header:&quot;, line.strip().split('\t'))
              continue
          fields = line.strip().split('\t')
          all_in.append([int(a.replace(&quot;,&quot;, &quot;&quot;)) for a in fields[5:7]])
          all_out.append([int(a.replace(&quot;,&quot;, &quot;&quot;)) for a in fields[7:11]])
  
  case_in = np.array(all_in, dtype = &quot;uint64&quot;)
  target_out = np.array(all_out, dtype = &quot;uint64&quot;)
  
  normalize_layer = tf.keras.layers.Normalization(axis=-1, name = &quot;normalize_in&quot;)
  normalize_layer.adapt(all_in)
  normalize_out = tf.keras.layers.Normalization(axis=-1, name = &quot;normalize_out&quot;)
  normalize_out.adapt(all_out)
  denormalize_out = tf.keras.layers.Normalization(axis=-1, invert = True, name = &quot;denormalize_out&quot;)
  denormalize_out.adapt(all_out)
  scaled_out = normalize_out(all_out)
  
  inputs = normalize_layer(Input(shape = 2))
  x = Dense(6, input_dim = 2, activation = &quot;sigmoid&quot;, use_bias = True)(inputs)
  x = Dense(4, activation = &quot;sigmoid&quot;)(x)
  y_4 = Dense(1, activation = &quot;sigmoid&quot;, name = &quot;y_4&quot;)(x)
  punishment = Dense(3, activation = &quot;sigmoid&quot;, name = &quot;punishment&quot;)(x)
  y_1 = Dense(1, activation = &quot;sigmoid&quot;, name = &quot;y_1&quot;)(punishment)
  y_2 = Dense(1, activation = &quot;sigmoid&quot;, name = &quot;y_2&quot;)(punishment)
  y_3 = Dense(1, activation = &quot;sigmoid&quot;, name= &quot;y_3&quot;)(punishment)
  
  model = Model(inputs = inputs, outputs = [y_1, y_2, y_3, y_4])
  
  model.compile(
      optimizer = SGD(learning_rate=0.01, weight_decay=1e-6, momentum=0.9, nesterov=True),
      loss = {
          &quot;y_1&quot; : &quot;MeanSquaredError&quot;,
          &quot;y_2&quot; : &quot;MeanSquaredError&quot;,
          &quot;y_3&quot; : &quot;MeanSquaredError&quot;,
          &quot;y_4&quot; : &quot;MeanSquaredError&quot;
      },
      metrics=['accuracy']
  )
  
  model.fit(
      case_in,
      scaled_out,
      batch_size = 10,#after # of data increases, increase
      epochs = 300,
      verbose = 2,
      validation_split = 0.8
  )
  
  for layer in model.layers:
      print(&quot;===== LAYER: &quot;, layer.name, &quot; =====&quot;)
      if layer.get_weights() != []:
          weights = layer.get_weights()[0]
          biases = layer.get_weights()[1]
          print(&quot;weights:&quot;)
          print(weights)
          print(&quot;biases:&quot;)
          print(biases)
      else:
          print(&quot;weights: &quot;, [])
  </code></pre>
  <p>And, once starting to train the model, the accuracy do not change at all. Also, while I was trying to fix that, I messed something up to make my data with ~50 datasets only one batch of the .fit operation, though my batch size says 10. I was just trying way too many things to fix my code from here and there on the internet and it kind of messed everything up even more.</p>
  <p>Initially, I tried normalizing the data, both my ins and outs, which unfroze my loss function, but it didn't really do anything to the accuracy function. I also changed the ReLU function to the sigmoid function too, because at some point it seemed it might be a problem with a dead ReLU, as the biases on my layers were not being updated from their initial 0s. After that, I kept nudging around the optimizer, the loss functions, and epochs, which all came to no avail.</p>
  <p>How to make the module actually work to its purpose? And do you have general feedback on the code?</p>
  
Tags: <machine-learning><python><keras>
Title: Keras accuracy does not change for multi-output module
UserDisplayName: Aliquis
ThreadText: # Keras accuracy does not change for multi-output module
  
  (Asked by: username_0 on 2023-12-08)
  
  I want to predict the penalty/punishment given for fraud cases, with inputs in the form of (damage amount(\$), if recidivism), and targets in the format of (fine(\$), jail(months), community service(hours), probation(months)) - the following is my code:
  fname = "filepath.tsv"
  all_in = []
  all_out = []
  
  with open(fname) as f:
      for i, line in enumerate(f):
          if i < 3:#first line
              print("Header:", line.strip().split('\t'))
              continue
          fields = line.strip().split('\t')
          all_in.append([int(a.replace(",", "")) for a in fields[5:7]])
          all_out.append([int(a.replace(",", "")) for a in fields[7:11]])
  
  case_in = np.array(all_in, dtype = "uint64")
  target_out = np.array(all_out, dtype = "uint64")
  
  normalize_layer = tf.keras.layers.Normalization(axis=-1, name = "normalize_in")
  normalize_layer.adapt(all_in)
  normalize_out = tf.keras.layers.Normalization(axis=-1, name = "normalize_out")
  normalize_out.adapt(all_out)
  denormalize_out = tf.keras.layers.Normalization(axis=-1, invert = True, name = "denormalize_out")
  denormalize_out.adapt(all_out)
  scaled_out = normalize_out(all_out)
  
  inputs = normalize_layer(Input(shape = 2))
  x = Dense(6, input_dim = 2, activation = "sigmoid", use_bias = True)(inputs)
  x = Dense(4, activation = "sigmoid")(x)
  y_4 = Dense(1, activation = "sigmoid", name = "y_4")(x)
  punishment = Dense(3, activation = "sigmoid", name = "punishment")(x)
  y_1 = Dense(1, activation = "sigmoid", name = "y_1")(punishment)
  y_2 = Dense(1, activation = "sigmoid", name = "y_2")(punishment)
  y_3 = Dense(1, activation = "sigmoid", name= "y_3")(punishment)
  
  model = Model(inputs = inputs, outputs = [y_1, y_2, y_3, y_4])
  
  model.compile(
      optimizer = SGD(learning_rate=0.01, weight_decay=1e-6, momentum=0.9, nesterov=True),
      loss = {
          "y_1" : "MeanSquaredError",
          "y_2" : "MeanSquaredError",
          "y_3" : "MeanSquaredError",
          "y_4" : "MeanSquaredError"
      },
      metrics=['accuracy']
  )
  
  model.fit(
      case_in,
      scaled_out,
      batch_size = 10,#after # of data increases, increase
      epochs = 300,
      verbose = 2,
      validation_split = 0.8
  )
  
  for layer in model.layers:
      print("===== LAYER: ", layer.name, " =====")
      if layer.get_weights() != []:
          weights = layer.get_weights()[0]
          biases = layer.get_weights()[1]
          print("weights:")
          print(weights)
          print("biases:")
          print(biases)
      else:
          print("weights: ", [])
  
  And, once starting to train the model, the accuracy do not change at all. Also, while I was trying to fix that, I messed something up to make my data with ~50 datasets only one batch of the .fit operation, though my batch size says 10. I was just trying way too many things to fix my code from here and there on the internet and it kind of messed everything up even more.
  Initially, I tried normalizing the data, both my ins and outs, which unfroze my loss function, but it didn't really do anything to the accuracy function. I also changed the ReLU function to the sigmoid function too, because at some point it seemed it might be a problem with a dead ReLU, as the biases on my layers were not being updated from their initial 0s. After that, I kept nudging around the optimizer, the loss functions, and epochs, which all came to no avail.
  How to make the module actually work to its purpose? And do you have general feedback on the code?
  
  **Tags:** machine-learning, python, keras
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-12-11. Score: 0 | Accepted answer
  
  Choose an appropriate accuracy metric for regression tasks, such as MAE or MAPE.
  
  Use the `batch_size` parameter to specify the batch size during training, and ensure the `validation_split` parameter is used for validation data splitting.
  
  Consider adjusting the model architecture and trying different optimizers to optimize your model's performance.
  
  It's always beneficial to experiment and fine-tune these parameters based on your specific problem and dataset characteristics.
  
  
  ---
  
  