keras 如何摆脱“UserWarning:初始化器GlorotUniform是非种子的”消息?

0h4hbjxa  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(234)

下面的代码是:Bias-Variance Decomposition for Model Assessment

import matplotlib.pyplot as plt
import numpy as np

import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential

from mlxtend.evaluate import bias_variance_decomp
from mlxtend.data import boston_housing_data

from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import BaggingRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

np.random.seed(16)
tf.random.set_seed(16)

X, y = boston_housing_data()
X_train, X_test, y_train, y_test = train_test_split(X, y,
                                                test_size=0.3,
                                                random_state=123,
                                                shuffle=True)

model = Sequential()
model.add(Dense(2048, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(1, activation='linear'))

optimizer = tf.keras.optimizers.Adam()
model.compile(loss='mean_squared_error', optimizer=optimizer)
model.fit(X_train, y_train, epochs=100, batch_size=32, verbose=0)
mean_squared_error(model.predict(X_test), y_test)

avg_expected_loss, avg_bias, avg_var = bias_variance_decomp(
    model, X_train, y_train, X_test, y_test, 
    loss='mse',
    num_rounds=100,
    random_seed=16,
    epochs=100,
    batch_size=32,
    verbose=0)

print('Average expected loss: %.3f' % avg_expected_loss)
print('Average bias: %.3f' % avg_bias)
print('Average variance: %.3f' % avg_var)

该代码有效。但是,它会产生一个恼人的警告:

  • 用户警告:初始值设定项GlorotUniform未设为种子,并且被多次调用,每次都将返回相同的值(即使初始值设定项未设为种子)。请更新代码以向初始值设定项提供种子,或者避免多次使用同一初始值设定项示例。warnings.warn(*

需要对代码进行哪些更改才能消除警告?

epfja78i

epfja78i1#

正如警告消息所说,需要为初始化程序提供一个种子。只需将代码更改为:

import matplotlib.pyplot as plt
import numpy as np

import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential

from mlxtend.evaluate import bias_variance_decomp
from mlxtend.data import boston_housing_data

from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import BaggingRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

from keras import initializers

np.random.seed(16)
tf.random.set_seed(16)

X, y = boston_housing_data()
X_train, X_test, y_train, y_test = train_test_split(X, y,
                                                test_size=0.3,
                                                random_state=123,
                                                shuffle=True)

model = Sequential()
model.add(Dense(2048, activation='relu', 
kernel_initializer=initializers.glorot_uniform(seed=0)))
model.add(Dense(512, activation='relu', 
kernel_initializer=initializers.glorot_uniform(seed=0)))
model.add(Dense(32, activation='relu', 
kernel_initializer=initializers.glorot_uniform(seed=0)))
model.add(Dense(1, activation='linear', 
kernel_initializer=initializers.glorot_uniform(seed=0)))

optimizer = tf.keras.optimizers.Adam()
model.compile(loss='mean_squared_error', optimizer=optimizer)
model.fit(X_train, y_train, epochs=100, batch_size=32, verbose=0)
mean_squared_error(model.predict(X_test), y_test)

avg_expected_loss, avg_bias, avg_var = bias_variance_decomp(
    model, X_train, y_train, X_test, y_test, 
    loss='mse',
    num_rounds=10,
    random_seed=16,
    epochs=10,
    batch_size=32,
    verbose=0)

print('Average expected loss: %.3f' % avg_expected_loss)
print('Average bias: %.3f' % avg_bias)
print('Average variance: %.3f' % avg_var)

相关问题