質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Q&A

0回答

397閲覧

CNN hyperopt 最適化

qingfengliu

総合スコア0

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

0グッド

0クリップ

投稿2020/08/12 05:38

前提・実現したいこと

hyperoptでDNNを最適化したいです。でもデータはfunctionの形ではなくarrayで代入したいです。

発生している問題・エラーメッセージ

Unexpected error: <class 'IndentationError'> Traceback (most recent call last): File "/Users/qingfengliu/Dropbox/Feng_and_Liu/DMC/DMC_cluster/output/dnn_working.py", line 111, in <module> Loss_test = dnn_regression(data, Random_seed, Sample_size) File "/Users/qingfengliu/Dropbox/Feng_and_Liu/DMC/DMC_cluster/output/dnn_working.py", line 97, in dnn_regression trials=Trials()) File "/Users/qingfengliu/.conda/envs/Example/lib/python3.7/site-packages/hyperas/optim.py", line 69, in minimize keep_temp=keep_temp) File "/Users/qingfengliu/.conda/envs/Example/lib/python3.7/site-packages/hyperas/optim.py", line 106, in base_minimizer from temp_model import keras_fmin_fnct, get_space File "/Users/qingfengliu/Dropbox/Feng_and_Liu/DMC/DMC_cluster/output/temp_model.py", line 75 x_train, x_val, x_test, y_train, y_val, y_test = dgp(self.random_seed, self.sample_size) ^ IndentationError: unexpected indent

該当のソースコード

import os

import tensorflow as tf
import numpy as np

os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"

from tensorflow import keras

import numpy as np

from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from keras.layers.advanced_activations import PReLU
from sklearn.preprocessing import MinMaxScaler
from hyperopt import Trials, STATUS_OK, tpe
from keras.layers.core import Dense, Dropout, Activation
from keras.models import Sequential
from hyperas import optim
from hyperas.distributions import choice, uniform
from tensorflow.keras.mixed_precision import experimental as mixed_precision
from simulation import dgp

class ImportData:
def init(self, random_seed, sample_size):
# Hold this implementation specific arguments as the fields of the class.
self.random_seed = random_seed
self.sample_size = sample_size

def data(self): # Calculate an objective value by using the extra arguments. x_train, x_val, x_test, y_train, y_val, y_test = dgp(self.random_seed, self.sample_size) return np.concatenate([x_train, x_val]), np.concatenate([y_train, y_val]), x_test, y_test

def create_regression_model(x_train, y_train):
model = Sequential()
model.add(Dense({{choice([50, 100, 1000])}}, input_shape=(len(x_train[0]),)))
model.add(Activation({{choice(['relu', 'tanh'])}}))
model.add(Dropout({{uniform(0, 1)}}))
model.add(Dense({{choice([50, 100, 1000])}}))
model.add(Activation({{choice(['relu', 'tanh'])}}))
model.add(Dropout({{uniform(0, 1)}}))
model.add(Dense({{choice([50, 100, 1000])}}))
model.add(Activation('relu'))
model.add(Dropout({{uniform(0, 1)}}))
model.add(Dense({{choice([50, 100, 1000])}}))
model.add(Activation('relu'))
model.add(Dropout({{uniform(0, 1)}}))
model.add(Dense({{choice([50, 100, 1000])}}))
model.add(Activation('relu'))
model.add(Dropout({{uniform(0, 1)}}))
model.add(Dense({{choice([50, 100, 1000])}}))
model.add(Dense(1))
# model.add(Activation('softmax', dtype='float32')) # mixed_float16

model.compile(loss='mean_squared_error', metrics=['accuracy'], optimizer={{choice(['rmsprop', 'adam', 'sgd'])}}) result = model.fit(x_train, y_train, batch_size={{choice([50, 100, 1000, 5000])}}, epochs={{choice([10, 20, 30, 50, 100])}}, verbose=2, validation_split=0.1) validation_loss = np.amin(result.history['val_loss']) # print('Best validation acc of epoch:', validation_acc) return {'loss': validation_loss, 'status': STATUS_OK, 'model': model}

def dnn_regression(data_fun, random_seed, sample_size):
# mixed_float16
policy = mixed_precision.Policy('mixed_float16')
mixed_precision.set_policy(policy)
best_run, best_model = optim.minimize(model=create_regression_model,
data=data_fun,
algo=tpe.suggest,
max_evals=1,
trials=Trials())
x_train, y_train, x_test, y_test = ImportData(random_seed, sample_size).data()
# evacuation of best performing model
loss_test, acc_test = best_model.evaluate(x_test, y_test)
best_model.summary()
return loss_test

if name == 'main':
# print(data_set)
Random_seed, Sample_size = 1, 100
data = ImportData(Random_seed, Sample_size)
data = data.data
# print(data())
Loss_test = dnn_regression(data, Random_seed, Sample_size)
print(Loss_test)

試したこと

補足情報(FW/ツールのバージョンなど)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問