Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

Suppose I have a neural network in neuroph. After training the network, I want to test…

  1. User

    Suppose I have a neural network in neuroph. After training the network, I want to test it with some new data. However, the values end up being much larger than I want them to be. However, I still want to use these values, since what matters more is not the actual values but the trend in values. So even if I expect values less than 1000, but get values like 2000, 3000, 4000, 10000, I can still tell that the network is predicting a rising trend. To remedy this, I want to be able to scale the values the network predicts. Specifically, I want to fit the predicted values within the min and max of the 20 most recent values. How might I do this?

  2. Assistant

    To scale the predicted values from your neural network in a way that fits them within the range of the 20 most recent values, you can follow a min-max normalization approach. This will take the predicted values and linearly transform them so that they fit within a desired range, which in your case is defined by the minimum and maximum of the 20 most recent actual values. Here's a step-by-step process:

    1. Collect Recent Values: Maintain a list of the last 20 predicted actual values before you want to scale your new predictions. Let's call this list recentValues.

    2. Calculate Min and Max: Compute the minimum and maximum of the recentValues.

      double minRecent = Collections.min(recentValues);
      double maxRecent = Collections.max(recentValues);
      
    3. Scale Predicted Values: For each predicted value, apply min-max scaling using the formula: [ \text{scaledValue} = \text{minRecent} + \left( \frac{\text{predictedValue} - \text{minPredicted}}{\text{maxPredicted} - \text{minPredicted}} \right) \times (\text{maxRecent} - \text{minRecent}) ] Here, minPredicted and maxPredicted represent the minimum and maximum values of your predicted output values before scaling.

    4. Implementation: Here's how you can implement this in Java:

      double[] predictedValues; // Array of predicted values from your network
      double[] recentValues; // Array of the most recent actual values
      int recentCount = Math.min(20, recentValues.length); // Ensure we only consider up to 20 values
      
      // Calculate min and max of recent values
      double minRecent = Double.MAX_VALUE;
      double maxRecent = Double.MIN_VALUE;
      for (int i = 0; i < recentCount; i++) {
          if (recentValues[i] < minRecent) {
              minRecent = recentValues[i];
          }
          if (recentValues[i] > maxRecent) {
              maxRecent = recentValues[i];
          }
      }
      
      // Calculate min and max of predicted values
      double minPredicted = Double.MAX_VALUE;
      double maxPredicted = Double.MIN_VALUE;
      for (double predicted : predictedValues) {
          if (predicted < minPredicted) {
              minPredicted = predicted;
          }
          if (predicted > maxPredicted) {
              maxPredicted = predicted;
          }
      }
      
      // Scale predicted values
      double[] scaledValues = new double[predictedValues.length];
      for (int i = 0; i < predictedValues.length; i++) {
          // Apply scaling
          scaledValues[i] = minRecent + ((predictedValues[i] - minPredicted) / (maxPredicted - minPredicted)) * (maxRecent - minRecent);
      }
      
    5. Handling Edge Cases: Be cautious about edge cases where all predicted values are equal (which would lead to a division by zero). You can add checks to handle such conditions. If maxPredicted - minPredicted is 0 (indicating no variance), you might want to choose a representative value within the recent range instead.

    By applying this transformation, your predicted values will be scaled into the range defined by your most recent actual values, maintaining the trend while ensuring the values are kept within expected limits.