简体   繁体   English

自动编码器 Gridsearch 超参数调优 Keras

[英]Autoencoder Gridsearch Hyperparameter tuning Keras

My data shape is the same, I just generated here random numbers.我的数据形状是一样的,我只是在这里生成了随机数。 In real the datas are float numbers from range -6 to 6, I scaled them as well.实际上,数据是从 -6 到 6 的浮点数,我也对它们进行了缩放。 The Input layer size and Encoding dimension have to remain the same.输入层大小和编码维度必须保持不变。 When I am training the loss starts and stays at 0.631 all the time.当我训练时,损失开始并一直保持在0.631 I changed the learning rate manually.我手动更改了学习率。 I am new to python and do not know to implement to a grid search to this code to find the right parameters.我是 python 的新手,不知道要对这段代码进行网格搜索以找到正确的参数。 What else can I do to tune my network ?我还能做些什么来调整我的网络?

import numpy as np
from keras.layers import Input, Dense
from keras.models import Model
from keras import optimizers

#Train data
x_train=np.random.rand(2666000)
x_train = (train-min(train))/(max(train)-min(train))
x_train=x_train.reshape(-1,2000)

x_test=[]#empty testing later
#Enc Dimension 
encoding_dim=100
#Input shape
input_dim = Input(shape=(2000,))
#Encoding Layer
encoded = Dense(encoding_dim, activation='relu')(input_dim)
#Decoding Layer
decoded = Dense(2000, activation='sigmoid')(encoded)

#Model AE
autoencoder = Model(input_dim, decoded)
#Model Encoder 
encoder = Model(input_dim, encoded)
#Encoding
encoded_input = Input(shape=(encoding_dim,))
#Decoding 
decoder_layer = autoencoder.layers[-1]
#Model Decoder 
decoder = Model(encoded_input, decoder_layer(encoded_input))

optimizers.Adadelta(lr=0.1, rho=0.95, epsilon=None, decay=0.0)
autoencoder.compile(optimizer=optimizer, loss='binary_crossentropy', 
                metrics=['accuracy'])
#Train and test 
autoencoder_train= autoencoder.fit(x_train, x_train,
            epochs=epochs, shuffle=False, batch_size=2048)

I suggest adding more hidden layers.我建议添加更多隐藏层。 If your loss stays the same it means at least one of two things:如果您的损失保持不变,则至少意味着以下两件事之一:

  • Your data is more or less random and there are no relationships to be drawn您的数据或多或少是随机的,没有要绘制的关系

  • Your model is not complex enough to learn meaningful relationships from your data您的模型不够复杂,无法从数据中学习有意义的关系

A rule of thumb for me is that a model should be powerful enough to overfit the data given enough training iterations.对我来说,一个经验法则是模型应该足够强大,以在足够多的训练迭代下过度拟合数据。

Unfortunately there is a fine line between sufficiently complex and too complex.不幸的是,在足够复杂和过于复杂之间只有一线之隔。 You have to play around with the number of hidden layers, the number of units in each layer, and the amount of epochs you take to train your network.您必须考虑隐藏层的数量、每层中的单元数量以及用于训练网络的 epoch 数量。 Since you only have two Dense layers, a good starting point would be to increase model complexity.由于您只有两个 Dense 层,因此增加模型复杂性是一个很好的起点。

在此处输入图片说明

If you insist on using a grid search keras has a wrapper for scikit_learn and sklearn has a grid search module.如果你坚持使用网格搜索,keras 有一个 scikit_learn 的包装器,sklearn 有一个网格搜索模块。 A toy example:一个玩具示例:

from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV

def create_model():
    <return a compiled but untrained keras model>

model = KerasClassifier(build_fn = create_model, batch_size=1000, epochs=10)
#now write out all the parameters you want to try out for the grid search
activation = ['relu', 'tanh', 'sigmoid'...]
learn_rate = [0.1, 0.2, ...]
init = ['unform', 'normal', 'zero', ...]
optimizer = ['SGD', 'Adam' ...]
param_grid = dict(activation=activation, learn_rate=learn_rate, init=init, optimizer=optimizer)
grid = GridSearchCV(estimator=model, param_grid=param_grid)
result = grid.fit(X, y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM