仿照feature = pd.DataFrame( Parallel(n_jobs=16, backend="multiprocessing")(delayed(processing_feature)(f) for f in tqdm(all_ids)))的方法,使用Parallel和delayed优化下面的代码:mse_weights, ce_weights, soft_weights = nn.fit( train_feature, train_target_lb, gamma, batch_num)
时间: 2024-04-10 14:27:56 浏览: 155
你可以使用`Parallel`和`delayed`来优化下面的代码:
```python
from joblib import Parallel, delayed
def fit_wrapper(train_feature, train_target_lb, gamma, batch_num):
nn = YourNeuralNetwork() # 替换为你的神经网络类
mse_weights, ce_weights, soft_weights = nn.fit(train_feature, train_target_lb, gamma, batch_num)
return mse_weights, ce_weights, soft_weights
results = Parallel(n_jobs=16, backend="multiprocessing")(
delayed(fit_wrapper)(train_feature, train_target_lb, gamma, batch_num)
for _ in tqdm(range(len(all_ids)))
)
mse_weights_list, ce_weights_list, soft_weights_list = zip(*results)
mse_weights = np.concatenate(mse_weights_list)
ce_weights = np.concatenate(ce_weights_list)
soft_weights = np.concatenate(soft_weights_list)
```
这里,我们首先定义了一个包装函数`fit_wrapper`,它调用你的神经网络类并返回相应的权重。然后,我们使用`Parallel`和`delayed`来并行化调用`fit_wrapper`函数,这样每个任务可以在不同的CPU核心上同时运行。最后,我们提取各个任务的结果,并将它们连接起来得到最终的权重结果。
阅读全文