我正在尝试对我的超参数进行网格搜索,以调整深度学习架构。我有多个模型输入选项,我正在尝试使用 sklearn 的网格搜索 api。问题是,网格搜索api只接受单个数组作为输入,代码在检查数据大小维度时失败。(我的输入维度是5*数据点数,而根据sklearn api,它应该是数据点数*特征维度)。我的代码看起来像这样:
from keras.layers import Concatenate, Reshape, Input, Embedding, Dense, Dropout
from keras.models import Model
from keras.wrappers.scikit_learn import KerasClassifier
def model(hyparameters):
a = Input(shape=(1,))
b = Input(shape=(1,))
c = Input(shape=(1,))
d = Input(shape=(1,))
e = Input(shape=(1,))
//Some operations and I get a single output -->out
model = Model([a, b, c, d, e], out)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
k_model = KerasClassifier(build_fn=model, epochs=150, batch_size=512, verbose=2)
# define the grid search parameters
param_grid = hyperparameter options dict
grid = …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 keras.models.model_from_json 加载在 Keras 中训练的 RNN 模型架构,但出现上述错误
with open('model_architecture.json', 'r') as f:
model = model_from_json(f.read(), custom_objects={'AttLayer':AttLayer})
# Load weights into the new model
model.load_weights('model_weights.h5')
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的自定义图层
class AttLayer(Layer):
def __init__(self, attention_dim):
self.init = initializers.get('normal')
self.supports_masking = True
self.attention_dim = attention_dim
super(AttLayer, self).__init__()
def build(self, input_shape):
assert len(input_shape) == 3
self.W = K.variable(self.init((input_shape[-1], self.attention_dim)))
self.b = K.variable(self.init((self.attention_dim, )))
self.u = K.variable(self.init((self.attention_dim, 1)))
self.trainable_weights = [self.W, self.b, self.u]
super(AttLayer, self).build(input_shape)
def compute_mask(self, inputs, mask=None):
return None
def call(self, x, mask=None):
# size of …
Run Code Online (Sandbox Code Playgroud)