gpt4 book ai didi

python - keras.metrics 没有属性 'Metric'

转载 作者:行者123 更新时间:2023-12-05 06:18:39 25 4
gpt4 key购买 nike

我尝试使用 Keras Tuner 进行超参数优化:

import keras
from kerastuner import HyperModel
from kerastuner.tuners import Hyperband

input_shape = (1, 28, 28)
num_classes = 10

# Define hypermodel class
class CNNHyperModel(HyperModel):
def __init__(self, input_shape, num_classes):
self.input_shape = input_shape
self.num_classes = num_classes

def build(self, hp):
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation="relu", input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation="softmax"))

model.compile(
loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=["accuracy"],
)

return model

# Instantiate
hypermodel = CNNHyperModel(input_shape=input_shape, num_classes=num_classes)

# Create tuner
HYPERBAND_MAX_EPOCHS = 40
MAX_TRIALS = 20
EXECUTION_PER_TRIAL = 2
SEED = 1

tuner = RandomSearch(
hypermodel,
max_epochs=HYPERBAND_MAX_EPOCHS,
objective='val_accuracy',
seed=SEED,
max_trials=MAX_TRIALS,
executions_per_trial=EXECUTION_PER_TRIAL,
directory='hyperband',
project_name='mnist'
)

我明白了

AttributeError: module 'tensorflow._api.v1.keras.metrics' has no attribute 'Metric'

使用 conda 安装了 Tensorflow 1.13 和 2.0。

按照 this answer 中的建议,包括 from tensorflow.python.keras.metrics import Metric不会改变任何东西。

最佳答案

最新版本的 Tensorflow 2.6.0 有 tf.keras.metrics.Metric API。

您可以导入为

from tensorflow.keras.metrics import Metric

关于python - keras.metrics 没有属性 'Metric',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61193282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com