Tensorflow:迭代器的图与创建数据集的图不同

问题描述 投票:0回答:1

在使用 Keras_tuner 库时,我收到标题中的错误。我不知道如何修复它,并且有一段时间找不到任何解决方案。

我正在使用 Tensorflow BatchDatasets 并将它们传递给超参数调整。

batch_size = 5
train_data = keras.utils.timeseries_dataset_from_array(
    x_train_scaled,
    y_train_scaled,
    1,
    batch_size=batch_size,
    shuffle=False
)

val_data = keras.utils.timeseries_dataset_from_array(
    x_val_scaled,
    y_val_scaled,
    1,
    batch_size=batch_size,
    shuffle=False
)

test_data = keras.utils.timeseries_dataset_from_array(
    x_test_scaled,
    y_test_scaled,
    1,
    batch_size=batch_size,
    shuffle=False
)

# design network
def build_model(hp):
    model = keras.Sequential()
    model.add(keras.layers.LSTM(hp.Choice('units', [8, 16]), activation='relu', return_sequences=True, input_shape=(x_train.shape[1], x_train.shape[2])))
    model.add(keras.layers.LSTM(hp.Choice('units', [8, 16]), activation='relu', return_sequences=True))
    model.add(keras.layers.LSTM(hp.Choice('units', [8, 16]), activation='relu'))
    model.add(keras.layers.Dense(1))
    model.compile(loss='mae', optimizer='adam')
    return model

tuner  = keras_tuner.RandomSearch(
    build_model,
    objective='val_loss',
    max_trials=100)

tuner.search(train_data, epochs=100, shuffle=False, validation_data=val_data)
best_model = tuner.get_best_models()[0]

问题不在于输入形状或任何东西,因为如果我不使用 keras 调谐器,相同的模型也可以工作。 完整的错误是:

ValueError: The graph  of the iterator is different from the graph  the dataset: Tensor("PrefetchDataset:0", shape=(), dtype=variant) was created in. If you are using the Estimator API, make sure that no part of the dataset returned by the "input_fn" function is defined outside the "input_fn" function. Otherwise, make sure that the dataset is created in the same graph as the iterator.

触发它的线路是:

tuner.search(train_data, epochs=100, shuffle=False, validation_data=val_data)

python tensorflow keras lstm keras-tuner
1个回答
0
投票

我已经弄清楚如何解决这种情况下的问题。 我没有使用 BatchDataset,而是使用 np.array。 我还为

overwrite = True
添加了
keras_tuner.RandomSearch
代码现在可以运行,但是我仍然不知道该错误是什么。

# design network
def build_model(hp):
    model = keras.Sequential()
    model.add(keras.layers.LSTM(hp.Choice('units', [8, 16]), activation='relu',return_sequences=True, input_shape=(x_train.shape[1], x_train.shape[2])))
    model.add(keras.layers.LSTM(hp.Choice('units', [8, 16]), activation='relu', return_sequences=True))
    model.add(keras.layers.LSTM(hp.Choice('units', [8, 16]), activation='relu'))
    model.add(keras.layers.Dense(1))
    model.compile(loss='mae', optimizer='adam')
    return model

tuner  = keras_tuner.RandomSearch(
    build_model,
    objective='val_loss',
    overwrite=True,
    max_trials=100)

tuner.search(x_train, y_train, epochs=100, shuffle=False, validation_data=(x_val, y_val))
best_model = tuner.get_best_models()[0]
© www.soinside.com 2019 - 2024. All rights reserved.