# 修改记录日志 (日期: 2025-07-16)

## 1. 核心 Bug 修复

### 文件: `server/core/predictor.py`

- **问题**: 在 `train_model` 方法中调用内部辅助函数 `_prepare_training_params` 时,没有正确传递 `product_ids` 和 `store_ids` 参数,导致在 `_prepare_training_params` 内部发生 `NameError`。
- **修复**:
    - 修正了 `train_model` 方法内部对 `_prepare_training_params` 的调用,确保 `product_ids` 和 `store_ids` 被显式传递。
    - 此前已修复 `train_model` 的函数签名,使其能正确接收 `store_ids`。
- **结果**: 彻底解决了训练流程中的参数传递问题,根除了由此引发的 `NameError`。

## 2. 代码清理与重构

### 文件: `server/api.py`

- **内容**: 移除了在 `start_training` API 端点中遗留的旧版、基于线程(`threading.Thread`)的训练逻辑。
- **原因**: 该代码块已被新的、基于多进程(`multiprocessing`)的 `TrainingProcessManager` 完全取代。旧代码中包含了大量用于调试的 `thread_safe_print` 日志,已无用处。
- **结果**: `start_training` 端点的逻辑变得更加清晰,只负责参数校验和向 `TrainingProcessManager` 提交任务。

### 文件: `server/utils/training_process_manager.py`

- **内容**: 在 `TrainingWorker` 的 `run_training_task` 方法中,移除了一个用于模拟训练进度的 `for` 循环。
- **原因**: 该循环包含 `time.sleep(1)`,仅用于在没有实际训练逻辑时模拟进度更新,现在实际的训练器会通过回调函数报告真实进度,因此该模拟代码不再需要。
- **结果**: `TrainingWorker` 现在直接调用实际的训练器,不再有模拟延迟,代码更贴近生产环境。

## 3. 启动依赖

- **Python**: 3.x
- **主要库**:
    - Flask
    - Flask-SocketIO
    - Flasgger
    - pandas
    - numpy
    - torch
    - scikit-learn
    - matplotlib
- **启动命令**: `python server/api.py`
This commit is contained in:
xz2000 2025-07-16 15:34:48 +08:00
parent e999ed4af2
commit a9a0e51769
13 changed files with 715 additions and 1523 deletions

View File

@ -609,15 +609,17 @@ const startTraining = async () => {
epochs: form.epochs,
training_mode: 'global', //
training_scope: form.training_scope,
aggregation_method: form.aggregation_method
aggregation_method: form.aggregation_method,
store_ids: form.store_ids || [], //
product_ids: form.product_ids || [] //
};
if (form.store_ids.length > 0) {
payload.store_ids = form.store_ids;
// 使ID
if (payload.store_ids.length > 0) {
payload.store_id = payload.store_ids[0];
}
if (form.product_ids.length > 0) {
payload.product_ids = form.product_ids;
if (payload.product_ids.length > 0) {
payload.product_id = payload.product_ids[0];
}
if (form.training_type === "retrain") {

View File

@ -228,7 +228,11 @@
prop="version"
label="版本"
width="80"
/>
>
<template #default="{ row }">
{{ row.version || 'N/A' }}
</template>
</el-table-column>
<el-table-column prop="status" label="状态" width="100">
<template #default="{ row }">
<el-tag :type="statusTag(row.status)">
@ -652,9 +656,17 @@ const getModelTypeName = (modelType) => {
};
const getProductScopeText = (task) => {
if (task.product_scope === 'all' || !task.product_ids) {
// 使 product_scope
if (task.product_scope && task.product_scope.startsWith('指定药品')) {
return task.product_scope;
}
//
if (task.product_scope === 'all' || !task.product_ids || task.product_ids.length === 0) {
return '所有药品';
}
if (task.product_ids.length === 1 && task.product_ids[0] !== 'all') {
return `指定药品 (${task.product_ids[0]})`;
}
return `${task.product_ids.length} 种药品`;
};

View File

@ -916,6 +916,14 @@ def get_all_training_tasks():
for task_id, task_info in all_tasks.items():
task_copy = task_info.copy()
task_copy['task_id'] = task_id
# 添加药品范围描述
product_id = task_copy.get('product_id')
if product_id and product_id not in ['all', 'unknown']:
task_copy['product_scope'] = f"指定药品 ({product_id})"
else:
task_copy['product_scope'] = "所有药品"
tasks_with_id.append(task_copy)
# 按开始时间降序排序,最新的任务在前面
@ -968,348 +976,76 @@ def get_all_training_tasks():
})
def start_training():
"""
启动模型训练
---
post:
...
启动模型训练 - 已重构
"""
def _prepare_training_args(data):
"""从请求数据中提取并验证训练参数"""
training_scope = data.get('training_scope', 'all_stores_all_products')
model_type = data.get('model_type')
epochs = data.get('epochs', 50)
aggregation_method = data.get('aggregation_method', 'sum')
if not model_type:
return None, jsonify({'error': '缺少model_type参数'}), 400
valid_model_types = ['mlstm', 'kan', 'optimized_kan', 'transformer', 'tcn']
if model_type not in valid_model_types:
return None, jsonify({'error': '无效的模型类型'}), 400
# 直接从请求中获取,不设置默认值,以便进行更严格的校验
product_ids = data.get('product_ids')
store_ids = data.get('store_ids')
args = {
'model_type': model_type,
'epochs': epochs,
'training_scope': training_scope,
'aggregation_method': aggregation_method,
'product_id': data.get('product_id'),
'store_id': data.get('store_id'),
'product_ids': product_ids or [], # 确保后续代码不会因None而出错
'store_ids': store_ids or [],
'product_scope': data.get('product_scope', 'all'),
'training_mode': data.get('training_mode', 'product')
}
# 根据新的 scope 规则进行严格校验
if training_scope == 'selected_stores' and not store_ids:
return None, jsonify({'error': "当 training_scope 为 'selected_stores' 时, 必须提供 store_ids 列表。"}), 400
if training_scope == 'selected_products' and not product_ids:
return None, jsonify({'error': "当 training_scope 为 'selected_products' 时, 必须提供 product_ids 列表。"}), 400
if training_scope == 'custom' and (not store_ids or not product_ids):
return None, jsonify({'error': "当 training_scope 为 'custom' 时, 必须同时提供 store_ids 和 product_ids 列表。"}), 400
return args, None, None
data = request.get_json()
# 新增训练模式参数
training_mode = data.get('training_mode', 'product') # 'product', 'store', 'global'
# 通用参数
model_type = data.get('model_type')
epochs = data.get('epochs', 50)
# 根据训练模式获取不同的参数
product_id = data.get('product_id')
store_id = data.get('store_id')
# 新增的参数
product_ids = data.get('product_ids', [])
store_ids = data.get('store_ids', [])
product_scope = data.get('product_scope', 'all')
training_scope = data.get('training_scope', 'all_stores_all_products')
aggregation_method = data.get('aggregation_method', 'sum')
if not data:
return jsonify({'error': '无效的请求体需要JSON格式的数据'}), 400
if not model_type:
return jsonify({'error': '缺少model_type参数'}), 400
training_args, error_response, status_code = _prepare_training_args(data)
# 根据训练模式验证必需参数
if training_mode == 'product' and not product_id:
return jsonify({'error': '按药品训练模式需要product_id参数'}), 400
elif training_mode == 'store' and not store_id:
return jsonify({'error': '按店铺训练模式需要store_id参数'}), 400
elif training_mode == 'global':
# 全局模式不需要特定的product_id或store_id
pass
if error_response:
return error_response, status_code
# 检查模型类型是否有效
valid_model_types = ['mlstm', 'kan', 'optimized_kan', 'transformer', 'tcn']
if model_type not in valid_model_types:
return jsonify({'error': '无效的模型类型'}), 400
# 使用新的训练进程管理器提交任务
try:
task_id = training_manager.submit_task(
product_id=product_id or "unknown",
model_type=model_type,
training_mode=training_mode,
store_id=store_id,
epochs=epochs
)
task_id = training_manager.submit_task(**training_args)
logger.info(f"🚀 训练任务已提交到进程管理器: {task_id[:8]}")
return jsonify({
'message': '模型训练已开始(使用独立进程)',
'task_id': task_id,
'training_mode': training_mode,
'model_type': model_type,
'product_id': product_id,
'epochs': epochs
'training_scope': training_args['training_scope'],
'model_type': training_args['model_type'],
'epochs': training_args['epochs']
})
except Exception as e:
logger.error(f"❌ 提交训练任务失败: {str(e)}")
logger.error(traceback.format_exc())
return jsonify({'error': f'启动训练任务失败: {str(e)}'}), 500
# 旧的训练逻辑已被现代化进程管理器替代
global training_tasks
# 创建线程安全的日志输出函数
def thread_safe_print(message, prefix=""):
"""线程安全的打印函数,支持并发训练"""
import threading
import time
thread_id = threading.current_thread().ident
timestamp = time.strftime('%H:%M:%S')
formatted_msg = f"[{timestamp}][线程{thread_id}][{task_id[:8]}]{prefix} {message}"
# 简化输出,只使用一种方式避免重复
try:
print(formatted_msg, flush=True)
sys.stdout.flush()
except Exception as e:
try:
print(f"[输出错误] {message}", flush=True)
except:
pass
# 测试输出函数
thread_safe_print("🔥🔥🔥 训练任务线程启动", "[ENTRY]")
thread_safe_print(f"📋 参数: product_id={product_id}, model_type={model_type}, epochs={epochs}", "[PARAMS]")
try:
thread_safe_print("=" * 60, "[START]")
thread_safe_print("🚀 训练任务正式开始", "[START]")
thread_safe_print(f"🧵 线程ID: {threading.current_thread().ident}", "[START]")
thread_safe_print("=" * 60, "[START]")
logger.info(f"🚀 训练任务开始: {task_id}")
# 根据训练模式生成描述信息
if training_mode == 'product':
scope_msg = f"药品 {product_id}" + (f"(店铺 {store_id})" if store_id else "(全局数据)")
elif training_mode == 'store':
scope_msg = f"店铺 {store_id}"
if kwargs.get('product_scope') == 'specific':
scope_msg += f"({len(kwargs.get('product_ids', []))} 种药品)"
else:
scope_msg += "(所有药品)"
elif training_mode == 'global':
scope_msg = f"全局模型({kwargs.get('aggregation_method', 'sum')}聚合)"
if kwargs.get('training_scope') != 'all_stores_all_products':
scope_msg += f"(自定义范围)"
else:
scope_msg = "未知模式"
thread_safe_print(f"📋 任务详情: 训练 {model_type} 模型 - {scope_msg}", "[INFO]")
thread_safe_print(f"⚙️ 配置参数: 共 {epochs} 个轮次", "[CONFIG]")
logger.info(f"📋 任务详情: 训练 {model_type} 模型 - {scope_msg}, 轮次: {epochs}")
# 根据训练模式生成版本号和模型标识
if training_mode == 'store':
scope = f"{store_id}_{product_id}"
elif training_mode == 'global':
scope = f"{product_id}" if product_id else "all"
else:
scope = f"{product_id}_all"
model_identifier = model_manager.get_model_identifier(model_type, training_mode, scope, aggregation_method)
version = model_manager.get_next_version_number(model_identifier)
thread_safe_print(f"🏷️ 版本信息: 版本号 {version}, 模型标识: {model_identifier}", "[VERSION]")
logger.info(f"🏷️ 版本信息: 版本号 {version}, 模型标识: {model_identifier}")
# 初始化训练进度管理器
progress_manager.start_training(
training_id=task_id,
product_id=product_id,
model_type=model_type,
training_mode=training_mode,
total_epochs=epochs,
total_batches=0, # 将在实际训练器中设置
batch_size=32, # 默认值,将在实际训练器中更新
total_samples=0 # 将在实际训练器中设置
)
thread_safe_print("📊 进度管理器已初始化", "[PROGRESS]")
logger.info(f"📊 进度管理器已初始化 - 任务ID: {task_id}")
# 发送训练开始的WebSocket消息
if socketio:
socketio.emit('training_update', {
'task_id': task_id,
'status': 'starting',
'message': f'开始训练 {model_type} 模型版本 {version} - {scope_msg}',
'product_id': product_id,
'store_id': store_id,
'model_type': model_type,
'version': version,
'training_mode': training_mode,
'progress': 0
}, namespace=WEBSOCKET_NAMESPACE, room=task_id)
# 根据训练模式选择不同的训练逻辑
thread_safe_print(f"🏃 开始调用训练器 - 模式: {training_mode}, 模型: {model_type}", "[TRAINER]")
logger.info(f"🏃 开始调用训练器 - 模式: {training_mode}, 模型: {model_type}")
if training_mode == 'product':
# 按药品训练 - 使用现有逻辑
if model_type == 'optimized_kan':
thread_safe_print("🧠 调用优化KAN训练器", "[KAN]")
logger.info(f"🧠 调用优化KAN训练器 - 产品: {product_id}")
metrics = predictor.train_model(
product_id=product_id,
model_type='optimized_kan',
store_id=store_id,
training_mode='product',
epochs=epochs,
socketio=socketio,
task_id=task_id,
version=version
)
else:
thread_safe_print(f"🤖 调用 {model_type.upper()} 训练器 - 产品: {product_id}", "[CALL]")
logger.info(f"🤖 调用 {model_type.upper()} 训练器 - 产品: {product_id}")
metrics = predictor.train_model(
product_id=product_id,
model_type=model_type,
store_id=store_id,
training_mode='product',
epochs=epochs,
socketio=socketio,
task_id=task_id,
version=version
)
thread_safe_print(f"✅ 训练器返回结果: {type(metrics)}", "[RESULT]")
logger.info(f"✅ 训练器返回结果: {type(metrics)}")
elif training_mode == 'store':
# 按店铺训练 - 需要新的训练逻辑
metrics = train_store_model(
store_id=store_id,
model_type=model_type,
epochs=epochs,
product_scope=kwargs.get('product_scope', 'all'),
product_ids=kwargs.get('product_ids', [])
)
elif training_mode == 'global':
# 全局训练 - 需要新的训练逻辑
metrics = train_global_model(
model_type=model_type,
epochs=epochs,
training_scope=kwargs.get('training_scope', 'all_stores_all_products'),
aggregation_method=kwargs.get('aggregation_method', 'sum'),
store_ids=kwargs.get('store_ids', []),
product_ids=kwargs.get('product_ids', [])
)
thread_safe_print(f"📈 训练完成! 结果类型: {type(metrics)}", "[COMPLETE]")
if metrics:
thread_safe_print(f"📊 训练指标: {metrics}", "[METRICS]")
else:
thread_safe_print("⚠️ 训练指标为空", "[WARNING]")
logger.info(f"📈 训练完成 - 结果类型: {type(metrics)}, 内容: {metrics}")
# 更新模型路径使用版本管理
model_path = model_manager.get_model_version_path(model_type, training_mode, scope, version, aggregation_method)
thread_safe_print(f"💾 模型保存路径: {model_path}", "[SAVE]")
logger.info(f"💾 模型保存路径: {model_path}")
# 更新任务状态
training_tasks[task_id]['status'] = 'completed'
training_tasks[task_id]['metrics'] = metrics
training_tasks[task_id]['model_path'] = model_path
training_tasks[task_id]['version'] = version
print(f"✔️ 任务状态更新: 已完成, 版本: {version}", flush=True)
logger.info(f"✔️ 任务状态更新: 已完成, 版本: {version}, 任务ID: {task_id}")
# 保存模型版本信息到数据库
try:
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('''
INSERT INTO model_versions (product_id, model_type, version, file_path, created_at, metrics)
VALUES (?, ?, ?, ?, ?, ?)
''', (
model_identifier, model_type, f'v{version}', model_path,
datetime.now().isoformat(), json.dumps(metrics)
))
conn.commit()
conn.close()
except Exception as db_error:
thread_safe_print(f"数据库保存失败: {db_error}", "[DB_ERROR]")
# 完成训练进度管理器
progress_manager.finish_training(success=True)
# 发送训练完成的WebSocket消息
if socketio:
print(f"📡 发送WebSocket完成消息", flush=True)
logger.info(f"📡 发送WebSocket完成消息 - 任务ID: {task_id}")
socketio.emit('training_update', {
'task_id': task_id,
'status': 'completed',
'message': f'模型 {model_type} 版本 {version} 训练完成',
'product_id': product_id,
'model_type': model_type,
'version': version,
'progress': 100,
'metrics': metrics,
'model_path': model_path
}, namespace=WEBSOCKET_NAMESPACE, room=task_id)
print(f"SUCCESS 任务 {task_id}: 训练完成!评估指标: {metrics}", flush=True)
except Exception as e:
import traceback
print(f"ERROR 任务 {task_id}: 训练过程中发生异常!", flush=True)
traceback.print_exc()
error_msg = str(e)
print(f"FAILED 任务 {task_id}: 训练失败。错误: {error_msg}", flush=True)
training_tasks[task_id]['status'] = 'failed'
training_tasks[task_id]['error'] = error_msg
# 完成训练进度管理器(失败)
progress_manager.finish_training(success=False, error_message=error_msg)
# 发送训练失败的WebSocket消息
if socketio:
socketio.emit('training_update', {
'task_id': task_id,
'status': 'failed',
'message': f'模型 {model_type} 训练失败: {error_msg}',
'product_id': product_id,
'model_type': model_type,
'error': error_msg
}, namespace=WEBSOCKET_NAMESPACE, room=task_id)
# 构建训练任务参数
training_kwargs = {
'product_scope': product_scope,
'product_ids': product_ids,
'training_scope': training_scope,
'aggregation_method': aggregation_method,
'store_ids': store_ids
}
print(f"\n🚀🚀🚀 THREAD START: 准备启动训练线程 task_id={task_id} 🚀🚀🚀", flush=True)
print(f"📋 线程参数: training_mode={training_mode}, product_id={product_id}, model_type={model_type}", flush=True)
sys.stdout.flush()
thread = threading.Thread(
target=train_task,
args=(training_mode, product_id, store_id, epochs, model_type),
kwargs=training_kwargs
)
print(f"🧵 线程已创建,准备启动...", flush=True)
thread.start()
print(f"✅ 线程已启动!", flush=True)
sys.stdout.flush()
training_tasks[task_id] = {
'status': 'running',
'product_id': product_id,
'model_type': model_type,
'store_id': store_id,
'training_mode': training_mode,
'product_scope': product_scope,
'product_ids': product_ids,
'training_scope': training_scope,
'aggregation_method': aggregation_method,
'store_ids': store_ids,
'start_time': datetime.now().isoformat(),
'metrics': None,
'error': None,
'model_path': None
}
print(f"✅ API返回响应: 训练任务 {task_id} 已启动", flush=True)
return jsonify({'message': '模型训练已开始', 'task_id': task_id})
@app.route('/api/test-thread-output', methods=['POST'])
def test_thread_output():
@ -2302,25 +2038,20 @@ def list_models():
# 格式化响应数据
formatted_models = []
for model in models:
# 生成唯一且有意义的model_id
model_id = model.get('filename', '').replace('.pth', '')
if not model_id:
# 备用方案基于模型信息生成ID
product_id = model.get('product_id', 'unknown')
model_type = model.get('model_type', 'unknown')
version = model.get('version', 'v1')
training_mode = model.get('training_mode', 'product')
store_id = model.get('store_id')
if training_mode == 'store' and store_id:
model_id = f"{model_type}_store_{store_id}_{product_id}_{version}"
elif training_mode == 'global':
aggregation_method = model.get('aggregation_method', 'mean')
model_id = f"{model_type}_global_{product_id}_{aggregation_method}_{version}"
else:
model_id = f"{model_type}_product_{product_id}_{version}"
# 从 model_manager 获取的信息已经很全面
# 我们只需要确保前端需要的字段都存在
# The new list_models returns a dictionary with all necessary info
# 药品范围
product_id = model.get('product_id')
if product_id and product_id not in ['all', 'unknown']:
model['product_scope'] = f"指定药品 ({product_id})"
else:
model['product_scope'] = "所有药品"
# 确保版本号存在
if 'version' not in model:
model['version'] = 'v1' # 默认值
formatted_models.append(model)
logger.info(f"[API] 成功获取 {len(formatted_models)} 个模型")
@ -3693,9 +3424,25 @@ def get_model_versions_api(product_id, model_type):
def get_store_model_versions_api(store_id, model_type):
"""获取店铺模型版本列表API"""
try:
model_identifier = f"store_{store_id}"
versions = get_model_versions(model_identifier, model_type)
latest_version = get_latest_model_version(model_identifier, model_type)
all_models_data = model_manager.list_models()
all_models = all_models_data.get('models', [])
versions = []
for model in all_models:
is_store_model = model.get('training_mode') == 'store'
# 检查scope是否以 "store_id_" 开头,以匹配 "store_id_product_id" 或 "store_id_all"
is_correct_scope = model.get('scope', '').startswith(f"{store_id}_")
is_correct_type = model.get('model_type') == model_type
if is_store_model and is_correct_scope and is_correct_type:
version_str = model.get('version')
if version_str:
versions.append(version_str)
# 对版本进行排序, e.g., 'v10', 'v2', 'v1' -> 'v10', 'v2', 'v1'
versions.sort(key=lambda v: int(v.replace('v', '')), reverse=True)
latest_version = versions[0] if versions else None
return jsonify({
"status": "success",
@ -3714,9 +3461,23 @@ def get_store_model_versions_api(store_id, model_type):
def get_global_model_versions_api(model_type):
"""获取全局模型版本列表API"""
try:
model_identifier = "global"
versions = get_model_versions(model_identifier, model_type)
latest_version = get_latest_model_version(model_identifier, model_type)
all_models_data = model_manager.list_models()
all_models = all_models_data.get('models', [])
versions = []
for model in all_models:
is_global_model = model.get('training_mode') == 'global'
is_correct_type = model.get('model_type') == model_type
if is_global_model and is_correct_type:
version_str = model.get('version')
if version_str:
versions.append(version_str)
# 对版本进行排序
versions.sort(key=lambda v: int(v.replace('v', '')), reverse=True)
latest_version = versions[0] if versions else None
return jsonify({
"status": "success",

View File

@ -47,10 +47,83 @@ class PharmacyPredictor:
print(f"加载数据失败: {e}")
self.data = None
def _prepare_global_params(self, **kwargs):
"""'global' (all_stores_all_products) 模式准备参数"""
return {
'final_training_mode': 'global',
'agg_store_id': None,
'agg_product_id': None,
'path_store_id': 'all',
'path_product_id': 'all',
}
def _prepare_stores_params(self, **kwargs):
"""'stores' (selected_stores) 模式准备参数并校验"""
store_ids_list = kwargs.get('store_ids')
if not store_ids_list:
raise ValueError("进行 'selected_stores' 范围训练时,必须提供 store_ids 列表。")
return {
'final_training_mode': 'stores',
'agg_store_id': store_ids_list,
'agg_product_id': None,
'path_store_id': store_ids_list[0],
'path_product_id': 'all',
}
def _prepare_products_params(self, **kwargs):
"""'products' (selected_products) 模式准备参数并校验"""
product_ids_list = kwargs.get('product_ids')
if not product_ids_list:
raise ValueError("进行 'selected_products' 范围训练时,必须提供 product_ids 列表。")
return {
'final_training_mode': 'products',
'agg_store_id': None,
'agg_product_id': product_ids_list,
'path_store_id': 'all',
'path_product_id': product_ids_list[0],
}
def _prepare_custom_params(self, **kwargs):
"""'custom' 模式准备参数并校验"""
store_ids_list = kwargs.get('store_ids')
product_ids_list = kwargs.get('product_ids')
if not store_ids_list or not product_ids_list:
raise ValueError("进行 'custom' 范围训练时,必须同时提供 store_ids 和 product_ids 列表。")
return {
'final_training_mode': 'custom',
'agg_store_id': store_ids_list,
'agg_product_id': product_ids_list,
'path_store_id': store_ids_list[0],
'path_product_id': product_ids_list[0],
}
def _prepare_training_params(self, training_scope, product_id, store_id, **kwargs):
"""
参数准备分发器根据 training_scope 调用相应的处理函数
"""
scope_handlers = {
'all_stores_all_products': self._prepare_global_params,
'selected_stores': self._prepare_stores_params,
'selected_products': self._prepare_products_params,
'custom': self._prepare_custom_params,
}
handler = scope_handlers.get(training_scope)
if not handler:
raise ValueError(f"不支持的训练范围: '{training_scope}'")
# 将所有相关参数合并到一个字典中,然后传递给处理函数
all_params = kwargs.copy()
all_params['training_scope'] = training_scope
all_params['product_id'] = product_id
all_params['store_id'] = store_id
return handler(**all_params)
def train_model(self, product_id, model_type='transformer', epochs=100,
learning_rate=0.001, use_optimized=False,
store_id=None, training_mode='product', aggregation_method='sum',
socketio=None, task_id=None, progress_callback=None, patience=10):
product_scope='all', product_ids=None, store_ids=None,
socketio=None, task_id=None, progress_callback=None, patience=10, **kwargs):
"""
训练预测模型 - 完全适配新的训练器接口
"""
@ -66,26 +139,44 @@ class PharmacyPredictor:
log_message("没有可用的数据,请先加载或生成数据", 'error')
return None
# --- 数据准备 ---
try:
if training_mode == 'store':
product_data = get_store_product_sales_data(store_id, product_id, self.data_path)
log_message(f"按店铺-产品训练: 店铺 {store_id}, 产品 {product_id}, 数据量: {len(product_data)}")
elif training_mode == 'global':
product_data = aggregate_multi_store_data(product_id, aggregation_method, self.data_path)
log_message(f"全局训练模式: 产品 {product_id}, 聚合方法 {aggregation_method}, 数据<E695B0><E68DAE><EFBFBD>: {len(product_data)}")
else: # 'product'
product_data = self.data[self.data['product_id'] == product_id].copy()
log_message(f"按产品训练模式: 产品 {product_id}, 数据量: {len(product_data)}")
# 从kwargs复制一份避免修改原始字典
call_kwargs = kwargs.copy()
training_scope = call_kwargs.pop('training_scope', None)
# The dispatcher will pop the legacy store_id and product_id
params = self._prepare_training_params(
training_scope=training_scope,
store_id=store_id,
product_id=product_id,
product_ids=product_ids,
store_ids=store_ids,
**call_kwargs
)
product_data = aggregate_multi_store_data(
store_id=params['agg_store_id'],
product_id=params['agg_product_id'],
aggregation_method=aggregation_method,
file_path=self.data_path
)
if product_data is None or product_data.empty:
raise ValueError(f"聚合后数据为空,无法继续训练。模式: {params['final_training_mode']}")
except ValueError as e:
log_message(f"参数校验或数据准备失败: {e}", 'error')
return None
except Exception as e:
log_message(f"数据准备失败: {e}", 'error')
import traceback
log_message(f"数据准备过程中发生未知错误: {e}", 'error')
log_message(traceback.format_exc(), 'error')
return None
if product_data.empty:
log_message(f"找不到产品 {product_id} 的数据", 'error')
return None
# --- 训练器选择与参数准备 ---
trainers = {
'transformer': train_product_model_with_transformer,
'mlstm': train_product_model_with_mlstm,
@ -100,13 +191,13 @@ class PharmacyPredictor:
trainer_func = trainers[model_type]
# 统一所有训练器的参数
trainer_args = {
"product_id": product_id,
"product_id": params['path_product_id'],
"product_df": product_data,
"store_id": store_id,
"training_mode": training_mode,
"store_id": params['path_store_id'],
"training_mode": params['final_training_mode'],
"aggregation_method": aggregation_method,
"scope": kwargs.get('training_scope'),
"epochs": epochs,
"socketio": socketio,
"task_id": task_id,
@ -115,11 +206,9 @@ class PharmacyPredictor:
"learning_rate": learning_rate
}
# 为 KAN 模型添加特殊参数
if 'kan' in model_type:
trainer_args['use_optimized'] = (model_type == 'optimized_kan')
# --- 调用训练器 ---
try:
log_message(f"🤖 开始调用 {model_type} 训练器")
@ -128,14 +217,16 @@ class PharmacyPredictor:
log_message(f"{model_type} 训练器成功返回", 'success')
if metrics:
relative_model_path = os.path.relpath(model_version_path, os.getcwd())
metrics.update({
'model_type': model_type,
'version': version,
'model_path': model_version_path,
'training_mode': training_mode,
'store_id': store_id,
'product_id': product_id,
'aggregation_method': aggregation_method if training_mode == 'global' else None
'model_path': relative_model_path.replace('\\', '/'),
'training_mode': params['final_training_mode'],
'store_id': params['path_store_id'],
'product_id': params['path_product_id'],
'aggregation_method': aggregation_method if params['final_training_mode'] == 'global' else None
})
log_message(f"📈 最终返回的metrics: {metrics}", 'success')
return metrics

View File

@ -21,14 +21,32 @@ from utils.data_utils import create_dataset, PharmacyDataset
from analysis.metrics import evaluate_model
from core.config import DEVICE, LOOK_BACK, FORECAST_HORIZON
from utils.model_manager import model_manager
from typing import Any
def convert_numpy_types(obj: Any) -> Any:
"""
递归地将字典或列表中的Numpy数值类型转换为Python原生类型
"""
if isinstance(obj, dict):
return {k: convert_numpy_types(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [convert_numpy_types(elem) for elem in obj]
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
return obj
def train_product_model_with_kan(
product_id,
product_id,
product_df=None,
store_id=None,
training_mode='product',
aggregation_method='sum',
epochs=50,
store_id=None,
training_mode='product',
aggregation_method='sum',
scope=None,
epochs=50,
use_optimized=False,
socketio=None,
task_id=None,
@ -70,12 +88,17 @@ def train_product_model_with_kan(
# 1. 确定模型标识符和版本
model_type = 'optimized_kan' if use_optimized else 'kan'
if training_mode == 'store':
scope = f"{store_id}_{product_id}"
elif training_mode == 'global':
scope = f"{product_id}" if product_id else "all"
else: # 'product' mode
scope = f"{product_id}_all"
# 直接使用从 predictor 传递过来的、已经构建好的 scope
if scope is None:
# 作为后备如果scope未提供则根据旧逻辑构建不推荐
if training_mode == 'store':
current_product_id = product_id if product_id and product_id not in ['unknown', 'all'] else 'all'
scope = f"{store_id}_{current_product_id}"
elif training_mode == 'product':
scope = f"{product_id}_{store_id or 'all'}"
elif training_mode == 'global':
scope = product_id if product_id else "all"
emit_progress(f"警告: Scope未由调用方提供已自动构建为 '{scope}'", 'warning')
model_identifier = model_manager.get_model_identifier(model_type, training_mode, scope, aggregation_method)
version = model_manager.get_next_version_number(model_identifier)
@ -83,7 +106,15 @@ def train_product_model_with_kan(
emit_progress(f"开始训练 {model_type} 模型 v{version}")
# 2. 获取模型版本路径
model_version_path = model_manager.get_model_version_path(model_type, training_mode, scope, version, aggregation_method)
model_version_path = model_manager.get_model_version_path(
model_type=model_type,
training_mode=training_mode,
scope=scope,
version=version,
aggregation_method=aggregation_method,
product_id=product_id,
store_id=store_id
)
emit_progress(f"模型将保存到: {model_version_path}")
# 3. 数据加载和预处理
@ -106,8 +137,11 @@ def train_product_model_with_kan(
training_scope = f"店铺 {store_id}"
elif training_mode == 'global':
training_scope = f"全局聚合({aggregation_method})"
else:
training_scope = "所有店铺"
else: # 主要对应 product 模式
if store_id:
training_scope = f"店铺 {store_id}"
else:
training_scope = "所有店铺"
min_required_samples = LOOK_BACK + FORECAST_HORIZON
if len(product_df) < min_required_samples:
@ -244,6 +278,9 @@ def train_product_model_with_kan(
metrics = evaluate_model(test_true_inv.flatten(), test_pred_inv.flatten())
metrics['training_time'] = training_time
# 解决 'Object of type float32 is not JSON serializable' 错误
metrics = convert_numpy_types(metrics)
emit_progress(f"模型评估完成 - RMSE: {metrics['rmse']:.4f}, R²: {metrics['r2']:.4f}")
# 7. 保存最终模型和元数据
@ -258,6 +295,7 @@ def train_product_model_with_kan(
'product_id': product_id, 'product_name': product_name, 'model_type': model_type,
'version': f'v{version}', 'training_mode': training_mode, 'scope': scope,
'aggregation_method': aggregation_method, 'training_scope_description': training_scope,
'product_scope': '所有药品' if product_id == 'all' else product_name,
'timestamp': datetime.now().isoformat(), 'metrics': metrics,
'config': {
'input_dim': input_dim, 'output_dim': output_dim,

View File

@ -22,6 +22,23 @@ from core.config import (
)
from utils.training_progress import progress_manager
from utils.model_manager import model_manager
from typing import Any
def convert_numpy_types(obj: Any) -> Any:
"""
递归地将字典或列表中的Numpy数值类型转换为Python原生类型
"""
if isinstance(obj, dict):
return {k: convert_numpy_types(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [convert_numpy_types(elem) for elem in obj]
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
return obj
def train_product_model_with_mlstm(
product_id,
@ -29,6 +46,7 @@ def train_product_model_with_mlstm(
store_id=None,
training_mode='product',
aggregation_method='sum',
scope=None,
epochs=50,
socketio=None,
task_id=None,
@ -54,7 +72,7 @@ def train_product_model_with_mlstm(
progress_callback: 进度回调函数用于多进程训练
"""
# 创建WebSocket进度反馈函数支持多进程 """
# 创建WebSocket进度反馈函数支持多进程
def emit_progress(message, progress=None, metrics=None):
"""发送训练进度到前端"""
@ -89,12 +107,18 @@ def train_product_model_with_mlstm(
# 1. 确定模型标识符和版本
model_type = 'mlstm'
if training_mode == 'store':
scope = f"{store_id}_{product_id}"
elif training_mode == 'global':
scope = f"{product_id}" if product_id else "all"
else:
scope = f"{product_id}_all"
# 直接使用从 predictor 传递过来的、已经构建好的 scope
if scope is None:
# 作为后备如果scope未提供则根据旧逻辑构建不推荐
if training_mode == 'store':
current_product_id = product_id if product_id and product_id not in ['unknown', 'all'] else 'all'
scope = f"{store_id}_{current_product_id}"
elif training_mode == 'product':
scope = f"{product_id}_{store_id or 'all'}"
elif training_mode == 'global':
scope = product_id if product_id else "all"
emit_progress(f"警告: Scope未由调用方提供已自动构建为 '{scope}'", 'warning')
model_identifier = model_manager.get_model_identifier(model_type, training_mode, scope, aggregation_method)
version = model_manager.get_next_version_number(model_identifier)
@ -102,15 +126,25 @@ def train_product_model_with_mlstm(
emit_progress(f"开始训练 mLSTM 模型 v{version}")
# 2. 获取模型版本路径
model_version_path = model_manager.get_model_version_path(model_type, training_mode, scope, version, aggregation_method)
model_version_path = model_manager.get_model_version_path(
model_type=model_type,
training_mode=training_mode,
version=version,
aggregation_method=aggregation_method,
product_id=product_id,
store_id=store_id
)
emit_progress(f"模型将保存到: {model_version_path}")
if training_mode == 'store' and store_id:
training_scope = f"店铺 {store_id}"
elif training_mode == 'global':
training_scope = f"全局聚合({aggregation_method})"
else:
training_scope = "所有店铺"
else: # 主要对应 product 模式
if store_id:
training_scope = f"店铺 {store_id}"
else:
training_scope = "所有店铺"
min_required_samples = LOOK_BACK + FORECAST_HORIZON
if len(product_df) < min_required_samples:
@ -121,16 +155,16 @@ def train_product_model_with_mlstm(
product_name = product_df['product_name'].iloc[0]
print(f"[mLSTM] 使用mLSTM模型训练产品 '{product_name}' (ID: {product_id}) 的销售预测模型", flush=True)
print(f"[mLSTM] 使用mLSTM模型训练 '{product_name}' (ID: {product_id}) 的销售预测模型", flush=True)
print(f"[mLSTM] 训练范围: {training_scope}", flush=True)
print(f"[mLSTM] 版本: v{version}", flush=True)
print(f"[mLSTM] 使用设备: {DEVICE}", flush=True)
print(f"[mLSTM] 数据量: {len(product_df)} 条记录", flush=True)
emit_progress(f"训练产品: {product_name} (ID: {product_id}) - {training_scope}")
emit_progress(f"训练产品: {product_name} (ID: {product_id}) - {training_scope}")
# 创建特征和目标变量
features = ['sales', 'weekday', 'month', 'is_holiday', 'is_weekend', 'is_promotion', 'temperature']
print(f"[mLSTM] 开始数据预处理,特征: {features}", flush=True)
# 预处理数据
@ -144,7 +178,7 @@ def train_product_model_with_mlstm(
scaler_y = MinMaxScaler(feature_range=(0, 1))
X_scaled = scaler_X.fit_transform(X)
y_scaled = scaler_y.fit_transform(y)
print(f"[mLSTM] 数据归一化完成", flush=True)
print(f"[mLSTM] 数据归一化完成", flush=True)
train_size = int(len(X_scaled) * 0.8)
X_train, X_test = X_scaled[:train_size], X_scaled[train_size:]
y_train, y_test = y_scaled[:train_size], y_scaled[train_size:]
@ -155,11 +189,9 @@ def train_product_model_with_mlstm(
train_loader = DataLoader(PharmacyDataset(torch.Tensor(trainX), torch.Tensor(trainY)), batch_size=32, shuffle=True)
test_loader = DataLoader(PharmacyDataset(torch.Tensor(testX), torch.Tensor(testY)), batch_size=32, shuffle=False)
print(f"[mLSTM] 数据加载器创建完成 - 批次数: {total_batches}, 样本数: {total_samples}", flush=True)
total_batches = len(train_loader)
total_samples = len(train_loader.dataset)
print(f"[mLSTM] 数据加载器创建完成 - 批次数: {total_batches}, 样本数: {total_samples}", flush=True)
emit_progress(f"数据加载器准备完成 - 批次数: {total_batches}, 样本数: {total_samples}")
input_dim = X_train.shape[1]
output_dim = FORECAST_HORIZON
@ -170,7 +202,7 @@ def train_product_model_with_mlstm(
dense_dim=dense_dim, num_heads=num_heads, dropout_rate=dropout_rate,
num_blocks=num_blocks, output_sequence_length=output_dim
).to(DEVICE)
print(f"[mLSTM] 模型创建完成", flush=True)
print(f"[mLSTM] 模型创建完成", flush=True)
emit_progress("mLSTM模型初始化完成")
if continue_training:
emit_progress("继续训练模式启动,但当前重构版本将从头开始。")
@ -186,11 +218,11 @@ def train_product_model_with_mlstm(
checkpoint_interval = max(1, epochs // 10)
best_loss = float('inf')
epochs_no_improve = 0
emit_progress(f"开始训练 - 总epoch: {epochs}, 检查点间隔: {checkpoint_interval}, 耐心值: {patience}")
emit_progress(f"开始训练 - 总epoch: {epochs}, 检查点间隔: {checkpoint_interval}, 耐心值: {patience}")
for epoch in range(epochs):
emit_progress(f"开始训练 Epoch {epoch+1}/{epochs}")
emit_progress(f"开始训练 Epoch {epoch+1}/{epochs}")
model.train()
epoch_loss = 0
@ -204,10 +236,10 @@ def train_product_model_with_mlstm(
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=clip_norm)
optimizer.step()
epoch_loss += loss.item()
# 计算训练损失
# 计算训练损失
train_loss = epoch_loss / len(train_loader)
train_losses.append(train_loss)
# 在测试集上评估
# 在测试集上评估
model.eval()
test_loss = 0
with torch.no_grad():
@ -219,13 +251,14 @@ def train_product_model_with_mlstm(
test_loss /= len(test_loader)
test_losses.append(test_loss)
# 更新学习率
# 更新学习率
scheduler.step(test_loss)
emit_progress(f"Epoch {epoch+1}/{epochs} 完成 - Train Loss: {train_loss:.4f}, Test Loss: {test_loss:.4f}",
progress=10 + ((epoch + 1) / epochs) * 85)
# 定期保存检查点
# 3. 保存检查点 checkpoint_data = {
# 3. 保存检查点
checkpoint_data = {
'epoch': epoch + 1,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
@ -265,7 +298,10 @@ def train_product_model_with_mlstm(
metrics = evaluate_model(scaler_y.inverse_transform(testY), scaler_y.inverse_transform(test_pred))
metrics['training_time'] = training_time
# 打印评估指标
# 解决 'Object of type float32 is not JSON serializable' 错误
metrics = convert_numpy_types(metrics)
# 打印评估指标
print("\n模型评估指标:")
print(f"MSE: {metrics['mse']:.4f}")
print(f"RMSE: {metrics['rmse']:.4f}")
@ -286,6 +322,7 @@ def train_product_model_with_mlstm(
'product_id': product_id, 'product_name': product_name, 'model_type': model_type,
'version': f'v{version}', 'training_mode': training_mode, 'scope': scope,
'aggregation_method': aggregation_method, 'training_scope_description': training_scope,
'product_scope': '所有药品' if product_id == 'all' else product_name,
'timestamp': datetime.now().isoformat(), 'metrics': metrics,
'config': {
'input_dim': input_dim, 'output_dim': output_dim, 'hidden_size': hidden_size,

View File

@ -19,6 +19,23 @@ from utils.data_utils import create_dataset, PharmacyDataset
from analysis.metrics import evaluate_model
from core.config import DEVICE, LOOK_BACK, FORECAST_HORIZON
from utils.model_manager import model_manager
from typing import Any
def convert_numpy_types(obj: Any) -> Any:
"""
递归地将字典或列表中的Numpy数值类型转换为Python原生类型
"""
if isinstance(obj, dict):
return {k: convert_numpy_types(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [convert_numpy_types(elem) for elem in obj]
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
return obj
def train_product_model_with_tcn(
product_id,
@ -26,6 +43,7 @@ def train_product_model_with_tcn(
store_id=None,
training_mode='product',
aggregation_method='sum',
scope=None,
epochs=50,
socketio=None,
task_id=None,
@ -67,12 +85,17 @@ def train_product_model_with_tcn(
# 1. 确定模型标识符和版本
model_type = 'tcn'
if training_mode == 'store':
scope = f"{store_id}_{product_id}"
elif training_mode == 'global':
scope = f"{product_id}" if product_id else "all"
else: # 'product' mode
scope = f"{product_id}_all"
# 直接使用从 predictor 传递过来的、已经构建好的 scope
if scope is None:
# 作为后备如果scope未提供则根据旧逻辑构建不推荐
if training_mode == 'store':
current_product_id = product_id if product_id and product_id not in ['unknown', 'all'] else 'all'
scope = f"{store_id}_{current_product_id}"
elif training_mode == 'product':
scope = f"{product_id}_{store_id or 'all'}"
elif training_mode == 'global':
scope = product_id if product_id else "all"
emit_progress(f"警告: Scope未由调用方提供已自动构建为 '{scope}'", 'warning')
model_identifier = model_manager.get_model_identifier(model_type, training_mode, scope, aggregation_method)
version = model_manager.get_next_version_number(model_identifier)
@ -80,7 +103,15 @@ def train_product_model_with_tcn(
emit_progress(f"开始训练 {model_type} 模型 v{version}")
# 2. 获取模型版本路径
model_version_path = model_manager.get_model_version_path(model_type, training_mode, scope, version, aggregation_method)
model_version_path = model_manager.get_model_version_path(
model_type=model_type,
training_mode=training_mode,
scope=scope,
version=version,
aggregation_method=aggregation_method,
product_id=product_id,
store_id=store_id
)
emit_progress(f"模型将保存到: {model_version_path}")
# 3. 数据加载和预处理
@ -102,8 +133,11 @@ def train_product_model_with_tcn(
training_scope = f"店铺 {store_id}"
elif training_mode == 'global':
training_scope = f"全局聚合({aggregation_method})"
else:
training_scope = "所有店铺"
else: # 主要对应 product 模式
if store_id:
training_scope = f"店铺 {store_id}"
else:
training_scope = "所有店铺"
min_required_samples = LOOK_BACK + FORECAST_HORIZON
if len(product_df) < min_required_samples:
@ -239,6 +273,9 @@ def train_product_model_with_tcn(
metrics = evaluate_model(test_true_inv.flatten(), test_pred_inv.flatten())
metrics['training_time'] = training_time
# 解决 'Object of type float32 is not JSON serializable' 错误
metrics = convert_numpy_types(metrics)
emit_progress(f"模型评估完成 - RMSE: {metrics['rmse']:.4f}, R²: {metrics['r2']:.4f}")
# 7. 保存最终模型和元数据
@ -253,6 +290,7 @@ def train_product_model_with_tcn(
'product_id': product_id, 'product_name': product_name, 'model_type': model_type,
'version': f'v{version}', 'training_mode': training_mode, 'scope': scope,
'aggregation_method': aggregation_method, 'training_scope_description': training_scope,
'product_scope': '所有药品' if product_id == 'all' else product_name,
'timestamp': datetime.now().isoformat(), 'metrics': metrics,
'config': {
'input_dim': input_dim, 'output_dim': output_dim, 'hidden_size': hidden_size,

View File

@ -19,6 +19,23 @@ from utils.data_utils import create_dataset, PharmacyDataset
from analysis.metrics import evaluate_model
from core.config import DEVICE, LOOK_BACK, FORECAST_HORIZON
from utils.model_manager import model_manager
from typing import Any
def convert_numpy_types(obj: Any) -> Any:
"""
递归地将字典或列表中的Numpy数值类型转换为Python原生类型
"""
if isinstance(obj, dict):
return {k: convert_numpy_types(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [convert_numpy_types(elem) for elem in obj]
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
return obj
def train_product_model_with_transformer(
product_id,
@ -26,6 +43,7 @@ def train_product_model_with_transformer(
store_id=None,
training_mode='product',
aggregation_method='sum',
scope=None,
epochs=50,
socketio=None,
task_id=None,
@ -68,12 +86,17 @@ def train_product_model_with_transformer(
# 1. 确定模型标识符和版本
model_type = 'transformer'
if training_mode == 'store':
scope = f"{store_id}_{product_id}"
elif training_mode == 'global':
scope = f"{product_id}" if product_id else "all"
else: # 'product' mode
scope = f"{product_id}_all"
# 直接使用从 predictor 传递过来的、已经构建好的 scope
if scope is None:
# 作为后备如果scope未提供则根据旧逻辑构建不推荐
if training_mode == 'store':
current_product_id = product_id if product_id and product_id not in ['unknown', 'all'] else 'all'
scope = f"{store_id}_{current_product_id}"
elif training_mode == 'product':
scope = f"{product_id}_{store_id or 'all'}"
elif training_mode == 'global':
scope = product_id if product_id else "all"
emit_progress(f"警告: Scope未由调用方提供已自动构建为 '{scope}'", 'warning')
model_identifier = model_manager.get_model_identifier(model_type, training_mode, scope, aggregation_method)
version = model_manager.get_next_version_number(model_identifier)
@ -81,7 +104,15 @@ def train_product_model_with_transformer(
emit_progress(f"开始训练 {model_type} 模型 v{version}")
# 2. 获取模型版本路径
model_version_path = model_manager.get_model_version_path(model_type, training_mode, scope, version, aggregation_method)
model_version_path = model_manager.get_model_version_path(
model_type=model_type,
training_mode=training_mode,
scope=scope,
version=version,
aggregation_method=aggregation_method,
product_id=product_id,
store_id=store_id
)
emit_progress(f"模型将保存到: {model_version_path}")
# 3. 数据加载和预处理
@ -103,8 +134,11 @@ def train_product_model_with_transformer(
training_scope = f"店铺 {store_id}"
elif training_mode == 'global':
training_scope = f"全局聚合({aggregation_method})"
else:
training_scope = "所有店铺"
else: # 主要对应 product 模式
if store_id:
training_scope = f"店铺 {store_id}"
else:
training_scope = "所有店铺"
min_required_samples = LOOK_BACK + FORECAST_HORIZON
if len(product_df) < min_required_samples:
@ -246,6 +280,9 @@ def train_product_model_with_transformer(
metrics = evaluate_model(test_true_inv.flatten(), test_pred_inv.flatten())
metrics['training_time'] = training_time
# 解决 'Object of type float32 is not JSON serializable' 错误
metrics = convert_numpy_types(metrics)
emit_progress(f"模型评估完成 - RMSE: {metrics['rmse']:.4f}, R²: {metrics['r2']:.4f}")
# 7. 保存最终模型和元数据
@ -260,6 +297,7 @@ def train_product_model_with_transformer(
'product_id': product_id, 'product_name': product_name, 'model_type': model_type,
'version': f'v{version}', 'training_mode': training_mode, 'scope': scope,
'aggregation_method': aggregation_method, 'training_scope_description': training_scope,
'product_scope': '所有药品' if product_id == 'all' else product_name,
'timestamp': datetime.now().isoformat(), 'metrics': metrics,
'config': {
'input_dim': input_dim, 'output_dim': output_dim, 'd_model': hidden_size,

View File

@ -76,19 +76,47 @@ class ModelManager:
def get_model_version_path(self,
model_type: str,
training_mode: str,
scope: str,
version: int,
aggregation_method: Optional[str] = None) -> str:
training_mode: str,
aggregation_method: Optional[str] = None,
store_id: Optional[str] = None,
product_id: Optional[str] = None,
scope: Optional[str] = None) -> str: # scope为了兼容旧调用
"""
根据新规则生成模型版本目录的完整路径
根据 `xz训练模型保存规则.md` 中定义的新规则生成模型版本目录的完整路径
"""
base_path = os.path.join(self.model_dir, training_mode, scope)
if training_mode == 'global' and aggregation_method:
base_path = os.path.join(base_path, str(aggregation_method))
# 基础路径始终是 self.model_dir
base_path = self.model_dir
# 确定第一级目录,根据规则,所有模式都在 'global' 下
path_parts = [base_path, 'global']
if training_mode == 'global':
# global/all/{aggregation_method}/{model_type}/v{N}/
path_parts.extend(['all', str(aggregation_method)])
version_path = os.path.join(base_path, model_type, f'v{version}')
return version_path
elif training_mode == 'stores':
# global/stores/{store_id}/{aggregation_method}/{model_type}/v{N}/
if not store_id: raise ValueError("store_id is required for 'stores' training mode.")
path_parts.extend(['stores', store_id, str(aggregation_method)])
elif training_mode == 'products':
# global/products/{product_id}/{aggregation_method}/{model_type}/v{N}/
if not product_id: raise ValueError("product_id is required for 'products' training mode.")
path_parts.extend(['products', product_id, str(aggregation_method)])
elif training_mode == 'custom':
# global/custom/{store_id}/{product_id}/{aggregation_method}/{model_type}/v{N}/
if not store_id or not product_id:
raise ValueError("store_id and product_id are required for 'custom' training mode.")
path_parts.extend(['custom', store_id, product_id, str(aggregation_method)])
else:
raise ValueError(f"不支持的 training_mode: {training_mode}")
path_parts.extend([model_type, f'v{version}'])
return os.path.join(*path_parts)
def save_model_artifact(self,
artifact_data: Any,
@ -121,50 +149,22 @@ class ModelManager:
page: Optional[int] = None,
page_size: Optional[int] = None) -> Dict:
"""
通过扫描目录结构来列出所有模型
通过扫描目录结构来列出所有模型 (适配新结构)
"""
all_models = []
for training_mode in os.listdir(self.model_dir):
mode_path = os.path.join(self.model_dir, training_mode)
if not os.path.isdir(mode_path) or training_mode == 'checkpoints' or training_mode == self.VERSION_FILE:
continue
for scope in os.listdir(mode_path):
scope_path = os.path.join(mode_path, scope)
if not os.path.isdir(scope_path): continue
# 使用glob查找所有版本目录
search_pattern = os.path.join(self.model_dir, '**', 'v*')
for version_path in glob.glob(search_pattern, recursive=True):
# 确保它是一个目录并且包含 metadata.json
metadata_path = os.path.join(version_path, 'metadata.json')
if os.path.isdir(version_path) and os.path.exists(metadata_path):
model_info = self._parse_info_from_path(version_path)
if model_info:
all_models.append(model_info)
is_global_agg_level = False
if training_mode == 'global' and os.listdir(scope_path):
try:
first_item_path = os.path.join(scope_path, os.listdir(scope_path)[0])
if os.path.isdir(first_item_path):
is_global_agg_level = True
except IndexError:
continue
if is_global_agg_level:
for agg_method in os.listdir(scope_path):
agg_path = os.path.join(scope_path, agg_method)
if not os.path.isdir(agg_path): continue
for model_type in os.listdir(agg_path):
type_path = os.path.join(agg_path, model_type)
if not os.path.isdir(type_path): continue
for version_folder in os.listdir(type_path):
if version_folder.startswith('v'):
version_path = os.path.join(type_path, version_folder)
model_info = self._parse_info_from_path(version_path)
if model_info:
all_models.append(model_info)
else:
for model_type in os.listdir(scope_path):
type_path = os.path.join(scope_path, model_type)
if not os.path.isdir(type_path): continue
for version_folder in os.listdir(type_path):
if version_folder.startswith('v'):
version_path = os.path.join(type_path, version_folder)
model_info = self._parse_info_from_path(version_path)
if model_info:
all_models.append(model_info)
# 按时间戳降序排序
all_models.sort(key=lambda x: x.get('timestamp', ''), reverse=True)
total_count = len(all_models)
if page and page_size:
@ -185,7 +185,7 @@ class ModelManager:
}
def _parse_info_from_path(self, version_path: str) -> Optional[Dict]:
"""从版本目录路径解析模型信息"""
"""根据新的目录结构从版本目录路径解析模型信息"""
try:
norm_path = os.path.normpath(version_path)
norm_model_dir = os.path.normpath(self.model_dir)
@ -193,29 +193,51 @@ class ModelManager:
relative_path = os.path.relpath(norm_path, norm_model_dir)
parts = relative_path.split(os.sep)
# 期望路径: global/{scope_type}/{id...}/{agg_method}/{model_type}/v{N}
if parts[0] != 'global' or len(parts) < 5:
return None # 不是规范的新路径
info = {
'model_path': version_path,
'version': parts[-1],
'model_type': parts[-2]
'model_type': parts[-2],
'store_id': None,
'product_id': None,
}
training_mode = parts[0]
info['training_mode'] = training_mode
scope_type = parts[1] # all, stores, products, custom
if training_mode == 'global':
info['scope'] = parts[1]
if scope_type == 'all':
# global/all/sum/mlstm/v1
info['training_mode'] = 'global'
info['aggregation_method'] = parts[2]
info['model_identifier'] = self.get_model_identifier(info['model_type'], training_mode, info['scope'], info['aggregation_method'])
elif scope_type == 'stores':
# global/stores/S001/sum/mlstm/v1
info['training_mode'] = 'stores'
info['store_id'] = parts[2]
info['aggregation_method'] = parts[3]
elif scope_type == 'products':
# global/products/P001/sum/mlstm/v1
info['training_mode'] = 'products'
info['product_id'] = parts[2]
info['aggregation_method'] = parts[3]
elif scope_type == 'custom':
# global/custom/S001/P001/sum/mlstm/v1
info['training_mode'] = 'custom'
info['store_id'] = parts[2]
info['product_id'] = parts[3]
info['aggregation_method'] = parts[4]
else:
info['scope'] = parts[1]
info['aggregation_method'] = None
info['model_identifier'] = self.get_model_identifier(info['model_type'], training_mode, info['scope'])
return None
metadata_path = os.path.join(version_path, 'metadata.json')
if os.path.exists(metadata_path):
with open(metadata_path, 'r', encoding='utf-8') as f:
metadata = json.load(f)
# 确保从路径解析出的ID覆盖元数据中的因为路径是权威来源
info.update(metadata)
info['version'] = parts[-1] # 重新覆盖,确保正确
info['model_type'] = parts[-2]
return info
except (IndexError, IOError) as e:

View File

@ -268,7 +268,7 @@ def get_store_product_sales_data(store_id: str,
# 数据标准化已在load_multi_store_data中完成
# 验证必要的列是否存在
required_columns = ['sales', 'price', 'weekday', 'month', 'is_holiday', 'is_weekend', 'is_promotion', 'temperature']
required_columns = ['sales', 'weekday', 'month', 'is_holiday', 'is_weekend', 'is_promotion', 'temperature']
missing_columns = [col for col in required_columns if col not in df.columns]
if missing_columns:
@ -287,96 +287,72 @@ def get_store_product_sales_data(store_id: str,
# 返回只包含这些必需列的DataFrame
return df[existing_columns]
def aggregate_multi_store_data(product_id: Optional[str] = None,
store_id: Optional[str] = None,
aggregation_method: str = 'sum',
file_path: str = None) -> pd.DataFrame:
def aggregate_multi_store_data(product_id: Optional[Any] = None,
store_id: Optional[Any] = None,
aggregation_method: str = 'sum',
file_path: Optional[str] = None) -> Optional[pd.DataFrame]:
"""
聚合销售数据可按产品全局或按店铺所有产品
参数:
file_path: 数据文件路径
product_id: 产品ID (用于全局模型)
store_id: 店铺ID (用于店铺聚合模型)
aggregation_method: 聚合方法 ('sum', 'mean', 'median')
返回:
DataFrame: 聚合后的销售数据
聚合销售数据 (已修复支持ID列表)
- 如果提供了 product_id(s)则聚合指定产品的数据
- 如果提供了 store_id(s)则聚合指定店铺的数据
"""
# 根据是全局聚合、店铺聚合还是真正全局聚合来加载数据
if store_id:
# 店铺聚合:加载该店铺的所有数据
df = load_multi_store_data(file_path, store_id=store_id)
if len(df) == 0:
raise ValueError(f"没有找到店铺 {store_id} 的销售数据")
grouping_entity = f"店铺 {store_id}"
elif product_id:
# 按产品聚合:加载该产品在所有店铺的数据
df = load_multi_store_data(file_path, product_id=product_id)
if len(df) == 0:
raise ValueError(f"没有找到产品 {product_id} 的销售数据")
grouping_entity = f"产品 {product_id}"
else:
# 真正全局聚合:加载所有数据
if file_path is None:
file_path = DEFAULT_DATA_PATH
try:
# 先加载所有数据,再进行过滤
df = load_multi_store_data(file_path)
if len(df) == 0:
raise ValueError("数据文件为空,无法进行全局聚合")
grouping_entity = "所有产品"
# 按日期聚合(使用标准化后的列名)
agg_dict = {}
if aggregation_method == 'sum':
agg_dict = {
'sales': 'sum', # 标准化后的销量列
'sales_amount': 'sum',
'price': 'mean' # 标准化后的价格列,取平均值
}
elif aggregation_method == 'mean':
agg_dict = {
'sales': 'mean',
'sales_amount': 'mean',
'price': 'mean'
}
elif aggregation_method == 'median':
agg_dict = {
'sales': 'median',
'sales_amount': 'median',
'price': 'median'
}
# 确保列名存在
available_cols = df.columns.tolist()
agg_dict = {k: v for k, v in agg_dict.items() if k in available_cols}
# 聚合数据
aggregated_df = df.groupby('date').agg(agg_dict).reset_index()
# 获取产品信息(取第一个店铺的信息)
product_info = df[['product_id', 'product_name', 'product_category']].iloc[0]
for col, val in product_info.items():
aggregated_df[col] = val
# 添加店铺信息标识为全局
aggregated_df['store_id'] = 'GLOBAL'
aggregated_df['store_name'] = f'全部店铺-{aggregation_method.upper()}'
aggregated_df['store_location'] = '全局聚合'
aggregated_df['store_type'] = 'global'
# 对聚合后的数据进行标准化(添加缺失的特征列)
aggregated_df = aggregated_df.sort_values('date').copy()
aggregated_df = standardize_column_names(aggregated_df)
# 定义模型训练所需的所有列(特征 + 目标)
final_columns = [
'date', 'sales', 'product_id', 'product_name', 'store_id', 'store_name',
'weekday', 'month', 'is_holiday', 'is_weekend', 'is_promotion', 'temperature'
]
# 筛选出DataFrame中实际存在的列
existing_columns = [col for col in final_columns if col in aggregated_df.columns]
# 返回只包含这些必需列的DataFrame
return aggregated_df[existing_columns]
if df.empty:
raise ValueError("数据文件为空或加载失败")
# 根据 store_id 和 product_id 进行过滤 (支持列表和单个ID)
if store_id:
if isinstance(store_id, list):
df = df[df['store_id'].isin(store_id)]
else:
df = df[df['store_id'] == store_id]
if product_id:
if isinstance(product_id, list):
df = df[df['product_id'].isin(product_id)]
else:
df = df[df['product_id'] == product_id]
if df.empty:
raise ValueError(f"根据所选店铺/产品过滤后无数据")
# 确定聚合后的实体名称
if store_id and not product_id:
grouping_entity_name = df['store_name'].iloc[0] if len(df['store_id'].unique()) == 1 else "多个店铺聚合"
elif product_id and not store_id:
grouping_entity_name = df['product_name'].iloc[0] if len(df['product_id'].unique()) == 1 else "多个产品聚合"
elif store_id and product_id:
grouping_entity_name = f"{df['store_name'].iloc[0]} - {df['product_name'].iloc[0]}" if len(df['store_id'].unique()) == 1 and len(df['product_id'].unique()) == 1 else "自定义聚合"
else:
grouping_entity_name = "全局聚合模型"
# 按日期聚合
agg_df = df.groupby('date').agg({
'sales': aggregation_method,
'temperature': 'mean',
'is_holiday': 'max',
'is_weekend': 'max',
'is_promotion': 'max',
'weekday': 'first',
'month': 'first'
}).reset_index()
agg_df['product_name'] = grouping_entity_name
for col in ['is_holiday', 'is_weekend', 'is_promotion']:
if col not in agg_df:
agg_df[col] = 0
return agg_df
except Exception as e:
print(f"聚合数据失败: {e}")
return None
def get_sales_statistics(file_path: str = None,
store_id: Optional[str] = None,

View File

@ -45,6 +45,11 @@ class TrainingTask:
training_mode: str
store_id: Optional[str] = None
epochs: int = 100
product_scope: Optional[str] = 'all'
product_ids: Optional[list] = None
store_ids: Optional[list] = None
training_scope: Optional[str] = 'all_stores_all_products'
aggregation_method: Optional[str] = 'sum'
status: str = "pending" # pending, running, completed, failed
start_time: Optional[str] = None
end_time: Optional[str] = None
@ -90,24 +95,6 @@ class TrainingWorker:
task.process_id = os.getpid()
self.result_queue.put(('update', asdict(task)))
# 模拟训练进度更新
for epoch in range(1, task.epochs + 1):
progress = (epoch / task.epochs) * 100
# 发送进度更新
self.progress_queue.put({
'task_id': task.task_id,
'progress': progress,
'epoch': epoch,
'total_epochs': task.epochs,
'message': f"Epoch {epoch}/{task.epochs}"
})
training_logger.info(f"🔄 训练进度: Epoch {epoch}/{task.epochs} ({progress:.1f}%)")
# 模拟训练时间
time.sleep(1) # 实际训练中这里会是真正的训练代码
# 导入真正的训练函数
try:
# 添加服务器目录到路径确保能找到core模块
@ -144,11 +131,22 @@ class TrainingWorker:
epochs=task.epochs,
store_id=task.store_id,
training_mode=task.training_mode,
product_ids=task.product_ids,
product_scope=task.product_scope,
store_ids=task.store_ids,
training_scope=task.training_scope,
aggregation_method=task.aggregation_method,
socketio=None, # 子进程中不能直接使用socketio
task_id=task.task_id,
progress_callback=progress_callback # 传递进度回调函数
)
# 检查训练结果如果为None则表示训练失败
if metrics is None:
# predictor.py 已经记录了详细的错误日志
# 这里我们抛出异常,以触发通用的失败处理流程
raise ValueError("训练器未能成功返回结果,任务失败。请检查之前的日志获取详细错误信息。")
# 发送训练完成日志到主控制台
self.progress_queue.put({
'task_id': task.task_id,
@ -156,12 +154,11 @@ class TrainingWorker:
'message': f"{task.model_type} 模型训练完成!"
})
if metrics:
self.progress_queue.put({
'task_id': task.task_id,
'log_type': 'info',
'message': f"📊 训练指标: MSE={metrics.get('mse', 'N/A'):.4f}, RMSE={metrics.get('rmse', 'N/A'):.4f}"
})
self.progress_queue.put({
'task_id': task.task_id,
'log_type': 'info',
'message': f"📊 训练指标: MSE={metrics.get('mse', 'N/A'):.4f}, RMSE={metrics.get('rmse', 'N/A'):.4f}"
})
except ImportError as e:
training_logger.error(f"❌ 导入训练器失败: {e}")
# 返回模拟的训练结果用于测试
@ -281,23 +278,34 @@ class TrainingProcessManager:
self.logger.info("✅ 训练进程管理器已停止")
def submit_task(self, product_id: str, model_type: str, training_mode: str = "product",
store_id: str = None, epochs: int = 100, **kwargs) -> str:
def submit_task(self, product_id: str, model_type: str, training_mode: str = "product",
store_id: Optional[str] = None, epochs: int = 100,
product_ids: Optional[list] = None,
product_scope: str = 'all',
store_ids: Optional[list] = None,
training_scope: str = 'all_stores_all_products',
aggregation_method: str = 'sum',
**kwargs) -> str:
"""提交训练任务"""
task_id = str(uuid.uuid4())
task = TrainingTask(
task_id=task_id,
product_id=product_id,
model_type=model_type,
training_mode=training_mode,
store_id=store_id,
epochs=epochs
epochs=epochs,
product_ids=product_ids,
product_scope=product_scope,
store_ids=store_ids,
training_scope=training_scope,
aggregation_method=aggregation_method
)
with self.lock:
self.tasks[task_id] = task
# 将任务放入队列
self.task_queue.put(asdict(task))

View File

@ -1,873 +1,39 @@
### 根目录启动
**1**:`uv venv`
**2**:`uv pip install loguru numpy pandas torch matplotlib flask flask_cors flask_socketio flasgger scikit-learn tqdm pytorch_tcn pyarrow`
**3**: `uv run .\server\api.py`
### UI
**1**:`npm install` `npm run dev`
# 修改记录日志 (日期: 2025-07-16)
# “预测分析”模块UI重构修改记录
## 1. 核心 Bug 修复
**任务目标**: 将原有的、通过下拉菜单切换模式的单一预测页面重构为通过左侧子导航切换模式的多页面布局使其UI结构与“模型训练”模块保持一致。
### 文件: `server/core/predictor.py`
- **问题**: 在 `train_model` 方法中调用内部辅助函数 `_prepare_training_params` 时,没有正确传递 `product_ids``store_ids` 参数,导致在 `_prepare_training_params` 内部发生 `NameError`
- **修复**:
- 修正了 `train_model` 方法内部对 `_prepare_training_params` 的调用,确保 `product_ids``store_ids` 被显式传递。
- 此前已修复 `train_model` 的函数签名,使其能正确接收 `store_ids`
- **结果**: 彻底解决了训练流程中的参数传递问题,根除了由此引发的 `NameError`
### 后端修复 (2025-07-13)
## 2. 代码清理与重构
**任务目标**: 解决模型训练时因数据文件路径错误导致的数据加载失败问题。
### 文件: `server/api.py`
- **核心问题**: `server/core/predictor.py` 中的 `PharmacyPredictor` 类初始化时,硬编码了错误的默认数据文件路径 (`'pharmacy_sales_multi_store.csv'`)。
- **修复方案**:
1. 修改 `server/core/predictor.py`,将默认数据路径更正为 `'data/timeseries_training_data_sample_10s50p.parquet'`
2. 同步更新了 `server/trainers/mlstm_trainer.py` 中所有对数据加载函数的调用,确保使用正确的文件路径。
- **结果**: 彻底解决了在独立训练进程中数据加载失败的问题。
- **内容**: 移除了在 `start_training` API 端点中遗留的旧版、基于线程(`threading.Thread`)的训练逻辑。
- **原因**: 该代码块已被新的、基于多进程(`multiprocessing`)的 `TrainingProcessManager` 完全取代。旧代码中包含了大量用于调试的 `thread_safe_print` 日志,已无用处。
- **结果**: `start_training` 端点的逻辑变得更加清晰,只负责参数校验和向 `TrainingProcessManager` 提交任务。
---
### 后端修复 (2025-07-13) - 数据流重构
### 文件: `server/utils/training_process_manager.py`
**任务目标**: 解决因数据处理流程中断导致 `sales``price` 关键特征丢失,从而引发模型训练失败的根本问题。
- **内容**: 在 `TrainingWorker``run_training_task` 方法中,移除了一个用于模拟训练进度的 `for` 循环。
- **原因**: 该循环包含 `time.sleep(1)`,仅用于在没有实际训练逻辑时模拟进度更新,现在实际的训练器会通过回调函数报告真实进度,因此该模拟代码不再需要。
- **结果**: `TrainingWorker` 现在直接调用实际的训练器,不再有模拟延迟,代码更贴近生产环境。
- **核心问题**:
1. `server/core/predictor.py` 中的 `train_model` 方法在调用训练器(如 `train_product_model_with_mlstm`)时,没有将预处理好的数据传递过去。
2. `server/trainers/mlstm_trainer.py` 因此被迫重新加载和处理数据,但其使用的数据标准化函数 `standardize_column_names` 存在逻辑缺陷,导致关键列丢失。
## 3. 启动依赖
- **修复方案 (数据流重构)**:
1. **修改 `server/trainers/mlstm_trainer.py`**:
- 重构 `train_product_model_with_mlstm` 函数,使其能够接收一个预处理好的 DataFrame (`product_df`) 作为参数。
- 移除了函数内部所有的数据加载和重复处理逻辑。
2. **修改 `server/core/predictor.py`**:
- 在 `train_model` 方法中,将已经加载并处理好的 `product_data` 作为参数,显式传递给 `train_product_model_with_mlstm` 函数。
3. **修改 `server/utils/multi_store_data_utils.py`**:
- 在 `standardize_column_names` 函数中,使用 Pandas 的 `rename` 方法强制进行列名转换,确保 `quantity_sold``unit_price` 被可靠地重命名为 `sales``price`
- **结果**: 彻底修复了数据处理流程,确保数据只被加载和标准化一次,并被正确传递,从根本上解决了模型训练失败的问题。
---
### 第一次重构 (多页面、双栏布局)
- **新增文件**:
- `UI/src/views/prediction/ProductPredictionView.vue`
- `UI/src/views/prediction/StorePredictionView.vue`
- `UI/src/views/prediction/GlobalPredictionView.vue`
- **修改文件**:
- `UI/src/router/index.js`: 添加了指向新页面的路由。
- `UI/src/App.vue`: 将“预测分析”修改为包含三个子菜单的父菜单。
---
### 第二次重构 (基于用户反馈的单页面布局)
**任务目标**: 统一三个预测子页面的布局,采用旧的单页面预测样式,并将导航功能与页面内容解耦。
- **修改文件**:
- **`UI/src/views/prediction/ProductPredictionView.vue`**:
- **内容**: 使用 `UI/src/views/NewPredictionView.vue` 的布局进行替换。
- **逻辑**: 移除了“模型训练方式”选择器,并将该页面的预测模式硬编码为 `product`
- **`UI/src/views/prediction/StorePredictionView.vue`**:
- **内容**: 使用 `UI/src/views/NewPredictionView.vue` 的布局进行替换。
- **逻辑**: 移除了“模型训练方式”选择器,并将该页面的预测模式硬编码为 `store`
- **`UI/src/views/prediction/GlobalPredictionView.vue`**:
- **内容**: 使用 `UI/src/views/NewPredictionView.vue` 的布局进行替换。
- **逻辑**: 移除了“模型训练方式”及特定目标选择器,并将该页面的预测模式硬编码为 `global`
---
**总结**: 通过两次重构,最终实现了使用左侧导航栏切换预测模式,同时右侧内容区域保持统一、简洁的单页面布局,完全符合用户的最终要求。
---
**按药品训练修改**
**日期**: 2025-07-14
**文件**: `server/trainers/mlstm_trainer.py`
**问题**: 模型训练因 `KeyError: "['sales', 'price'] not in index"` 失败。
**分析**:
1. `'price'` 列在提供的数据中不存在,导致 `KeyError`
2. `'sales'` 列作为历史输入(自回归特征)对于模型训练是必要的。
**解决方案**: 从 `mlstm_trainer` 的特征列表中移除了不存在的 `'price'` 列,保留了 `'sales'` 列用于自回归。
---
**日期**: 2025-07-14 (补充)
**文件**:
* `server/trainers/transformer_trainer.py`
* `server/trainers/tcn_trainer.py`
* `server/trainers/kan_trainer.py`
**问题**: 预防性修复。这些文件存在与 `mlstm_trainer.py` 相同的 `KeyError` 隐患。
**分析**: 经过检查,这些训练器与 `mlstm_trainer` 共享相同的数据处理逻辑,其硬编码的特征列表中都包含了不存在的 `'price'` 列。
**解决方案**: 统一从所有相关训练器的特征列表中移除了 `'price'` 列,以确保所有模型训练的健壮性。
---
**日期**: 2025-07-14 (深度修复)
**文件**: `server/utils/multi_store_data_utils.py`
**问题**: 追踪 `KeyError: "['sales'] not in index"` 时,发现数据标准化流程存在多个问题。
**分析**:
1. 通过 `uv run` 读取了 `.parquet` 数据文件,确认了原始列名。
2. 发现 `standardize_column_names` 函数中的重命名映射与原始列名不匹配 (例如 `quantity_sold` vs `sales_quantity`)。
3. 确认了原始数据中没有 `price` 列,但代码中存在对它的依赖。
4. 函数缺乏一个明确的返回列选择机制,导致 `sales` 列在数据准备阶段被意外丢弃。
**解决方案**:
1. 修正了 `rename_map` 以正确匹配原始数据列名 (`sales_quantity` -> `sales`, `temperature_2m_mean` -> `temperature`, `dayofweek` -> `weekday`)。
2. 移除了对不存在的 `price` 列的依赖。
3. 在函数末尾添加了逻辑,确保返回的 `DataFrame` 包含所有模型训练所需的标准列(特征 + 目标),保证了数据流的稳定性。
4. 原始数据列名:['date', 'store_id', 'product_id', 'sales_quantity', 'sales_amount', 'gross_profit', 'customer_traffic', 'store_name', 'city', 'product_name', 'manufacturer', 'category_l1', 'category_l2', 'category_l3', 'abc_category', 'temperature_2m_mean', 'temperature_2m_max', 'temperature_2m_min', 'year', 'month', 'day', 'dayofweek', 'dayofyear', 'weekofyear', 'is_weekend', 'sl_lag_7', 'sl_lag_14', 'sl_rolling_mean_7', 'sl_rolling_std_7', 'sl_rolling_mean_14', 'sl_rolling_std_14']
---
**日期**: 2025-07-14 10:16
**主题**: 修复模型训练中的 `KeyError` 及数据流问题 (详细版)
### 阶段一:修复训练器层 `KeyError`
* **问题**: 模型训练因 `KeyError: "['sales', 'price'] not in index"` 失败。
* **分析**: 训练器硬编码的特征列表中包含了数据源中不存在的 `'price'` 列。
* **涉及文件**:
* `server/trainers/mlstm_trainer.py`
* `server/trainers/transformer_trainer.py`
* `server/trainers/tcn_trainer.py`
* `server/trainers/kan_trainer.py`
* **修改详情**:
* **位置**: 每个训练器文件中的 `features` 列表定义处。
* **操作**: 修改。
* **内容**:
```diff
- features = ['sales', 'price', 'weekday', 'month', 'is_holiday', 'is_weekend', 'is_promotion', 'temperature']
+ features = ['sales', 'weekday', 'month', 'is_holiday', 'is_weekend', 'is_promotion', 'temperature']
```
* **原因**: 移除对不存在的 `'price'` 列的依赖,解决 `KeyError`
### 阶段二:修复数据标准化层
* **问题**: 修复后出现新错误 `KeyError: "['sales'] not in index"`,表明数据标准化流程存在缺陷。
* **分析**: 通过 `uv run` 读取 `.parquet` 文件确认,`standardize_column_names` 函数中的列名映射错误,且缺少最终列选择机制。
* **涉及文件**: `server/utils/multi_store_data_utils.py`
* **修改详情**:
1. **位置**: `standardize_column_names` 函数, `rename_map` 字典。
* **操作**: 修改。
* **内容**:
```diff
- rename_map = { 'quantity_sold': 'sales', 'unit_price': 'price', 'day_of_week': 'weekday' }
+ rename_map = { 'sales_quantity': 'sales', 'temperature_2m_mean': 'temperature', 'dayofweek': 'weekday' }
```
* **原因**: 修正键名以匹配数据源的真实列名 (`sales_quantity`, `temperature_2m_mean`, `dayofweek`)。
2. **位置**: `standardize_column_names` 函数, `sales_amount` 计算部分。
* **操作**: 修改 (注释)。
* **内容**:
```diff
- if 'sales_amount' not in df.columns and 'sales' in df.columns and 'price' in df.columns:
- df['sales_amount'] = df['sales'] * df['price']
+ # 由于没有price列sales_amount的计算逻辑需要调整或移除
+ # if 'sales_amount' not in df.columns and 'sales' in df.columns and 'price' in df.columns:
+ # df['sales_amount'] = df['sales'] * df['price']
```
* **原因**: 避免因缺少 `'price'` 列而导致潜在错误。
3. **位置**: `standardize_column_names` 函数, `numeric_columns` 列表。
* **操作**: 删除。
* **内容**:
```diff
- numeric_columns = ['sales', 'price', 'sales_amount', 'weekday', 'month', 'temperature']
+ numeric_columns = ['sales', 'sales_amount', 'weekday', 'month', 'temperature']
```
* **原因**: 从数值类型转换列表中移除不存在的 `'price'` 列。
4. **位置**: `standardize_column_names` 函数, `return` 语句前。
* **操作**: 增加。
* **内容**:
```diff
+ # 定义模型训练所需的所有列(特征 + 目标)
+ final_columns = [
+ 'date', 'sales', 'product_id', 'product_name', 'store_id', 'store_name',
+ 'weekday', 'month', 'is_holiday', 'is_weekend', 'is_promotion', 'temperature'
+ ]
+ # 筛选出DataFrame中实际存在的列
+ existing_columns = [col for col in final_columns if col in df.columns]
+ # 返回只包含这些必需列的DataFrame
+ return df[existing_columns]
```
* **原因**: 增加列选择机制,确保函数返回的 `DataFrame` 结构统一且包含 `sales` 列,从根源上解决 `KeyError: "['sales'] not in index"`
### 阶段三:修复数据流分发层
* **问题**: `predictor.py` 未将处理好的数据统一传递给所有训练器。
* **分析**: `train_model` 方法中,只有 `mlstm` 的调用传递了 `product_df`,其他模型则没有,导致它们重新加载未处理的数据。
* **涉及文件**: `server/core/predictor.py`
* **修改详情**:
* **位置**: `train_model` 方法中对 `train_product_model_with_transformer`, `_tcn`, `_kan` 的调用处。
* **操作**: 增加。
* **内容**: 在函数调用中增加了 `product_df=product_data` 参数。
```diff
- model_result, metrics, actual_version = train_product_model_with_transformer(product_id, ...)
+ model_result, metrics, actual_version = train_product_model_with_transformer(product_id=product_id, product_df=product_data, ...)
```
*(对 `tcn``kan` 的调用也做了类似修改)*
* **原因**: 统一数据流,确保所有训练器都使用经过正确预处理的、包含完整信息的 `DataFrame`
### 阶段四:适配训练器以接收数据
* **问题**: `transformer`, `tcn`, `kan` 训练器需要能接收上游传来的数据。
* **分析**: 需要修改这三个训练器的函数签名和内部逻辑,使其在接收到 `product_df` 时跳过数据加载。
* **涉及文件**: `server/trainers/transformer_trainer.py`, `tcn_trainer.py`, `kan_trainer.py`
* **修改详情**:
1. **位置**: 每个训练器主函数的定义处。
* **操作**: 增加。
* **内容**: 在函数参数中增加了 `product_df=None`
```diff
- def train_product_model_with_transformer(product_id, ...)
+ def train_product_model_with_transformer(product_id, product_df=None, ...)
```
2. **位置**: 每个训练器内部的数据加载逻辑处。
* **操作**: 增加。
* **内容**: 增加了 `if product_df is None:` 的判断逻辑,只有在未接收到数据时才执行内部加载。
```diff
+ if product_df is None:
- # 根据训练模式加载数据
- from utils.multi_store_data_utils import load_multi_store_data
- ...
+ # [原有的数据加载逻辑]
+ else:
+ # 如果传入了product_df直接使用
+ ...
```
* **原因**: 完成数据流修复的最后一环,使训练器能够灵活地接收外部数据或自行加载,彻底解决问题。
---
**日期**: 2025-07-14 10:38
**主题**: 修复因NumPy类型导致的JSON序列化失败问题
### 阶段五:修复前后端通信层
* **问题**: 模型训练成功后后端向前端发送包含训练指标metrics的WebSocket消息或API响应时失败导致前端状态无法更新为“已完成”。
* **日志错误**: `Object of type float32 is not JSON serializable`
* **分析**: 训练过程产生的评估指标(如 `mse`, `rmse`是NumPy的 `float32` 类型。Python标准的 `json` 库无法直接序列化这种类型导致在通过WebSocket或HTTP API发送数据时出错。
* **涉及文件**: `server/utils/training_process_manager.py`
* **修改详情**:
1. **位置**: 文件顶部。
* **操作**: 增加。
* **内容**:
```python
import numpy as np
def convert_numpy_types(obj):
"""递归地将字典/列表中的NumPy类型转换为Python原生类型"""
if isinstance(obj, dict):
return {k: convert_numpy_types(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [convert_numpy_types(i) for i in obj]
elif isinstance(obj, np.generic):
return obj.item()
return obj
```
* **原因**: 添加一个通用的辅助函数用于将包含NumPy类型的数据结构转换为JSON兼容的格式。
2. **位置**: `_monitor_results` 方法内部,调用 `self.websocket_callback` 之前。
* **操作**: 增加。
* **内容**:
```diff
+ serializable_task_data = convert_numpy_types(task_data)
- self.websocket_callback('training_update', { ... 'metrics': task_data.get('metrics'), ... })
+ self.websocket_callback('training_update', { ... 'metrics': serializable_task_data.get('metrics'), ... })
```
* **原因**: 在通过WebSocket发送数据之前调用 `convert_numpy_types` 函数对包含训练结果的 `task_data` 进行处理,确保所有 `float32` 等类型都被转换为Python原生的 `float`,从而解决序列化错误。
**总结**: 通过在数据发送前进行类型转换,彻底解决了前后端通信中的序列化问题,确保了训练状态能够被正确地更新到前端。
---
**日期**: 2025-07-14 11:04
**主题**: 根治JSON序列化问题
### 阶段六修复API层序列化错误
* **问题**: 在修复WebSocket的序列化问题后发现直接轮询 `GET /api/training` 接口时,仍然出现 `Object of type float32 is not JSON serializable` 错误。
* **分析**: 上一阶段的修复只转换了准备通过WebSocket发送的数据但没有转换**存放在 `TrainingProcessManager` 内部 `self.tasks` 字典中的数据**。因此当API通过 `get_all_tasks()` 方法读取这个字典时获取到的仍然是包含NumPy类型的原始数据导致 `jsonify` 失败。
* **涉及文件**: `server/utils/training_process_manager.py`
* **修改详情**:
* **位置**: `_monitor_results` 方法,从 `result_queue` 获取数据之后。
* **操作**: 调整逻辑。
* **内容**:
```diff
- with self.lock:
- # ... 更新 self.tasks ...
- if self.websocket_callback:
- serializable_task_data = convert_numpy_types(task_data)
- # ... 使用 serializable_task_data 发送消息 ...
+ # 立即对从队列中取出的数据进行类型转换
+ serializable_task_data = convert_numpy_types(task_data)
+ with self.lock:
+ # 使用转换后的数据更新任务状态
+ for key, value in serializable_task_data.items():
+ setattr(self.tasks[task_id], key, value)
+ # WebSocket通知 - 使用已转换的数据
+ if self.websocket_callback:
+ # ... 使用 serializable_task_data 发送消息 ...
```
* **原因**: 将类型转换的步骤提前,确保存入 `self.tasks` 的数据已经是JSON兼容的。这样无论是通过WebSocket推送还是通过API查询获取到的都是安全的数据从根源上解决了所有序列化问题。
**最终总结**: 至此,所有已知的数据流和数据类型问题均已解决。
---
**日期**: 2025-07-14 11:15
**主题**: 修复模型评估中的MAPE计算错误
### 阶段七:修复评估指标计算
* **问题**: 训练 `transformer` 模型时,日志显示 `MAPE: nan%` 并伴有 `RuntimeWarning: Mean of empty slice.`
* **分析**: `MAPE` (平均绝对百分比误差) 的计算涉及除以真实值。当测试集中的所有真实销量(`y_true`都为0时用于避免除零错误的 `mask` 会导致一个空数组被传递给 `np.mean()`,从而产生 `nan` 和运行时警告。
* **涉及文件**: `server/analysis/metrics.py`
* **修改详情**:
* **位置**: `evaluate_model` 函数中计算 `mape` 的部分。
* **操作**: 增加条件判断。
* **内容**:
```diff
- mask = y_true != 0
- mape = np.mean(np.abs((y_true[mask] - y_pred[mask]) / y_true[mask])) * 100
+ mask = y_true != 0
+ if np.any(mask):
+ mape = np.mean(np.abs((y_true[mask] - y_pred[mask]) / y_true[mask])) * 100
+ else:
+ # 如果所有真实值都为0无法计算MAPE返回0
+ mape = 0.0
```
* **原因**: 在计算MAPE之前先检查是否存在任何非零的真实值。如果不存在则直接将MAPE设为0避免了对空数组求平均值从而解决了 `nan``RuntimeWarning` 的问题。
## 2025-07-14 11:41修复“按店铺训练”页面店铺列表加载失败问题
**问题描述:**
在“模型训练” -> “按店铺训练”页面中,“选择店铺”的下拉列表为空,无法加载任何店铺信息。
**根本原因:**
位于 `server/utils/multi_store_data_utils.py``standardize_column_names` 函数在标准化数据后,错误地移除了包括店铺元数据在内的非训练必需列。这导致调用该函数的 `get_available_stores` 函数无法获取到完整的店铺信息,最终返回一个空列表。
**解决方案:**
本着最小改动和保持代码清晰的原则,我进行了以下重构:
1. **净化 `standardize_column_names` 函数**:移除了其中所有与列筛选相关的代码,使其只专注于数据标准化这一核心职责。
2. **精确应用筛选逻辑**:将列筛选的逻辑精确地移动到了 `get_store_product_sales_data``aggregate_multi_store_data` 这两个为模型训练准备数据的函数中。这确保了只有在需要为模型准备数据时,才会执行列筛选。
3. **增强 `get_available_stores` 函数**:由于 `load_multi_store_data` 现在可以返回所有列,`get_available_stores` 将能够正常工作。同时,我增强了其代码的健壮性,以优雅地处理数据文件中可能存在的列缺失问题。
**代码变更:**
- **文件:** `server/utils/multi_store_data_utils.py`
- **主要改动:**
- 从 `standardize_column_names` 中移除列筛选逻辑。
- 在 `get_store_product_sales_data``aggregate_multi_store_data` 中添加列筛选逻辑。
- 重写 `get_available_stores` 以更健壮地处理数据。
---
**日期**: 2025-07-14 13:00
**主题**: 修复“按店铺训练-所有药品”模式下的训练失败问题
### 问题描述
在“模型训练” -> “按店铺训练”页面,当选择“所有药品”进行训练时,后端日志显示 `获取店铺产品数据失败: 没有找到店铺 [store_id] 产品 unknown 的销售数据`,导致训练任务失败。
### 根本原因
1. **API层**: `server/api.py` 在处理来自前端的训练请求时,如果 `product_id``null`(对应“所有药品”选项),会执行 `product_id or "unknown"`错误地将产品ID设置为字符串 `"unknown"`
2. **预测器层**: `server/core/predictor.py` 中的 `train_model` 方法接收到无效的 `product_id="unknown"`尝试使用它来获取数据但数据源中不存在ID为“unknown”的产品导致数据加载失败。
3. **数据工具层**: `server/utils/multi_store_data_utils.py` 中的 `aggregate_multi_store_data` 函数只支持按产品ID进行全局聚合不支持按店铺ID聚合其下所有产品的数据。
### 解决方案 (保留"unknown"字符串)
为了在不改变API层行为的前提下解决问题采用了在下游处理这个特殊值的策略
1. **修改 `server/core/predictor.py`**:
* **位置**: `train_model` 方法。
* **操作**: 增加了对 `product_id == 'unknown'` 的特殊处理逻辑。
* **内容**:
```python
# 如果product_id是'unknown',则表示为店铺所有商品训练一个聚合模型
if product_id == 'unknown':
try:
# 使用聚合函数,按店铺聚合
product_data = aggregate_multi_store_data(
store_id=store_id,
aggregation_method=aggregation_method,
file_path=self.data_path
)
# 将product_id设置为店铺ID以便模型保存时使用有意义的标识
product_id = store_id
except Exception as e:
# ... 错误处理 ...
else:
# ... 原有的按单个产品获取数据的逻辑 ...
```
* **原因**: 在预测器层面拦截无效的 `"unknown"` ID并将其意图正确地转换为“聚合此店铺的所有产品数据”。同时`product_id` 重新赋值为 `store_id`,确保了后续模型保存时能使用一个唯一且有意义的名称(如 `store_01010023_mlstm_v1.pth`)。
2. **修改 `server/utils/multi_store_data_utils.py`**:
* **位置**: `aggregate_multi_store_data` 函数。
* **操作**: 重构函数签名和内部逻辑。
* **内容**:
```python
def aggregate_multi_store_data(product_id: Optional[str] = None,
store_id: Optional[str] = None,
aggregation_method: str = 'sum',
...)
# ...
if store_id:
# 店铺聚合:加载该店铺的所有数据
df = load_multi_store_data(file_path, store_id=store_id)
# ...
elif product_id:
# 全局聚合:加载该产品的所有数据
df = load_multi_store_data(file_path, product_id=product_id)
# ...
else:
raise ValueError("必须提供 product_id 或 store_id")
```
* **原因**: 扩展了数据聚合函数的功能,使其能够根据传入的 `store_id` 参数,加载并聚合特定店铺的所有销售数据,为店铺级别的综合模型训练提供了数据基础。
**最终结果**: 通过这两处修改,系统现在可以正确处理“按店铺-所有药品”的训练请求。它会聚合该店铺所有产品的销售数据训练一个综合模型并以店铺ID为标识来保存该模型彻底解决了该功能点的训练失败问题。
---
**日期**: 2025-07-14 14:19
**主题**: 修复并发训练中的稳定性和日志错误
### 阶段八:修复并发训练中的多个错误
* **问题**: 在并发执行多个训练任务时,系统出现 `JSON序列化错误``API列表排序错误``WebSocket连接错误`
* **分析**:
1. **`Object of type float32 is not JSON serializable`**: `training_process_manager.py` 在通过WebSocket发送**中途**的训练进度时没有对包含NumPy `float32` 类型的 `metrics` 数据进行序列化。
2. **`'<' not supported between instances of 'str' and 'NoneType'`**: `api.py` 在获取训练任务列表时,对 `start_time` 进行排序,但未处理某些任务的 `start_time` 可能为 `None` 的情况,导致 `TypeError`
3. **`AssertionError: write() before start_response`**: `api.py` 中,当以 `debug=True` 模式运行时Flask内置的Werkzeug服务器的调试器与Socket.IO的连接管理机制发生冲突。
* **解决方案**:
1. **文件**: `server/utils/training_process_manager.py`
* **位置**: `_monitor_progress` 方法。
* **操作**: 在发送 `training_progress` 事件前,调用 `convert_numpy_types` 函数对 `progress_data` 进行完全序列化。
* **原因**: 确保所有通过WebSocket发送的数据包括中途进度都是JSON兼容的彻底解决序列化问题。
2. **文件**: `server/api.py`
* **位置**: `get_all_training_tasks` 函数。
* **操作**: 修改 `sorted` 函数的 `key`,使用 `lambda x: x.get('start_time') or '1970-01-01 00:00:00'`
* **原因**: 为 `None` 类型的 `start_time` 提供一个有效的默认值,使其可以和字符串类型的日期进行安全比较,解决了排序错误。
3. **文件**: `server/api.py`
* **位置**: `socketio.run()` 调用处。
* **操作**: 增加 `allow_unsafe_werkzeug=True if args.debug else False` 参数。
* **原因**: 这是 `Flask-SocketIO` 官方推荐的解决方案用于在调试模式下协调Werkzeug与Socket.IO的事件循环避免底层WSGI错误。
**最终结果**: 通过这三项修复,系统的并发稳定性和健壮性得到显著提升,解决了在高并发训练场景下出现的各类错误。
---
**日期**: 2025-07-14 14:48
**主题**: 修复模型评估指标计算错误并优化训练过程
### 阶段九:修复模型评估与训练优化
* **问题**: 所有模型训练完成后,评估指标 `R²` 始终为0.0`MAPE` 始终为0.00%,这表明模型评估或训练过程存在严重问题。
* **分析**:
1. **核心错误**: 在 `mlstm_trainer.py``transformer_trainer.py` 中,计算损失函数时,模型输出 `outputs` 的维度是 `(batch_size, forecast_horizon)`,而目标 `y_batch` 的维度被错误地通过 `unsqueeze(-1)` 修改为 `(batch_size, forecast_horizon, 1)`。这种维度不匹配导致损失计算错误,模型无法正确学习。
2. **优化缺失**: 训练过程中缺少学习率调度、梯度裁剪和提前停止等关键的优化策略,影响了训练效率和稳定性。
* **解决方案**:
1. **修复维度不匹配 (关键修复)**:
* **文件**: `server/trainers/mlstm_trainer.py`, `server/trainers/transformer_trainer.py`
* **位置**: 训练和验证循环中的损失计算部分。
* **操作**: 移除了对 `y_batch``unsqueeze(-1)` 操作,确保 `outputs``y_batch` 维度一致。
```diff
- loss = criterion(outputs, y_batch.unsqueeze(-1))
+ loss = criterion(outputs, y_batch.squeeze(-1) if y_batch.dim() == 3 else y_batch)
```
* **原因**: 修正损失函数的输入使模型能够根据正确的误差进行学习从而解决评估指标恒为0的问题。
2. **增加训练优化策略**:
* **文件**: `server/trainers/mlstm_trainer.py`, `server/trainers/transformer_trainer.py`
* **操作**: 在两个训练器中增加了以下功能:
* **学习率调度器**: 引入 `torch.optim.lr_scheduler.ReduceLROnPlateau`,当测试损失停滞时自动降低学习率。
* **梯度裁剪**: 在优化器更新前,使用 `torch.nn.utils.clip_grad_norm_` 对梯度进行裁剪,防止梯度爆炸。
* **提前停止**: 增加了 `patience` 参数当测试损失连续多个epoch未改善时提前终止训练防止过拟合。
* **原因**: 引入这些业界标准的优化技术,可以显著提高训练过程的稳定性、收敛速度和最终的模型性能。
**最终结果**: 通过修复核心的逻辑错误并引入多项优化措施,模型现在不仅能够正确学习,而且训练过程更加健壮和高效。
---
**日期**: 2025-07-14 15:20
**主题**: 根治模型维度错误并统一数据流 (完整调试过程)
### 阶段九:错误的修复尝试 (记录备查)
* **问题**: 所有模型训练完成后,评估指标 `R²` 始终为0.0`MAPE` 始终为0.00%。
* **初步分析**: 怀疑损失函数计算时,`outputs``y_batch` 维度不匹配。
* **错误的假设**: 当时错误地认为是 `y_batch` 的维度有问题,而 `outputs` 的维度是正确的。
* **错误的修复**:
* **文件**: `server/trainers/mlstm_trainer.py`, `server/trainers/transformer_trainer.py`
* **操作**: 尝试在训练器层面使用 `squeeze` 调整 `y_batch` 的维度来匹配 `outputs`
```diff
- loss = criterion(outputs, y_batch)
+ loss = criterion(outputs, y_batch.squeeze(-1) if y_batch.dim() == 3 else y_batch)
```
* **结果**: 此修改导致了新的运行时错误 `UserWarning: Using a target size (torch.Size([32, 3])) that is different to the input size (torch.Size([32, 3, 1]))`,证明了修复方向错误,但帮助定位了问题的真正根源。
### 阶段十:根治维度不匹配问题
* **问题**: 深入分析阶段九的错误后,确认了问题的根源。
* **根本原因**: `server/models/mlstm_model.py` 中的 `MLSTMTransformer` 模型,其 `forward` 方法的最后一层输出了一个多余的维度,导致其输出形状为 `(B, H, 1)`,而并非期望的 `(B, H)`
* **正确的解决方案 (端到端维度一致性)**:
1. **修复模型层 (治本)**:
* **文件**: `server/models/mlstm_model.py`
* **位置**: `MLSTMTransformer``forward` 方法。
* **操作**: 在 `output_layer` 之后增加 `.squeeze(-1)`,将模型输出的维度从 `(B, H, 1)` 修正为 `(B, H)`
```diff
- return self.output_layer(decoder_outputs)
+ return self.output_layer(decoder_outputs).squeeze(-1)
```
2. **净化训练器层 (治标)**:
* **文件**: `server/trainers/mlstm_trainer.py`, `server/trainers/transformer_trainer.py`
* **操作**: 撤销了阶段九的错误修改,恢复为最直接的损失计算 `loss = criterion(outputs, y_batch)`
3. **优化评估逻辑**:
* **文件**: `server/trainers/mlstm_trainer.py`, `server/trainers/transformer_trainer.py`
* **操作**: 简化了模型评估部分的反归一化逻辑,使其更清晰、更直接地处理 `(样本数, 预测步长)` 形状的数据。
```diff
- test_pred_inv = scaler_y.inverse_transform(test_pred.reshape(-1, 1)).flatten()
- test_true_inv = scaler_y.inverse_transform(testY.reshape(-1, 1)).flatten()
+ test_pred_inv = scaler_y.inverse_transform(test_pred)
+ test_true_inv = scaler_y.inverse_transform(test_true)
```
**最终结果**: 通过记录整个调试过程,我们不仅修复了问题,还理解了其根本原因。通过在模型源头修正维度,并在整个数据流中保持维度一致性,彻底解决了训练失败的问题。代码现在更简洁、健壮,并遵循了良好的设计实践。
---
**日期**: 2025-07-14 15:30
**主题**: 根治模型维度错误并统一数据流 (完整调试过程)
### 阶段九:错误的修复尝试 (记录备查)
* **问题**: 所有模型训练完成后,评估指标 `R²` 始终为0.0`MAPE` 始终为0.00%。
* **初步分析**: 怀疑损失函数计算时,`outputs``y_batch` 维度不匹配。
* **错误的假设**: 当时错误地认为是 `y_batch` 的维度有问题,而 `outputs` 的维度是正确的。
* **错误的修复**:
* **文件**: `server/trainers/mlstm_trainer.py`, `server/trainers/transformer_trainer.py`
* **操作**: 尝试在训练器层面使用 `squeeze` 调整 `y_batch` 的维度来匹配 `outputs`
```diff
- loss = criterion(outputs, y_batch)
+ loss = criterion(outputs, y_batch.squeeze(-1) if y_batch.dim() == 3 else y_batch)
```
* **结果**: 此修改导致了新的运行时错误 `UserWarning: Using a target size (torch.Size([32, 3])) that is different to the input size (torch.Size([32, 3, 1]))`,证明了修复方向错误,但帮助定位了问题的真正根源。
### 阶段十:根治维度不匹配问题
* **问题**: 深入分析阶段九的错误后,确认了问题的根源在于模型输出维度。
* **根本原因**: `server/models/mlstm_model.py` 中的 `MLSTMTransformer` 模型,其 `forward` 方法的最后一层输出了一个多余的维度,导致其输出形状为 `(B, H, 1)`,而并非期望的 `(B, H)`
* **正确的解决方案 (端到端维度一致性)**:
1. **修复模型层 (治本)**:
* **文件**: `server/models/mlstm_model.py`
* **位置**: `MLSTMTransformer``forward` 方法。
* **操作**: 在 `output_layer` 之后增加 `.squeeze(-1)`,将模型输出的维度从 `(B, H, 1)` 修正为 `(B, H)`
2. **净化训练器层 (治标)**:
* **文件**: `server/trainers/mlstm_trainer.py`, `server/trainers/transformer_trainer.py`
* **操作**: 撤销了阶段九的错误修改,恢复为最直接的损失计算 `loss = criterion(outputs, y_batch)`
3. **优化评估逻辑**:
* **文件**: `server/trainers/mlstm_trainer.py`, `server/trainers/transformer_trainer.py`
* **操作**: 简化了模型评估部分的反归一化逻辑,使其更清晰、更直接地处理 `(样本数, 预测步长)` 形状的数据。
### 阶段十一:最终修复与逻辑统一
* **问题**: 在应用阶段十的修复后训练仍然失败。mLSTM出现维度反转错误 (`target size (B, H, 1)` vs `input size (B, H)`)而Transformer则出现评估错误 (`'numpy.ndarray' object has no attribute 'numpy'`)。
* **分析**:
1. **维度反转根源**: 问题的最终根源在 `server/utils/data_utils.py``create_dataset` 函数。它在创建目标数据集 `dataY` 时,错误地保留了一个多余的维度,导致 `y_batch` 的形状变为 `(B, H, 1)`
2. **评估Bug**: 在 `mlstm_trainer.py``transformer_trainer.py` 的评估部分,代码 `test_true = testY.numpy()` 是错误的,因为 `testY` 已经是Numpy数组。
* **最终解决方案 (端到端修复)**:
1. **修复数据加载层 (治本)**:
* **文件**: `server/utils/data_utils.py`
* **位置**: `create_dataset` 函数。
* **操作**: 修改 `dataY.append(y)``dataY.append(y.flatten())`,从源头上确保 `y` 标签的维度是正确的 `(B, H)`
2. **修复训练器评估层**:
* **文件**: `server/trainers/mlstm_trainer.py`, `server/trainers/transformer_trainer.py`
* **位置**: 模型评估部分。
* **操作**: 修正 `test_true = testY.numpy()``test_true = testY`,解决了属性错误。
**最终结果**: 通过记录并分析整个调试过程(阶段九到十一),我们最终定位并修复了从数据加载、模型设计到训练器评估的整个流程中的维度不一致问题。代码现在更加简洁、健壮,并遵循了端到端维度一致的良好设计实践。
---
**日期**: 2025-07-14 15:34
**主题**: 扩展维度修复至Transformer模型
### 阶段十二:统一所有模型的输出维度
* **问题**: 在修复 `mLSTM` 模型后,`Transformer` 模型的训练仍然因为完全相同的维度不匹配问题而失败。
* **分析**: `server/models/transformer_model.py` 中的 `TimeSeriesTransformer` 类也存在与 `mLSTM` 相同的设计缺陷,其 `forward` 方法的输出维度为 `(B, H, 1)` 而非 `(B, H)`
* **解决方案**:
1. **修复Transformer模型层**:
* **文件**: `server/models/transformer_model.py`
* **位置**: `TimeSeriesTransformer``forward` 方法。
* **操作**: 在 `output_layer` 之后增加 `.squeeze(-1)`,将模型输出的维度从 `(B, H, 1)` 修正为 `(B, H)`
```diff
- return self.output_layer(decoder_outputs)
+ return self.output_layer(decoder_outputs).squeeze(-1)
```
**最终结果**: 通过将维度修复方案应用到所有相关的模型文件,我们确保了整个系统的模型层都遵循了统一的、正确的输出维度标准。至此,所有已知的维度相关问题均已从根源上解决。
---
**日期**: 2025-07-14 16:10
**主题**: 修复“全局模型训练-所有药品”模式下的训练失败问题
### 问题描述
在“全局模型训练”页面,当选择“所有药品”进行训练时,后端日志显示 `聚合全局数据失败: 没有找到产品 unknown 的销售数据`,导致训练任务失败。
### 根本原因
1. **API层 (`server/api.py`)**: 在处理全局训练请求时,如果前端未提供 `product_id`对应“所有药品”选项API层会执行 `product_id or "unknown"`错误地将产品ID设置为字符串 `"unknown"`
2. **预测器层 (`server/core/predictor.py`)**: `train_model` 方法接收到无效的 `product_id="unknown"` 后,在 `training_mode='global'` 分支下,直接将其传递给数据聚合函数。
3. **数据工具层 (`server/utils/multi_store_data_utils.py`)**: `aggregate_multi_store_data` 函数缺少处理“真正”全局聚合(即不按任何特定产品或店铺过滤)的逻辑,当收到 `product_id="unknown"` 时,它会尝试按一个不存在的产品进行过滤,最终导致失败。
### 解决方案 (遵循现有设计模式)
为了在不影响现有功能的前提下修复此问题,采用了与历史修复类似的、在中间层进行逻辑适配的策略。
1. **修改 `server/utils/multi_store_data_utils.py`**:
* **位置**: `aggregate_multi_store_data` 函数。
* **操作**: 扩展了函数功能。
* **内容**: 增加了新的逻辑分支。当 `product_id``store_id` 参数都为 `None` 时,函数现在会加载**所有**数据进行聚合,以支持真正的全局模型训练。
```python
# ...
elif product_id:
# 按产品聚合...
else:
# 真正全局聚合:加载所有数据
df = load_multi_store_data(file_path)
if len(df) == 0:
raise ValueError("数据文件为空,无法进行全局聚合")
grouping_entity = "所有产品"
```
* **原因**: 使数据聚合函数的功能更加完整和健壮,能够服务于真正的全局训练场景,同时不影响其原有的按店铺或按产品的聚合功能。
2. **修改 `server/core/predictor.py`**:
* **位置**: `train_model` 方法,`training_mode == 'global'` 的逻辑分支内。
* **操作**: 增加了对 `product_id == 'unknown'` 的特殊处理。
* **内容**:
```python
if product_id == 'unknown':
product_data = aggregate_multi_store_data(
product_id=None, # 传递None以触发真正的全局聚合
# ...
)
# 将product_id设置为一个有意义的标识符
product_id = 'all_products'
else:
# ...原有的按单个产品聚合的逻辑...
```
* **原因**: 在核心预测器层面拦截无效的 `"unknown"` ID并将其正确地解释为“聚合所有产品数据”的意图。通过向聚合函数传递 `product_id=None` 来调用新增强的全局聚合功能,并用一个有意义的标识符 `all_products` 来命名模型,确保了后续流程的正确执行。
**最终结果**: 通过这两处修改,系统现在可以正确处理“全局模型-所有药品”的训练请求,聚合所有产品的销售数据来训练一个通用的全局模型,彻底解决了该功能点的训练失败问题。
---
**日期**: 2025-07-14
**主题**: UI导航栏重构
### 描述
根据用户请求,对左侧功能导航栏进行了调整。
### 主要改动
1. **删除“数据管理”**:
* 从 `UI/src/App.vue` 的导航菜单中移除了“数据管理”项。
* 从 `UI/src/router/index.js` 中删除了对应的 `/data` 路由。
* 删除了视图文件 `UI/src/views/DataView.vue`
2. **提升“店铺管理”**:
* 将“店铺管理”菜单项在 `UI/src/App.vue` 中的位置提升,以填补原“数据管理”的位置,使其在导航中更加突出。
### 涉及文件
* `UI/src/App.vue`
* `UI/src/router/index.js`
* `UI/src/views/DataView.vue` (已删除)
**按药品模型预测**
---
**日期**: 2025-07-14
**主题**: 修复导航菜单高亮问题
### 描述
修复了首次进入或刷新页面时,左侧导航菜单项与当前路由不匹配导致不高亮的问题。
### 主要改动
* **文件**: `UI/src/App.vue`
* **修改**:
1. 引入 `useRoute``computed`
2. 创建了一个计算属性 `activeMenu`,其值动态地等于当前路由的路径 (`route.path`)。
3. 将 `el-menu` 组件的 `:default-active` 属性绑定到 `activeMenu`
### 结果
确保了导航菜单的高亮状态始终与当前页面的URL保持同步。
---
**日期**: 2025-07-15
**主题**: 修复硬编码文件路径问题,提高项目可移植性
### 问题描述
项目在从一台计算机迁移到另一台时,由于数据文件路径被硬编码在代码中,导致程序无法找到数据文件而运行失败。
### 根本原因
多个Python文件`predictor.py`, `multi_store_data_utils.py`)中直接写入了相对路径 `'data/timeseries_training_data_sample_10s50p.parquet'` 作为默认值。这种方式在不同运行环境下(如从根目录运行 vs 从子目录运行)会产生路径解析错误。
### 解决方案:集中配置,统一管理
1. **修改 `server/core/config.py` (核心)**:
* 动态计算并定义了一个全局变量 `PROJECT_ROOT`,它始终指向项目的根目录。
* 基于 `PROJECT_ROOT`,使用 `os.path.join` 创建了一个跨平台的、绝对的默认数据路径 `DEFAULT_DATA_PATH` 和模型保存路径 `DEFAULT_MODEL_DIR`
* 这确保了无论从哪个位置执行代码,路径总能被正确解析。
2. **修改 `server/utils/multi_store_data_utils.py`**:
* 从 `server/core/config` 导入 `DEFAULT_DATA_PATH`
* 将所有数据加载函数的 `file_path` 参数的默认值从硬编码的字符串改为 `None`
* 在函数内部,如果 `file_path``None`,则自动使用导入的 `DEFAULT_DATA_PATH`
* 移除了原有的、复杂的、为了猜测正确路径而编写的冗余代码。
3. **修改 `server/core/predictor.py`**:
* 同样从 `server/core/config` 导入 `DEFAULT_DATA_PATH`
* 在初始化 `PharmacyPredictor` 时,如果未提供数据路径,则使用导入的 `DEFAULT_DATA_PATH` 作为默认值。
### 最终结果
通过将数据源路径集中到唯一的配置文件中进行管理,彻底解决了因硬编码路径导致的可移植性问题。项目现在可以在任何环境下可靠地运行。
---
### 未来如何修改数据源(例如,连接到服务器数据库)
本次重构为将来更换数据源打下了坚实的基础。操作非常简单:
1. **定位配置文件**: 打开 `server/core/config.py` 文件。
2. **修改数据源定义**:
* **当前 (文件)**:
```python
DEFAULT_DATA_PATH = os.path.join(PROJECT_ROOT, 'data', 'timeseries_training_data_sample_10s50p.parquet')
```
* **未来 (数据库示例)**:
您可以将这行替换为数据库连接字符串,或者添加新的数据库配置变量。例如:
```python
# 注释掉或删除旧的文件路径配置
# DEFAULT_DATA_PATH = ...
# 新增数据库连接配置
DATABASE_URL = "postgresql://user:password@your_server_ip:5432/your_database_name"
```
3. **修改数据加载逻辑**:
* **定位数据加载函数**: 打开 `server/utils/multi_store_data_utils.py`
* **修改 `load_multi_store_data` 函数**:
* 引入数据库连接库(如 `sqlalchemy``psycopg2`)。
* 修改函数逻辑,使其使用 `config.py` 中的 `DATABASE_URL` 来连接数据库并执行SQL查询来获取数据而不是读取文件。
* **示例**:
```python
from sqlalchemy import create_engine
from core.config import DATABASE_URL # 导入新的数据库配置
def load_multi_store_data(...):
# ...
engine = create_engine(DATABASE_URL)
query = "SELECT * FROM sales_data" # 根据需要构建查询
df = pd.read_sql(query, engine)
# ... 后续处理逻辑保持不变 ...
```
通过以上步骤,您就可以在不改动项目其他任何部分的情况下,轻松地将数据源从本地文件切换到服务器数据库。
---
**日期**: 2025-07-15 11:43
**主题**: 修复因PyTorch版本不兼容导致的训练失败问题
### 问题描述
在修复了路径和依赖问题后,在某些机器上运行模型训练时,程序因 `TypeError: ReduceLROnPlateau.__init__() got an unexpected keyword argument 'verbose'` 而崩溃。但在本地开发机上运行正常。
### 根本原因
此问题是典型的**环境不一致**导致的兼容性错误。
1. **PyTorch版本差异**: 本地开发环境安装了较旧版本的PyTorch其学习率调度器 `ReduceLROnPlateau` 支持 `verbose` 参数(用于在学习率变化时打印日志)。
2. **新环境**: 在其他计算机或新创建的虚拟环境中安装了较新版本的PyTorch。在新版本中`ReduceLROnPlateau``verbose` 参数已被移除。
3. **代码问题**: `server/trainers/mlstm_trainer.py``server/trainers/transformer_trainer.py` 的代码中,在创建 `ReduceLROnPlateau` 实例时硬编码了 `verbose=True` 参数导致在新版PyTorch环境下调用时出现 `TypeError`
### 解决方案:移除已弃用的参数
1. **全面排查**: 检查了项目中所有训练器文件 (`mlstm_trainer.py`, `transformer_trainer.py`, `kan_trainer.py`, `tcn_trainer.py`)。
2. **精确定位**: 确认只有 `mlstm_trainer.py``transformer_trainer.py` 使用了 `ReduceLROnPlateau` 并传递了 `verbose` 参数。
3. **执行修复**:
* **文件**: `server/trainers/mlstm_trainer.py``server/trainers/transformer_trainer.py`
* **位置**: `ReduceLROnPlateau` 的初始化调用处。
* **操作**: 删除了 `verbose=True` 参数。
```diff
- scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', ..., verbose=True)
+ scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', ...)
```
* **原因**: 移除这个在新版PyTorch中已不存在的参数可以从根本上解决 `TypeError`并确保代码在不同版本的PyTorch环境中都能正常运行。此修改不影响学习率调度器的核心功能。
### 最终结果
通过移除已弃用的 `verbose` 参数,彻底解决了由于环境差异导致的版本兼容性问题,确保了项目在所有目标机器上都能稳定地执行训练任务。
---
**日期**: 2025-07-15 14:05
**主题**: 仪表盘UI调整
### 描述
根据用户请求,将仪表盘上的“数据管理”卡片替换为“店铺管理”。
### 主要改动
* **文件**: `UI/src/views/DashboardView.vue`
* **修改**:
1. 在 `featureCards` 数组中,将原“数据管理”的对象修改为“店铺管理”。
2. 更新了卡片的 `title`, `description`, `icon``path`,使其指向店铺管理页面 (`/store-management`)。
3. 在脚本中导入了新的 `Shop` 图标。
### 结果
仪表盘现在直接提供到“店铺管理”页面的快捷入口,提高了操作效率。
---
### 2025-07-15: 模型保存与管理系统重构
**核心目标**: 根据 `xz训练模型保存规则.md` 的规范重构整个模型的保存、读取、版本管理和API交互逻辑以提高系统的可维护性、健壮性和可扩展性。
**1. 修改 `server/utils/model_manager.py`**
* **内容**: 完全重写了 `ModelManager` 类。
* **版本管理**: 引入了 `saved_models/versions.json` 文件作为版本号的唯一来源,并实现了线程安全的读写操作。
* **路径构建**: 实现了 `get_model_version_path` 方法,用于根据训练模式、范围、类型和版本生成结构化的目录路径 (e.g., `saved_models/product/{scope}/{type}/v{N}/`)。
* **产物保存**: 实现了 `save_model_artifact` 方法,用于将模型、检查点、图表和元数据等所有训练产物统一保存到指定的版本目录中。
* **模型发现**: 重写了 `list_models` 方法,使其通过扫描目录结构来发现和列出所有模型及其元数据。
**2. 修改 `server/trainers/mlstm_trainer.py`**
* **内容**: 集成新的 `ModelManager`
* 移除了旧的、手动的 `save_checkpoint``load_checkpoint` 函数。
* 在训练开始时,调用 `model_manager` 来获取模型的唯一标识符和下一个版本号。
* 在训练过程中,统一使用 `model_manager.save_model_artifact()` 来保存所有产物(`model.pth`, `checkpoint_best.pth`, `loss_curve.png`, `metadata.json`)。
* 在训练成功后,调用 `model_manager.update_version()` 来更新 `versions.json`
**3. 修改 `server/core/config.py`**
* **内容**: 清理废弃的函数。
* 删除了 `get_next_model_version`, `get_model_file_path`, `get_model_versions`, `get_latest_model_version`, 和 `save_model_version_info` 等所有与旧的、基于文件名的版本管理相关的函数。
**4. 修改 `server/core/predictor.py`**
* **内容**: 解耦与旧路径逻辑的依赖。
* 更新了 `train_model` 方法,将版本和路径管理的职责完全下放给具体的训练器。
* 更新了 `predict` 方法,使其调用新的 `load_model_and_predict`,并传递标准的模型版本目录路径。
**5. 修改 `server/predictors/model_predictor.py`**
* **内容**: 适配新的模型加载逻辑。
* 重写了 `load_model_and_predict` 函数,使其接受一个模型版本目录路径 (`model_version_path`) 作为输入。
* 函数现在从该目录下的 `metadata.json` 读取元数据,并从 `model.pth` 加载模型和 `scaler` 对象。
**6. 修改 `server/api.py`**
* **内容**: 更新API端点以适应新的模型管理系统。
* `/api/models`: 调用 `model_manager.list_models()` 来获取模型列表。
* `/api/models/<model_id>`: 更新详情和删除逻辑,以处理基于目录的结构。
* `/api/prediction`: 更新调用 `predictor.predict()` 的方式。
* `/api/training`: 更新了数据库保存逻辑,现在向 `model_versions` 表中存入的是模型版本目录的路径。
---
### 2025-07-15 (续): 训练器与核心调用层重构
**核心目标**: 将新的 `ModelManager` 统一应用到项目中所有剩余的模型训练器,并重构核心调用逻辑,确保整个训练链路的架构一致性。
**1. 修改 `server/trainers/kan_trainer.py`**
* **内容**: 完全重写了 `kan_trainer.py`
* **适配接口**: 函数签名与 `mlstm_trainer` 对齐,增加了 `socketio`, `task_id`, `patience` 等参数。
* **集成 `ModelManager`**: 移除了所有旧的、手动的保存逻辑,改为在训练开始时调用 `model_manager` 获取版本号和路径。
* **标准化产物保存**: 所有产物(模型、元数据、检查点、损失曲线)均通过 `model_manager.save_model_artifact()` 保存。
* **增加健壮性**: 引入了早停Early Stopping和保存最佳检查点Best Checkpoint的逻辑。
**2. 修改 `server/trainers/tcn_trainer.py`**
* **内容**: 完全重写了 `tcn_trainer.py`,应用了与 `kan_trainer` 完全相同的重构模式。
* 移除了旧的 `save_checkpoint` 辅助函数和基于 `core.config` 的版本管理。
* 全面转向使用 `model_manager` 进行版本控制和文件保存。
* 统一了函数签名和进度反馈逻辑。
**3. 修改 `server/trainers/transformer_trainer.py`**
* **内容**: 完全重写了 `transformer_trainer.py`,完成了对所有训练器的统一重构。
* 移除了所有遗留的、基于文件名的路径拼接和保存逻辑。
* 实现了与其它训练器一致的、基于 `ModelManager` 的标准化训练流程。
**4. 修改 `server/core/predictor.py`**
* **内容**: 对核心预测器类 `PharmacyPredictor` 进行了彻底重构。
* **统一调用接口**: `train_model` 方法现在以完全一致的方式调用所有(`mlstm`, `kan`, `tcn`, `transformer`)训练器。
* **移除旧逻辑**: 删除了 `_parse_model_filename` 等所有基于文件名解析的旧方法。
* **适配 `ModelManager`**: `list_models``delete_model` 等方法现在直接调用 `model_manager` 的相应功能,不再自己实现逻辑。
* **简化 `predict`**: 预测方法现在直接接收标准化的模型版本路径 (`model_version_path`) 作为输入,逻辑更清晰。
- **Python**: 3.x
- **主要库**:
- Flask
- Flask-SocketIO
- Flasgger
- pandas
- numpy
- torch
- scikit-learn
- matplotlib
- **启动命令**: `python server/api.py`

View File

@ -4,30 +4,47 @@
我们已经从“一个文件包含所有信息”的模式,转向了“目录结构本身就是信息”的模式。
基本结构:
```
saved_models/
├── versions.json # 记录所有模型最新版本号的“注册表”
├── product/
│ ├── all/
│ │ ├── MLSTM/
│ │ │ ├── v1/
│ │ │ │ ├── model.pth
│ │ │ │ ├── metadata.json
│ │ │ │ ├── loss_curve.png
│ │ │ │ └── checkpoint_best.pth
│ │ │ └── v2/
│ │ │ └── ...
│ │ └── TCN/
│ │ └── v1/
│ │ └── ...
│ └── {product_id}/
│ └── ...
├── user/
│ └── ...
└── versions.json
txt
│ └── {product_id}_{scope}/
│ └── {model_type}/
│ └── v{N}/
│ ├── model.pth # 最终用于预测的模型文件
│ ├── checkpoint_best.pth # 训练中性能最佳的检查点
│ ├── metadata.json # 包含训练参数、scaler等元数据
│ └── loss_curve.png # 训练过程的损失曲线图
├── store/
│ └── {store_id}_{scope}/
│ └── {model_type}/
│ └── v{N}/
│ ├── model.pth
│ ├── checkpoint_best.pth
│ ├── metadata.json
│ └── loss_curve.png
└── global/
├── all/{aggregation_method}/{model_type}/v{N}/
│ ├── model.pth
│ ├── checkpoint_best.pth
│ ├── metadata.json
│ └── loss_curve.png
├── stores/{store_id}/{aggregation_method}/{model_type}/v{N}/
│ ├── model.pth
│ ├── checkpoint_best.pth
│ ├── metadata.json
│ └── loss_curve.png
├── products/{product_id}/{aggregation_method}/{model_type}/v{N}/
│ ├── model.pth
│ ├── checkpoint_best.pth
│ ├── metadata.json
│ └── loss_curve.png
└── custom/{store_id}/{product_id}/{aggregation_method}/{model_type}/v{N}/
├── model.pth
├── checkpoint_best.pth
├── metadata.json
└── loss_curve.png
```
关键点解读:
@ -119,18 +136,18 @@ checkpoint_best.pth: 训练过程中验证集上表现最好的模型检查点
#### 3. 全局训练 (Global Training)
* **目录结构**: `saved_models/global/{scope_path}/{aggregation_method}/{model_type}/v{N}/`
* **目录结构**: `saved_models/global/{scope}/{aggregation_method}/{model_type}/v{N}/`
* **路径解析**:
* `global`: 表示这是“全局”训练模式。
* `{scope_path}`: 描述训练所用数据的范围,结构比较灵活
* `all`: 代表所有店铺所有药品。
* `stores/{store_id}`: 代表选择了特定的店铺。
* `products/{product_id}`: 代表选择了特定的药品。
* `custom/{store_id}/{product_id}`: 代表自定义范围,同时指定了店铺和药品。
* `{scope}`: 描述训练所用数据的范围,有以下几种情况
* `all`: 代表所有店铺所有药品
* `stores/{store_id}`: 代表选择了特定的店铺
* `products/{product_id}`: 代表选择了特定的药品
* `custom/{store_id}/{product_id}`: 代表自定义范围同时指定了店铺和药品。
* `{aggregation_method}`: 数据的聚合方式 (例如 `sum`, `mean`)。
* `{model_type}`: 模型的类型。
* `v{N}`: 模型的版本号。
* **文件夹内容**: 与“按药品训练”模式相同。
* **文件夹内容**: 与“按药品训练”模式相同,包含 `model.pth`, `metadata.json` 等标准产物
### 总结
@ -152,20 +169,6 @@ checkpoint_best.pth: 训练过程中验证集上表现最好的模型检查点
* **损失曲线图**: `loss_curve.png`
* **训练元数据**: `metadata.json` (包含训练参数、指标等详细信息)
**示例路径:**
1. **按药品训练 (P001, 所有店铺, mlstm, v2)**:
* **目录**: `saved_models/product/P001_all/mlstm/v2/`
* **最终模型**: `saved_models/product/P001_all/mlstm/v2/model.pth`
* **损失曲线**: `saved_models/product/P001_all/mlstm/v2/loss_curve.png`
2. **按店铺训练 (S001, 指定药品P002, transformer, v1)**:
* **目录**: `saved_models/store/S001_P002/transformer/v1/`
* **最终模型**: `saved_models/store/S001_P002/transformer/v1/model.pth`
3. **全局训练 (所有数据, sum聚合, kan, v5)**:
* **目录**: `saved_models/global/all/sum/kan/v5/`
* **最终模型**: `saved_models/global/all/sum/kan/v5/model.pth`
#### 二、 文件读取规则