実現したいこと
Google Colabでgpuを使用できるようにしたい。
前提
グリッドサーチを行いたいのですが、gpuを使用できません。
ランタイムはgpuに変更しています。
また、gpu使用時間は問題ありません。
該当のソースコード
Python
12import lightgbm as lgb 3from sklearn.datasets import fetch_california_housing 4from sklearn.model_selection import GridSearchCV 5from sklearn.metrics import mean_squared_error 6from sklearn.model_selection import train_test_split 7import numpy as np 8import pandas as pd 9import time 10 11# サンプルデータの追加12fetch_california_housing = fetch_california_housing()13X, y = fetch_california_housing.data, fetch_california_housing.target 14 15X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)16 17 18# LightGBMのモデルを定義する19model = lgb.LGBMRegressor()20 21# ハイパーパラメータのグリッドサーチを定義する22param_grid = {23 'num_leaves': [20, 30, 40],24 'learning_rate': [0.05, 0.1, 0.2],25 'n_estimators': [50, 100, 200]26}27 28# グリッドサーチを実行する29grid_search = GridSearchCV(model, param_grid=param_grid, cv=5, n_jobs=-2, verbose=2)30start = time.time()31grid_search.fit(X_train, y_train)32print("グリッドサーチの処理時間:", time.time() - start)33 34# 最適なハイパーパラメータを取得する35best_params = grid_search.best_params_ 36print("最適なハイパーパラメータ:", best_params)37 38# 最適なハイパーパラメータでモデルをトレーニングする39best_model = lgb.LGBMRegressor(**best_params)40best_model.fit(X_train, y_train)41 42# テストデータで予測を行う43y_pred = best_model.predict(X_val)44mse = mean_squared_error(y_val, y_pred)45print("MSE:", mse)
試したこと
gpuのラインタイムに変更。
以下のコードを実装し、gpuの稼働を確認
python
1import tensorflow as tf 2print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))3 4# Num GPUs Available: 1
補足情報(FW/ツールのバージョンなど)
Google Colab 無償版
0 コメント