gpt4 book ai didi

python - tensorflow 2.x : Cannot save trained model in h5 format (OSError: Unable to create link (name already exists))

转载 作者:行者123 更新时间:2023-12-04 01:14:41 25 4
gpt4 key购买 nike

我的模型使用预处理数据来预测客户是私有(private)客户还是非私有(private)客户。预处理步骤使用诸如 feature_column.bucketized_column(…)、feature_column.embedding_column(…) 等步骤。训练结束后,我试图保存模型,但出现以下错误:

File "h5py_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py\h5o.pyx", line 202, in h5py.h5o.link
OSError: Unable to create link (name already exists)

我尝试了以下方法来解决我的问题:

一切都没有成功!

模型相关代码如下:

(feature_columns, train_ds, val_ds, test_ds) = preprocessing.getPreProcessedDatasets(args.data, args.zip, args.batchSize)

feature_layer = tf.keras.layers.DenseFeatures(feature_columns, trainable=False)

model = tf.keras.models.Sequential([
feature_layer,
tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)
])

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

paramString = "Arg-e{}-b{}-z{}".format(args.epoch, args.batchSize, bucketSizeGEO)

...

model.fit(train_ds,
validation_data=val_ds,
epochs=args.epoch,
callbacks=[tensorboard_callback])


model.summary()

loss, accuracy = model.evaluate(test_ds)
print("Accuracy", accuracy)

paramString = paramString + "-a{:.4f}".format(accuracy)

outputName = "logReg" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") + paramStrin

if args.saveModel:
filepath = "./saved_models/" + outputName + ".h5"
model.save(filepath, save_format='h5')

预处理模块中调用的函数:

def getPreProcessedDatasets(filepath, zippath, batch_size, bucketSizeGEO):
print("start preprocessing...")

path = filepath
data = pd.read_csv(path, dtype={
"NAME1": np.str_,
"NAME2": np.str_,
"EMAIL1": np.str_,
"ZIP": np.str_,
"STREET": np.str_,
"LONGITUDE":np.floating,
"LATITUDE": np.floating,
"RECEIVERTYPE": np.int64})

feature_columns = []

data = data.fillna("NaN")

data = __preProcessName(data)
data = __preProcessStreet(data)

train, test = train_test_split(data, test_size=0.2, random_state=0)
train, val = train_test_split(train, test_size=0.2, random_state=0)

train_ds = __df_to_dataset(train, batch_size=batch_size)
val_ds = __df_to_dataset(val, shuffle=False, batch_size=batch_size)
test_ds = __df_to_dataset(test, shuffle=False, batch_size=batch_size)


__buildFeatureColums(feature_columns, data, zippath, bucketSizeGEO, True)

print("preprocessing completed")

return (feature_columns, train_ds, val_ds, test_ds)

调用特征的不同预处理函数:

def __buildFeatureColums(feature_columns, data, zippath, bucketSizeGEO, addCrossedFeatures):

feature_columns.append(__getFutureColumnLon(bucketSizeGEO))
feature_columns.append(__getFutureColumnLat(bucketSizeGEO))

(namew1_one_hot, namew2_one_hot) = __getFutureColumnsName(__getNumberOfWords(data, 'NAME1PRO'))
feature_columns.append(namew1_one_hot)
feature_columns.append(namew2_one_hot)

feature_columns.append(__getFutureColumnStreet(__getNumberOfWords(data, 'STREETPRO')))

feature_columns.append(__getFutureColumnZIP(2223, zippath))

if addCrossedFeatures:
feature_columns.append(__getFutureColumnCrossedNames(100))
feature_columns.append(__getFutureColumnCrossedZIPStreet(100, 2223, zippath))

与嵌入相关的函数:

def __getFutureColumnsName(name_num_words):
vocabulary_list = np.arange(0, name_num_words + 1, 1).tolist()

namew1_voc = tf.feature_column.categorical_column_with_vocabulary_list(
key='NAME1W1', vocabulary_list=vocabulary_list, dtype=tf.dtypes.int64)
namew2_voc = tf.feature_column.categorical_column_with_vocabulary_list(
key='NAME1W2', vocabulary_list=vocabulary_list, dtype=tf.dtypes.int64)

dim = __getNumberOfDimensions(name_num_words)

namew1_embedding = feature_column.embedding_column(namew1_voc, dimension=dim)
namew2_embedding = feature_column.embedding_column(namew2_voc, dimension=dim)

return (namew1_embedding, namew2_embedding)
def __getFutureColumnStreet(street_num_words):
vocabulary_list = np.arange(0, street_num_words + 1, 1).tolist()

street_voc = tf.feature_column.categorical_column_with_vocabulary_list(
key='STREETW', vocabulary_list=vocabulary_list, dtype=tf.dtypes.int64)

dim = __getNumberOfDimensions(street_num_words)

street_embedding = feature_column.embedding_column(street_voc, dimension=dim)

return street_embedding
def __getFutureColumnZIP(zip_num_words, zippath):
zip_voc = feature_column.categorical_column_with_vocabulary_file(
key='ZIP', vocabulary_file=zippath, vocabulary_size=zip_num_words,
default_value=0)

dim = __getNumberOfDimensions(zip_num_words)

zip_embedding = feature_column.embedding_column(zip_voc, dimension=dim)

return zip_embedding

最佳答案

以 h5 格式保存模型时出现错误 OSError: Unable to create link (name already exists) 是由一些重复的变量名引起的。通过 for i, w in enumerate(model.weights): print(i, w.name) 检查显示它们是 embedding_weights 名称。

通常,在构建feature_column 时,传入每个特征列的不同key 将用于构建不同的变量name。这在 TF 2.1 中工作正常,但在 TF 2.2 和 2.3 中出现故障,据推测 fixed in TF 2.4 nigthly .

关于python - tensorflow 2.x : Cannot save trained model in h5 format (OSError: Unable to create link (name already exists)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63753130/

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