/opt/conda/lib/python3.10/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
from .autonotebook import tqdm as notebook_tqdm
import torch
import random
import optuna
import numpy as np
import pandas as pd
#データを準備しました。
data = np.array([[8,3,83.74,18,22],[5,2,75.72,19,19],[7,2,31.47,46,7.8],[18,2,46.62,36,8],[8,3,41.02,49,8],[8,1,70.43,25,15.8],[8,1,70.43,25,15.8],[12,1,48.02,3,12.5],[10,4,58.57,36,11.8]])
df = pd.DataFrame(data,columns=['toho','kaisu','hirosa','chiku','chinryo'])
#賃料だけのデータ
y = df["chinryo"]
#賃料以外のデータ
X = df.drop(columns=["chinryo"], axis=1)
from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=1)
#シード固定
seed = 1
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
def objective(trial):
#中間レイヤー数x
x = trial.suggest_int('x', 4, 20)
model2 = torch.nn.Sequential()
model2.add_module('fc1', torch.nn.Linear(4, x))
model2.add_module('relu', torch.nn.ReLU())
model2.add_module('fc2', torch.nn.Linear(x, 1))
optimizer_name = trial.suggest_categorical('optimizer_name', ['SGD', 'Adam'])
learning_rate = trial.suggest_float('learning_rate', 0.01, 1.)
if optimizer_name == "SGD":
optimizer = torch.optim.SGD(model2.parameters(), lr = learning_rate)
else:
optimizer = torch.optim.Adam(model2.parameters(), lr = learning_rate)
#損失関数選ぶ
lossf = torch.nn.MSELoss()
#入力値とターゲット
input = torch.Tensor(X_train.values)
target = torch.Tensor([[i] for i in y_train.values])
#トレーニング
for _ in range(100):
loss = lossf(model2(input), target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
X_test = np.array([[9,5,58.3,34]])
pred = model2(torch.Tensor(X_test))
return (pred - 11.5)**2
study = optuna.create_study()
study.optimize(objective, n_trials=20)
[32m[I 2022-08-30 12:00:04,059][0m A new study created in memory with name: no-name-1f08f809-d9e0-4a77-90dc-787d52f43b03[0m
[32m[I 2022-08-30 12:00:04,155][0m Trial 0 finished with value: 5.479587078094482 and parameters: {'x': 6, 'optimizer_name': 'SGD', 'learning_rate': 0.044333181067415524}. Best is trial 0 with value: 5.479587078094482.[0m
[32m[I 2022-08-30 12:00:04,307][0m Trial 1 finished with value: 8.065937042236328 and parameters: {'x': 5, 'optimizer_name': 'Adam', 'learning_rate': 0.6226217508662838}. Best is trial 0 with value: 5.479587078094482.[0m
[32m[I 2022-08-30 12:00:04,389][0m Trial 2 finished with value: 8.660406112670898 and parameters: {'x': 7, 'optimizer_name': 'SGD', 'learning_rate': 0.5020381290904798}. Best is trial 0 with value: 5.479587078094482.[0m
[32m[I 2022-08-30 12:00:04,467][0m Trial 3 finished with value: 8.660411834716797 and parameters: {'x': 13, 'optimizer_name': 'SGD', 'learning_rate': 0.46807361305790574}. Best is trial 0 with value: 5.479587078094482.[0m
[32m[I 2022-08-30 12:00:04,542][0m Trial 4 finished with value: 8.660406112670898 and parameters: {'x': 8, 'optimizer_name': 'SGD', 'learning_rate': 0.8074981983899306}. Best is trial 0 with value: 5.479587078094482.[0m
[32m[I 2022-08-30 12:00:04,662][0m Trial 5 finished with value: 3.2180960178375244 and parameters: {'x': 9, 'optimizer_name': 'Adam', 'learning_rate': 0.46194632516596684}. Best is trial 5 with value: 3.2180960178375244.[0m
[32m[I 2022-08-30 12:00:04,737][0m Trial 6 finished with value: 8.660406112670898 and parameters: {'x': 18, 'optimizer_name': 'SGD', 'learning_rate': 0.691990889952687}. Best is trial 5 with value: 3.2180960178375244.[0m
[32m[I 2022-08-30 12:00:04,797][0m Trial 7 finished with value: 8.660406112670898 and parameters: {'x': 7, 'optimizer_name': 'SGD', 'learning_rate': 0.5674500614264276}. Best is trial 5 with value: 3.2180960178375244.[0m
[32m[I 2022-08-30 12:00:04,947][0m Trial 8 finished with value: 0.00038580328691750765 and parameters: {'x': 9, 'optimizer_name': 'Adam', 'learning_rate': 0.7956685276125515}. Best is trial 8 with value: 0.00038580328691750765.[0m
Display all output >>>