gpt4 book ai didi

python - 如何确保 Keras 模型权重在每次模型拟合时随机初始化

转载 作者:行者123 更新时间:2023-11-30 09:59:08 25 4
gpt4 key购买 nike

我正在为我的数据训练 Keras 模型。我必须将数据分成 3 部分,并且为每个部分调用相同的 keras 模型,并尝试连续拟合和预测。

我怀疑每次调用模型时,模型权重在上次训练达到收敛后都保持不变。调用的下一个模型开始最小化其先前状态的误差。我希望每次训练模型时,它都会开始拟合来自不同随机权重初始化的数据。因为我的所有 3 个分割都是同一数据集的子集,并且我不希望由于在训练时事先查看分割数据而导致任何数据泄漏到模型中。

我可以知道是否每次模型拟合时都会重新初始化权重。如果不是,我该怎么做?

这是我的代码的样子



model = Sequential()
model.add(Dense(512, input_dim=77, kernel_initializer='RandomNormal', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(256, kernel_initializer='RandomNormal', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(512, kernel_initializer='RandomNormal', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(256, kernel_initializer='RandomNormal', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(512, kernel_initializer='RandomNormal', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(256, kernel_initializer='RandomNormal', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1))


# Compile model
model.compile(loss='mean_absolute_error', optimizer='adam')


model()
# evaluate model
history = model.fit(scaler.transform(X_train_high), y_train_high,
batch_size=128,
epochs=5)
results = model.evaluate(scaler.transform(X_train_high), y_train_high, batch_size=128)
print('High test loss, test acc:', results)

# evaluate model
history = model.fit(scaler.transform(X_train_medium), y_train_medium,
batch_size=128,
epochs=5)
results = model.evaluate(scaler.transform(X_train_medium), y_train_medium, batch_size=128)
print(' Medium test loss, test acc:', results)

# evaluate model
history = model.fit(scaler.transform(X_train_low), y_train_low,
batch_size=128,
epochs=5)
results = model.evaluate(scaler.transform(X_train_low), y_train_low, batch_size=128, epochs=5)
print('Low test loss, test acc:', results)

最佳答案

模型将保持其权重,直到您重新定义权重。

def define_model():
model = Sequential()
model.add(Dense(512, input_dim=77, kernel_initializer='RandomNormal', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(256, kernel_initializer='RandomNormal', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(512, kernel_initializer='RandomNormal', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(256, kernel_initializer='RandomNormal', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(512, kernel_initializer='RandomNormal', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(256, kernel_initializer='RandomNormal', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1))

model=define_model()
# Compile model
model.compile(loss='mean_absolute_error', optimizer='adam')


# evaluate model
history = model.fit(scaler.transform(X_train_high), y_train_high,
batch_size=128,
epochs=5)
results = model.evaluate(scaler.transform(X_train_high), y_train_high, batch_size=128)
print('High test loss, test acc:', results)

model=define_model()

model.compile(loss='mean_absolute_error', optimizer='adam')
# evaluate model
history = model.fit(scaler.transform(X_train_medium), y_train_medium,
batch_size=128,
epochs=5)
results = model.evaluate(scaler.transform(X_train_medium), y_train_medium, batch_size=128)
print(' Medium test loss, test acc:', results)

您可以通过model.get_weights进行检查。

关于python - 如何确保 Keras 模型权重在每次模型拟合时随机初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59744488/

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