我正在使用Tensorflow开发神经网络模型。在LOSO交叉验证中,我需要训练10倍的模型,因为我有10个不同主题的数据。
考虑到这一点,我需要在每次交叉验证折叠的开始时重置优化器和网络权重。我将权重定义如下:
weights = {
'w1' : tf.Variable(tf.random_uniform(shape = [100, 10],seed = 0)),
'w2' : tf.Variable(tf.random_uniform(shape = [10, 100],seed = 0))}
然后我通过重新初始化所有全局变量来重置优化器和权重,如下所示:
# Start session to run Tensors and Operations
with tf.Session() as sess:
# Optimizer
optimizer = tf.train.GradientDescentOptimizer(lr).minimize(loss)
# Variables initializer
init = tf.global_variables_initializer()
# Loop over the 10 cross-validation subjects
for subject in range(0,10):
# Initialize global variables and optimizer
sess.run(init)
# Print initialized weights (should be always the same)
print(sess.run(weights['w1']))
# Loop over epochs to train the model
for epoch in range(epochs):
# Run network optimizer for the current epoch
_,cost = sess.run([optimizer,loss], feed_dict ={X:x_train[subject,:], Y:y_train[subject,:]})
但是,在循环的每次迭代中,我都会打印出不同的权重值,例如我设置为0的“运算”种子没有完成其工作。有人知道我在想什么吗?
答案 0 :(得分:0)
我猜正在发生以下情况。假设您在脚本开始处将初始种子设置为42:
docker run --name iroha1 -p 50052:50051 -v $(pwd)/node1:/opt/iroha_data -v blockstore1:/tmp/block_store --network=iroha-network -e KEY='node1' hyperledger/iroha:latest
key=node1
/opt/iroha_data
WARNING: IROHA_POSTGRES_HOST is not defined.
Do not wait for Postgres to become ready. Iroha may fail to start up
[2019-05-24 09:42:39.416652545][I][Init]: Irohad version: 1.0.0
[2019-05-24 09:42:39.417173322][I][Init]: config initialized
[2019-05-24 09:42:39.419332310][I][Irohad]: created
[2019-05-24 09:42:39.419510386][I][Irohad/Storage]: Start storage creation
[2019-05-24 09:42:39.419664393][I][Irohad/Storage]: block store created
[2019-05-24 09:42:39.425382813][E][Init]: Failed to initialize storage
然后,您采样一个随机数:
set_seed(42)
现在,您再次采样:
sample()
>>> 0.875
是不同的。这不是错误。 sample()
>>> 0.311
函数告诉系统每次运行程序时,而不是每次{{1}时,从位置42(以伪随机数的顺序)开始,从“位置42”开始采样“随机”数。 }函数被调用。尝试多次运行您的程序。您会看到参数的权重在折痕之间会有所不同,但是在多次运行中会相同,即在所有运行中,折痕1都会始终将权重初始化为相同的值。
在脚本开始处抽样一次随机初始化,并在每次折叠开始时用它初始化您的体重:
set_seed()