交叉验证中n_jobs=-1并行运算

scikit_learn包中的cross_val_score()是支持并行运算,但这并不是说只要让n_jobs=-1就能让CPU使用率接近100%。这要取决于交叉验证的折数cv,假如折数cv是n,这意味着最多只能使用n个物理CPU。

score1 = cross_val_score(clf, x_train, y_train, cv=5, scoring='accuracy', n_jobs=-1).mean()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在LSTM训练模型使用n_jobs=-1的Python代码如下: ```python from keras.models import Sequential from keras.layers import LSTM, Dense from sklearn.model_selection import GridSearchCV from sklearn.metrics import mean_squared_error from sklearn.preprocessing import MinMaxScaler import numpy as np # Load the data data = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [2, 4, 6, 8, 10, 12, 14, 16, 18, 20], [3, 6, 9, 12, 15, 18, 21, 24, 27, 30], [4, 8, 12, 16, 20, 24, 28, 32, 36, 40]]) # Normalize the data scaler = MinMaxScaler(feature_range=(0, 1)) data = scaler.fit_transform(data) # Split data into input and output X, y = data[:, :-1], data[:, -1] # Reshape input data into [samples, timesteps, features] X = X.reshape((X.shape[0], X.shape[1], 1)) # Define the model model = Sequential() model.add(LSTM(50, input_shape=(X.shape[1], X.shape[2]))) model.add(Dense(1)) model.compile(loss='mse', optimizer='adam') # Define the grid search parameters param_grid = {'batch_size': [1, 2, 3], 'epochs': [50, 100, 150]} # Create a grid search object grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1) # Fit the grid search object to the data grid_result = grid.fit(X, y) # Print the best parameters and score print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_)) ``` 在这个例子,我们使用了GridSearchCV来搜索最佳的batch_size和epochs参数配置。我们将n_jobs参数设置为-1,以使用所有可用的CPU核心进行并行处理。这可以加快搜索过程的速度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值