网格搜索适合不接受张量列表 [英] Grid Search fit not accepting list of tensors

查看:69
本文介绍了网格搜索适合不接受张量列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个暹罗网络,我想使用GridSearchCV对它进行网格搜索。

I have a siamese network and I want to perform a grid seach on it using GridSearchCV.

因此,我使用以下功能创建模型:

So I create a model using the following function:

def createMod(learn_rate=0.01, optimizer='Adam'):
#K.clear_session()
# network definition
base_network = create_base_network(input_shape)

input_a = Input(shape=input_shape)
input_b = Input(shape=input_shape)

# because we re-use the same instance `base_network`,
# the weights of the network will be shared across the two branches
processed_a = base_network(input_a)
processed_b = base_network(input_b)

distance = Lambda(euclidean_distance,
                  output_shape=eucl_dist_output_shape)([processed_a, processed_b])

prediction = Dense(1,activation='sigmoid')(distance)

model = Model([input_a, input_b], prediction)

if(optimizer=='SGD'):
    opt = SGD(lr=learn_rate)
elif (optimizer=='RMSprop'):
    opt = RMSprop(lr=learn_rate)
else:
    opt = Adam(lr=learn_rate)


model.compile(loss='binary_crossentropy', optimizer=opt, metrics=[accuracy])

return model

然后我创建KerasClassifier并GridSearch如下:

And then I create the KerasClassifier and GridSearch as follows:

model = KerasClassifier(build_fn=createMod, verbose=0)
param_grid = dict(epochs=epochs, batch_size=batch_size, learn_rate=learn_rate,optimizer=optimizer)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=5)
X = [tr_pairs[:, 0], tr_pairs[:, 1]]
grid_result = grid.fit(X, tr_y)

但是这会引发以下情况值错误:

However this throws the following value error:


ValueError:发现输入变量的样本数不一致:[2,1054]

ValueError: Found input variables with inconsistent numbers of samples: [2, 1054]

tr_pairs [:, 0]和tr_pairs [:, 1]的形状为(1054,6),tr_y为(1054,)

The shape of tr_pairs[:, 0] and tr_pairs[:, 1] is (1054, 6) and tr_y is (1054,)

基本网络是:

def create_base_network(input_shape):
    K.clear_session()
    encoder = build_encoder(latent_dim, n_in)
    decoder = build_decoder(latent_dim, n_in)

    item = Input(shape=(n_in, ))
    encoded_repr = encoder(item)
    reconstructed_item = decoder(encoded_repr)

    autoencoder = Model(item, reconstructed_item)
    
    return autoencoder

编码器和解码器的代码为:

The code for the encoder and decoder are:

def build_encoder(latent_dim, input_dim):
    input_layer = Input(shape=(input_dim, ))
    h = Dense(32, activation='relu', activity_regularizer=regularizers.l1(10e-5))(input_layer)
    h = Dropout(0.1)(h)
    h = Dense(64, activation='relu')(h)
    h = Dropout(0.1)(h)
#     h = Dense(128, activation='relu')(h)
#     h = Dropout(0.1)(h)
    latent_repr = Dense(latent_dim, activation='relu')(h)
    return Model(input_layer, latent_repr)

def build_decoder(latent_dim, input_dim):
    model = Sequential()
#     model.add(Dense(128, input_dim=latent_dim, activation='relu'))
#     model.add(Dropout(0.1))
    model.add(Dense(64, activation='relu'))
    model.add(Dropout(0.1))
    model.add(Dense(32, activation='relu'))
    model.add(Dropout(0.1))
    model.add(Dense(input_dim, activation='relu'))
    z = Input(shape=(latent_dim,))
    item = model(z)
    return Model(z, item)

当我做普通的keras模型的.fit函数,但是在这里不起作用..我的代码中是否存在问题,或者只是无法在Grid Search中提供多个输入,如果是这样,我仍然可以

The code works when I do the normal keras model's .fit function but doesn't work here.. is there a problem somewhere in my code or is it just not possible to feed multiple inputs in Grid Search and if that is the case is there a way I can still perform the grid search?

推荐答案

这是传递多个输入的解决方法。我创建了一个虚拟模型,该模型接收格式为(n_sample,2,6)的SINGLE输入,然后使用Lambda层将其分为两部分。您可以根据您的暹罗结构对其进行修改。

this is workaround to pass multiple input. I create a dummy model that receives a SINGLE input in the format (n_sample, 2, 6) and then split it into two parts using Lambda layer. you can modify this according to your siamese structure.

def createMod(optimizer='Adam'):
    
    combi_input = Input((2,6)) # (n_sample, 2, 6)
    input_a = Lambda(lambda x: x[:,0])(combi_input) # (n_sample, 6)
    input_b = Lambda(lambda x: x[:,1])(combi_input) # (n_sample, 6)

    c = Concatenate()([input_a,input_b])
    x = Dense(32)(c)

    prediction = Dense(1,activation='sigmoid')(x)
    model = Model(combi_input, prediction)

    model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics='accuracy')

    return model

tr_pairs = np.random.uniform(0,1, (1054, 2, 6))
tr_y = np.random.randint(0,2, 1054)

model = tf.keras.wrappers.scikit_learn.KerasClassifier(build_fn=createMod, verbose=0)
batch_size = [10, 20]
epochs = [10, 5]
optimizer = ['adam','SGD']
param_grid = dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=3)
grid_result = grid.fit(tr_pairs, tr_y)

这篇关于网格搜索适合不接受张量列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆