エラーが解決出来ずに困っています。
解決策を教えてほしいです。
エラー内容
model.add(Dense({{choice([64, 128, 256, 512])}}, activation = 'relu'))
^
SyntaxError: invalid syntax
参考にしたURL:https://torusblog.org/toru-hyperastunningdence/
Python
1import keras 2from hyperas import optim 3from hyperas.distributions import choice, uniform 4from hyperopt import STATUS_OK, Trials, tpe, rand 5from keras import models, optimizers 6from keras.datasets import mnist 7from keras.layers import Dense, Dropout 8from keras.models import Sequential 9from keras.optimizers import SGD 10 11 12def get_data () : 13 14 (x_train, y_train), (x_test, y_test) = mnist.load_data() 15 16 x_train = x_train.reshape(60000, 784) 17 x_test = x_test.reshape(10000, 784) 18 19 x_train = x_train.astype('float32') 20 x_test = x_test.astype('float32') 21 22 x_train /= 255 23 x_test /= 255 24 25 print(x_train.shape[0], 'train samples') 26 print(x_test.shape[0], 'test samples') 27 28 y_train = keras.utils.to_categorical(y_train, 10) 29 y_test = keras.utils.to_categorical(y_test, 10) 30 31 return x_train, y_train, x_test, y_test #並びに注意!!! 32 33 34 35def model(x_train, y_train, x_test, y_test) : 36 model = Sequential() 37 model.add(Dense({{choice([64, 128, 256, 512])}}, activation = 'relu', input_shape = (784,))) 38 model.add(Dropout(0.2) 39 model.add(Dense({{choice([64, 128, 256, 512])}}, activation = 'relu')) 40 model.add(Dropout(0.2) 41 model.add(Dense(10, activation = 'softmax')) 42 model.summary() 43 44 model.compile(loss = 'categorical_crossentropy', optimizer = SGD\ 45 (lr = 0.05, clipnorm = 1., nesterov = True), 46 metrics = ['accuracy']) 47 hist = model.fit(x_train, y_train, batch_size = 128, epochs = 20, 48 verbose = 1, validation_data = (x_test, y_test)) 49 val_loss, val_acc = model.evaluate(x_test, y_test, verbose = 1) 50 51 return {'loss': -val_acc, 'status': STATUS_OK, 'model': model} 52 53 54 55 56 57def hyper_model(model) : 58 59 best_run, best_model = optim.minimize(model = model, data = get_data, \ 60 algo = tpe.suggest,max_evals = 6, trials = Trials()) 61 62 print("=====Result=====") 63 print(best_model.summary()) 64 print(best_run) 65 66 _, _, x_test, y_test = get_data() 67 val_loss, val_acc = best_model.evaluate(x_test, y_test) 68 69 print("val_loss: ", val_loss) 70 print("val_acc: ", val_acc) 71 72 return val_acc, val_loss 73 74#===Learning=== 75 76val_acc, val_loss = hyper_model(model) 77 78#==============
回答1件
あなたの回答
tips
プレビュー