def build_model(max_features=1, maxlen=65): """Build LSTM model""" model = Sequential() model.add(Embedding(max_features, 128, input_length=maxlen)) model.add(LSTM(128)) model.add(Dropout(0.5)) model.add(Dense(1)) model.add(Activation('sigmoid')) # model.add(tf.keras.layers.BatchNormalization()) model.compile(loss='binary_crossentropy', optimizer='rmsprop') return model def data(Domain): valid_chars = pickle.load(open('dictionary.plk','rb')) Domain = [[valid_chars[y] for y in x] for x in Domain] Domain = sequence.pad_sequences(Domain, maxlen=64) return Domain def run(Domain): Domains=data(Domain) model = build_model() model = model.load_weights('Mymodel.h5') predictions = model.predict(Domains) return predictions if __name__ == "__main__": print(run('baidu.com')) # Run with 1 to make it fast
时间: 2024-03-07 13:49:22 浏览: 23
在 `run` 函数中,使用 `model.load_weights` 方法加载模型权重时,不需要重新定义 `model`,直接使用之前定义的 `model` 即可。修改后的代码如下:
```python
def run(Domain):
Domains=data(Domain)
model = build_model()
model.load_weights('Mymodel.h5')
predictions = model.predict(Domains)
return predictions
```
此外,`run` 函数的参数需要传入一个列表或数组,因为 `data` 函数返回的是一个二维数组。修改后的代码如下:
```python
if __name__ == "__main__":
print(run(['baidu.com'])) # 注意需要传入一个列表或数组
```
另外,`build_model` 函数中的 `maxlen` 参数为 65,而在 `data` 函数中使用的是 64。需要保持一致。
相关问题
def build_model(max_features=1, maxlen=65): """Build LSTM model""" model = Sequential() model.add(Embedding(max_features, 128, input_length=maxlen)) model.add(LSTM(128)) model.add(Dropout(0.5)) model.add(Dense(1)) model.add(Activation('sigmoid')) # model.add(tf.keras.layers.BatchNormalization()) model.compile(loss='binary_crossentropy', optimizer='rmsprop') return model def data(Domain): valid_chars = pickle.load(open('dictionary.plk','rb')) Domain = [[valid_chars[y] for y in x] for x in Domain] Domain = sequence.pad_sequences(Domain, maxlen=65) return Domain def run(Domain): Domains = data(Domain) modelaaa = build_model(max_features=1, maxlen=65) modelaaa.load_weights('Mymodel.h5') predictions = model.predict(Domains) return predictions if __name__ == "__main__": print(run(['baidu.com'])) # Run with 1 to make it fast报错Unable to load weights saved in HDF5 format into a subclassed Model which has not created its variables yet. Call the Model first, then load the weights.怎么改
在 `run` 函数中,我们应该先创建模型,再加载权重。所以我们需要将创建模型和加载权重的顺序调换一下,同时将模型命名为 `modelaaa`,但在 `predict` 中使用的却是 `model`,需要将其改为 `modelaaa`。修改后的代码如下:
```
def run(Domain):
Domains = data(Domain)
modelaaa = build_model(max_features=1, maxlen=65)
modelaaa.load_weights('Mymodel.h5')
predictions = modelaaa.predict(Domains)
return predictions
```
这样就可以避免 `Unable to load weights saved in HDF5 format into a subclassed Model which has not created its variables yet. Call the Model first, then load the weights.` 的错误了。
def build_model(max_features, maxlen): """Build LSTM model""" model = Sequential() model.add(Embedding(max_features, 128, input_length=maxlen)) model.add(LSTM(128)) model.add(Dropout(0.5)) model.add(Dense(1)) model.add(Activation('sigmoid')) # model.add(tf.keras.layers.BatchNormalization()) model.compile(loss='binary_crossentropy', optimizer='rmsprop') return model怎么初始化
要初始化这个函数中的模型,我们只需要调用这个函数即可,这将返回一个已编译好的Keras模型对象。以下是一个示例:
```
max_features = 10000
maxlen = 100
model = build_model(max_features, maxlen)
```
在这个示例中,我们将“max_features”设置为10000,“maxlen”设置为100,并调用“build_model”函数来构建模型。这将返回一个已编译好的Keras模型对象“model”,我们可以使用它来进行训练和预测。例如,我们可以使用以下代码来编译模型并训练它:
```
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=32, epochs=10, validation_data=(x_test, y_test))
```
在这个示例中,我们使用“model.compile”方法来编译模型,指定优化器、损失函数和评估指标。然后,我们使用“model.fit”方法来训练模型,指定训练数据、批大小、训练轮数和验证数据。这将训练模型并返回训练结果。