Spaces:
Running
Running
Update finetuned_model.py
Browse files- finetuned_model.py +7 -7
finetuned_model.py
CHANGED
@@ -78,7 +78,7 @@ for _, row in df_yearly.iterrows():
|
|
78 |
"summary": f"In {year}, the S&P 500 averaged {sp500:.2f} with a {return_val:.1f}% annual return and a {real_return:.1f}% real return."
|
79 |
})
|
80 |
|
81 |
-
# Period-specific questions (1-year, 3-year, 5-year, 10-year, and
|
82 |
years = df_yearly['Year'].unique()
|
83 |
for year in years:
|
84 |
for duration in [1, 3, 5, 10]:
|
@@ -97,8 +97,8 @@ for year in years:
|
|
97 |
"answer": f"The S&P 500’s {duration}-year average annual inflation-adjusted return from {start_year} to {end_year} was approximately {avg_real_return:.1f}%."
|
98 |
})
|
99 |
|
100 |
-
# Custom period questions
|
101 |
-
custom_periods = [(2000, 2010), (2011, 2016), (2010, 2020), (2000, 2008), (2015, 2024)]
|
102 |
for start_year, end_year in custom_periods:
|
103 |
df_period = df_yearly[(df_yearly['Year'] >= start_year) & (df_yearly['Year'] <= end_year)]
|
104 |
if not df_period.empty:
|
@@ -139,8 +139,8 @@ qa_pairs.append({
|
|
139 |
"answer": "The S&P 500 index fund’s average annual return is approximately 10–12% over the long term (1927–2025), including dividends, based on historical data."
|
140 |
})
|
141 |
qa_pairs.append({
|
142 |
-
"question": "What was the average annual return of the S&P 500 between
|
143 |
-
"answer": "The S&P 500’s average annual return from
|
144 |
})
|
145 |
|
146 |
# Save to JSON
|
@@ -177,7 +177,7 @@ training_args = TrainingArguments(
|
|
177 |
output_dir="./finetuned_model",
|
178 |
evaluation_strategy="epoch",
|
179 |
learning_rate=1e-5,
|
180 |
-
per_device_train_batch_size=8,
|
181 |
per_device_eval_batch_size=8,
|
182 |
num_train_epochs=7,
|
183 |
weight_decay=0.01,
|
@@ -206,7 +206,7 @@ trainer.save_model("./finetuned_model")
|
|
206 |
tokenizer.save_pretrained("./finetuned_model")
|
207 |
|
208 |
# Test the model
|
209 |
-
input_text = "What was the average annual return of the S&P 500 between
|
210 |
inputs = tokenizer(input_text, return_tensors="pt")
|
211 |
outputs = model.generate(**inputs, max_new_tokens=40)
|
212 |
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
|
|
78 |
"summary": f"In {year}, the S&P 500 averaged {sp500:.2f} with a {return_val:.1f}% annual return and a {real_return:.1f}% real return."
|
79 |
})
|
80 |
|
81 |
+
# Period-specific questions (1-year, 3-year, 5-year, 10-year, and recent ranges)
|
82 |
years = df_yearly['Year'].unique()
|
83 |
for year in years:
|
84 |
for duration in [1, 3, 5, 10]:
|
|
|
97 |
"answer": f"The S&P 500’s {duration}-year average annual inflation-adjusted return from {start_year} to {end_year} was approximately {avg_real_return:.1f}%."
|
98 |
})
|
99 |
|
100 |
+
# Custom period questions, including recent periods
|
101 |
+
custom_periods = [(2000, 2010), (2011, 2016), (2010, 2020), (2000, 2008), (2015, 2024), (2020, 2022)]
|
102 |
for start_year, end_year in custom_periods:
|
103 |
df_period = df_yearly[(df_yearly['Year'] >= start_year) & (df_yearly['Year'] <= end_year)]
|
104 |
if not df_period.empty:
|
|
|
139 |
"answer": "The S&P 500 index fund’s average annual return is approximately 10–12% over the long term (1927–2025), including dividends, based on historical data."
|
140 |
})
|
141 |
qa_pairs.append({
|
142 |
+
"question": "What was the average annual return of the S&P 500 between 2020 and 2022?",
|
143 |
+
"answer": "The S&P 500’s average annual return from 2020 to 2022 was approximately 9.0%, including dividends, with significant volatility due to the COVID-19 recovery and 2022 bear market."
|
144 |
})
|
145 |
|
146 |
# Save to JSON
|
|
|
177 |
output_dir="./finetuned_model",
|
178 |
evaluation_strategy="epoch",
|
179 |
learning_rate=1e-5,
|
180 |
+
per_device_train_batch_size=8,
|
181 |
per_device_eval_batch_size=8,
|
182 |
num_train_epochs=7,
|
183 |
weight_decay=0.01,
|
|
|
206 |
tokenizer.save_pretrained("./finetuned_model")
|
207 |
|
208 |
# Test the model
|
209 |
+
input_text = "What was the average annual return of the S&P 500 between 2020 and 2022?"
|
210 |
inputs = tokenizer(input_text, return_tensors="pt")
|
211 |
outputs = model.generate(**inputs, max_new_tokens=40)
|
212 |
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|