MightyOctopus commited on
Commit
002afbe
·
1 Parent(s): 08e76a1

Add refined ensemble agent logic with multi level model dominance logic for more accurate outcomes

Browse files
Files changed (3) hide show
  1. agents/ensemble_agent.py +51 -34
  2. day2.4.ipynb +0 -0
  3. testing.py +1 -1
agents/ensemble_agent.py CHANGED
@@ -1,4 +1,6 @@
 
1
  import pandas as pd
 
2
  from sklearn.linear_model import LinearRegression
3
  import joblib
4
  from agents.agents import Agent
@@ -23,57 +25,72 @@ class EnsembleAgent(Agent):
23
  self.log("Ensemble Agent is ready!")
24
 
25
 
26
- def price(self, description: str) -> float:
 
 
 
 
27
  """
28
  Run this ensemble model
29
  Ask each of the models to price the product
30
  Then use the Linear Regression model to return the weighted price
31
-
32
  :param description: the description of a product
33
  :return: an estimate of its price
34
  """
35
-
36
  self.log("Running Ensemble Agent - collaborating with specialist, frontier and neural network agents...")
37
 
38
- desc_into_str = description.prompt.replace(
39
  "How much does this cost to the nearest dollar?\n\n", ""
40
  ).split("\n\nPrice is $")[0]
41
-
42
- specialist = self.specialist.price(desc_into_str)
43
- frontier = self.frontier.price(desc_into_str)
44
- neural_network = self.neural_network.price(desc_into_str)
45
 
46
- ### Only include specialist and frontier model's results to get the average price in a more stable range
47
- ### as neural network model often makes drastically far off price estimates.
 
48
 
49
- ### rough_price V1:
50
- # rough_price = (specialist + frontier) / 2
 
 
 
 
 
 
 
 
 
 
51
 
52
- ### rough_price V2:
53
- # rough_price = specialist * 0.2 + frontier * 0.8
54
 
55
  ### Apply a different pricing distribution depending on the estimated price range based on each model's best accuracy by range
56
  ### Experiment Logs: https://docs.google.com/document/d/1RqaQeTpferlkdPNkXn1aEnrSq9d5uQs7cSWBWDS7As8/edit?tab=t.0
57
- # if rough_price < 50:
58
- # combined = frontier * 0.7 + specialist * 0.3
59
- # elif rough_price < 100:
60
- # combined = frontier * 0.35 + specialist * 0.6 + neural_network * 0.05
61
- # elif rough_price < 150:
62
- # combined = frontier * 0.9 + specialist * 0.08 + neural_network * 0.02
63
- # elif rough_price < 200:
64
- # combined = frontier * 0.9 + specialist * 0.07 + neural_network * 0.03
65
- # elif rough_price < 250:
66
- # combined = frontier * 0.7 + specialist * 0.3
67
- # elif rough_price < 300:
68
- # combined = frontier * 0.6 + specialist * 0.3 + neural_network * 0.1
69
- # elif rough_price < 350:
70
- # combined = frontier * 0.8 + specialist * 0.2
71
- # elif rough_price < 400:
72
- # combined = frontier * 0.9 + specialist * 0.1
73
- # else:
74
- # combined = frontier * 0.9 + specialist * 0.1
75
-
76
- ###### Simplified version of pricing contribution here
77
 
78
  self.log(f"Ensemble Agent complete - returning ${combined:.2f}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  return round(combined, 2)
 
1
+ import math
2
  import pandas as pd
3
+ from PIL.ImageStat import Global
4
  from sklearn.linear_model import LinearRegression
5
  import joblib
6
  from agents.agents import Agent
 
25
  self.log("Ensemble Agent is ready!")
26
 
27
 
28
+ ### Total Price Range Error for Each:
29
+ frontier_err, special_err, neural_err = (0, 0, 0)
30
+
31
+
32
+ def price(self, description: str, y_truth: float=None) -> float:
33
  """
34
  Run this ensemble model
35
  Ask each of the models to price the product
36
  Then use the Linear Regression model to return the weighted price
37
+
38
  :param description: the description of a product
39
  :return: an estimate of its price
40
  """
41
+
42
  self.log("Running Ensemble Agent - collaborating with specialist, frontier and neural network agents...")
43
 
44
+ processed_desc = description.replace(
45
  "How much does this cost to the nearest dollar?\n\n", ""
46
  ).split("\n\nPrice is $")[0]
 
 
 
 
47
 
48
+ specialist = self.specialist.price(processed_desc)
49
+ frontier = self.frontier.price(processed_desc)
50
+ neural_network = self.neural_network.price(processed_desc)
51
 
52
+ ### rough_price options to determine which model contributes more to deciding the price range
53
+ ### Some rough_price eliminates certain models to remove the volatility and to keep more stable price range estimations
54
+ def estimate_price_range(option):
55
+ if option == "o1":
56
+ ### rough_price Option 1:
57
+ return (specialist + frontier) / 2
58
+ elif option == "o2":
59
+ ## rough_price Option 2:
60
+ return specialist * 0.2 + frontier * 0.8
61
+ else:
62
+ ## rough_price Option 3:
63
+ return frontier
64
 
65
+ rough_price = estimate_price_range("o3")
 
66
 
67
  ### Apply a different pricing distribution depending on the estimated price range based on each model's best accuracy by range
68
  ### Experiment Logs: https://docs.google.com/document/d/1RqaQeTpferlkdPNkXn1aEnrSq9d5uQs7cSWBWDS7As8/edit?tab=t.0
69
+
70
+ ### Simplified version of allocating model dominance
71
+ if rough_price < 100:
72
+ combined = frontier * 0.7 + specialist * 0.3
73
+ elif rough_price < 200:
74
+ combined = frontier * 0.85 + specialist * 0.1 + neural_network * 0.05
75
+ elif rough_price < 300:
76
+ combined = frontier * 0.7 + specialist * 0.2 + neural_network * 0.1
77
+ else:
78
+ combined = frontier * 0.9 + specialist * 0.1
 
 
 
 
 
 
 
 
 
 
79
 
80
  self.log(f"Ensemble Agent complete - returning ${combined:.2f}")
81
+
82
+ ### This code below was placed for testing/experiment purposes. At inference, this below doesn't affect it.
83
+ if y_truth is not None:
84
+ f_err = abs(frontier - y_truth)
85
+ s_err = abs(specialist - y_truth)
86
+ n_err = abs(neural_network - y_truth)
87
+
88
+ EnsembleAgent.frontier_err += f_err
89
+ EnsembleAgent.special_err += s_err
90
+ EnsembleAgent.neural_err += n_err
91
+
92
+ self.log(f"Frontier Err: {EnsembleAgent.frontier_err:,.2f}")
93
+ self.log(f"Special Err: {EnsembleAgent.special_err:,.2f}")
94
+ self.log(f"Neural Err: {EnsembleAgent.neural_err:,.2f}")
95
+
96
  return round(combined, 2)
day2.4.ipynb CHANGED
The diff for this file is too large to render. See raw diff
 
testing.py CHANGED
@@ -31,7 +31,7 @@ class Tester:
31
 
32
  def run_datapoint(self, i):
33
  datapoint = self.data[i]
34
- guess = self.predictor(datapoint)
35
  truth = datapoint.price
36
  error = abs(guess - truth)
37
  log_error = math.log(truth + 1) - math.log(guess + 1)
 
31
 
32
  def run_datapoint(self, i):
33
  datapoint = self.data[i]
34
+ guess = self.predictor(datapoint.prompt, datapoint.price)
35
  truth = datapoint.price
36
  error = abs(guess - truth)
37
  log_error = math.log(truth + 1) - math.log(guess + 1)