Я столкнулся с проблемой, касающейся оптимизации гиперпараметров в сверточной нейронной сети для анализа текстовых данных. Во-первых, я объясню свой процесс до сих пор: с помощью различных отличных сообщений в блогах я смог создать CNN, который работает для моего проекта. В своем проекте я пытаюсь предсказать VIX и S&P 500 с помощью утверждений собрания FOM C. Так что в основном у меня есть текстовые данные с одной стороны и финансовые данные (отчеты) с другой стороны. После предварительной обработки и применения Googles Word2Ve c предварительно обученных Word-Embeddings я построил следующую сверточную сеть:
def ConvNet(embeddings, max_sequence_length, num_words, embedding_dim, trainable=False, extra_conv=True,
lr=0.001, dropout=0.5):
embedding_layer = Embedding(num_words,
embedding_dim,
weights=[embeddings],
input_length=max_sequence_length,
trainable=trainable)
sequence_input = Input(shape=(max_sequence_length,), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)
convs = []
filter_sizes = [3, 4, 5]
for filter_size in filter_sizes:
l_conv = Conv1D(filters=128, kernel_size=filter_size, activation='relu')(embedded_sequences)
l_pool = MaxPooling1D(pool_size=3)(l_conv)
convs.append(l_pool)
l_merge = concatenate([convs[0], convs[1], convs[2]], axis=1)
# add a 1D convnet with global maxpooling, instead of Yoon Kim model
conv = Conv1D(filters=128, kernel_size=3, activation='relu')(embedded_sequences)
pool = MaxPooling1D(pool_size=3)(conv)
if extra_conv == True:
x = Dropout(dropout)(l_merge)
else:
# Original Yoon Kim model
x = Dropout(dropout)(pool)
x = Flatten()(x)
x = Dense(128, activation='relu')(x)
preds = Dense(1, activation='linear')(x)
model = Model(sequence_input, preds)
sgd = SGD(learning_rate = lr, momentum= 0.8)
model.compile(loss='mean_squared_error',
optimizer= sgd,
metrics=['mean_squared_error'])
model.summary()
return model
model = ConvNet(train_embedding_weights, MAX_SEQUENCE_LENGTH, len(train_word_index)+1, EMBEDDING_DIM, False)
#define callbacks
early_stopping = EarlyStopping(monitor='val_loss', min_delta=0.01, patience=4, verbose=1)
callbacks_list = [early_stopping]
hist = model.fit(x_train, y_tr, epochs=5, batch_size=33, validation_split=0.2, shuffle=True, callbacks=callbacks_list)
Архитектура моей модели выглядит так:
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) (None, 1086) 0
__________________________________________________________________________________________________
embedding_1 (Embedding) (None, 1086, 300) 532500 input_1[0][0]
__________________________________________________________________________________________________
conv1d_1 (Conv1D) (None, 1084, 128) 115328 embedding_1[0][0]
__________________________________________________________________________________________________
conv1d_2 (Conv1D) (None, 1083, 128) 153728 embedding_1[0][0]
__________________________________________________________________________________________________
conv1d_3 (Conv1D) (None, 1082, 128) 192128 embedding_1[0][0]
__________________________________________________________________________________________________
max_pooling1d_1 (MaxPooling1D) (None, 361, 128) 0 conv1d_1[0][0]
__________________________________________________________________________________________________
max_pooling1d_2 (MaxPooling1D) (None, 361, 128) 0 conv1d_2[0][0]
__________________________________________________________________________________________________
max_pooling1d_3 (MaxPooling1D) (None, 360, 128) 0 conv1d_3[0][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate) (None, 1082, 128) 0 max_pooling1d_1[0][0]
max_pooling1d_2[0][0]
max_pooling1d_3[0][0]
__________________________________________________________________________________________________
dropout_2 (Dropout) (None, 1082, 128) 0 concatenate_1[0][0]
__________________________________________________________________________________________________
flatten_1 (Flatten) (None, 138496) 0 dropout_2[0][0]
__________________________________________________________________________________________________
dense_3 (Dense) (None, 128) 17727616 flatten_1[0][0]
__________________________________________________________________________________________________
dense_4 (Dense) (None, 1) 129 dense_3[0][0]
==================================================================================================
Total params: 18,721,429
Trainable params: 18,188,929
Non-trainable params: 532,500
Архитектура модели: ![enter image description here](https://i.stack.imgur.com/uMXPc.png)
Итак, теперь я столкнулся со следующей большой проблемой, и у меня действительно заканчиваются идеи, как ее решить: Оптимизация гиперпараметров
Итак, мой конкретный c вопрос: как выполнить оптимизацию гиперпараметров?
Мой поисковый код:
from hyperopt import fmin, hp, tpe, space_eval, Trials
def train_and_score(args):
# Train the model the fixed params plus the optimization args.
# Note that this method should return the final History object.
test = ConvNet(embeddings=train_embedding_weights, max_sequence_length= MAX_SEQUENCE_LENGTH,
num_words=len(train_word_index)+1, embedding_dim= EMBEDDING_DIM,
trainable=False, extra_conv=True,
lr=args['lr'], dropout=args['dropout'])
# Unpack and return the last validation loss from the history.
return test['val_loss'][-1]
# Define the space to optimize over.
space = {
'lr': hp.loguniform('lr', np.log(0.01), np.log(0.1)),
'dropout': hp.uniform('dropout', 0, 0.5)
}
# Minimize the training score over the space.
trials = Trials()
best = fmin(fn=train_and_score,
space=space,
trials=trials,
algo=tpe.suggest,
max_evals=100)
# Print details about the best results and hyperparameters.
print(best)
print(space_eval(space, best))
Сообщение об ошибке c:
__________________________________________________________________________________________________
0%| | 0/100 [00:00<?, ?trial/s, best loss=?]
job exception: 'Model' object is not subscriptable
Traceback (most recent call last):
File "/Users/lukaskoston/Desktop/MasterarbeitFOMCAnalysis/07_Regression/CNN regression neu.py", line 262, in <module>
max_evals=100)
File "/Users/lukaskoston/.local/lib/python3.7/site-packages/hyperopt/fmin.py", line 482, in fmin
show_progressbar=show_progressbar,
File "/Users/lukaskoston/.local/lib/python3.7/site-packages/hyperopt/base.py", line 686, in fmin
show_progressbar=show_progressbar,
File "/Users/lukaskoston/.local/lib/python3.7/site-packages/hyperopt/fmin.py", line 509, in fmin
rval.exhaust()
File "/Users/lukaskoston/.local/lib/python3.7/site-packages/hyperopt/fmin.py", line 330, in exhaust
self.run(self.max_evals - n_done, block_until_done=self.asynchronous)
File "/Users/lukaskoston/.local/lib/python3.7/site-packages/hyperopt/fmin.py", line 286, in run
self.serial_evaluate()
File "/Users/lukaskoston/.local/lib/python3.7/site-packages/hyperopt/fmin.py", line 165, in serial_evaluate
result = self.domain.evaluate(spec, ctrl)
File "/Users/lukaskoston/.local/lib/python3.7/site-packages/hyperopt/base.py", line 894, in evaluate
rval = self.fn(pyll_rval)
File "/Users/lukaskoston/Desktop/MasterarbeitFOMCAnalysis/07_Regression/CNN regression neu.py", line 248, in train_and_score
return hist['val_loss'][-1]
TypeError: 'Model' object is not subscriptable
Заранее спасибо, Лукас