【问题标题】:Non Reproducible results even after setting the seed value (Python API of Microsoft CNTK)即使在设置种子值后也无法重现结果(Microsoft CNTK 的 Python API)
【发布时间】:2017-11-05 20:33:30
【问题描述】:

之前,我提到在 CNTK 顺序机器学习模型 [1] 的 Branscripts 中没有任何选项可用于定义种子值。因此,我将我的代码迁移到 Python API (CNTK),它在定义顺序机器学习模型的种子值时提供了更细粒度的选项。以下是我在实现中使用随机初始化的实例(并设置了相应的种子值)

// CNTK 导入

import numpy as np
import pandas as pd
import random
import math as m

from cntk.device import *
from cntk import Trainer
from cntk.layers import * 

import cntk
import cntk.ops as o
import cntk.layers as l

//定义随机种子

np.random.seed(8888)
random.seed(8888)

// 定义输入输出训练向量

input_array_df = np.asarray(input_split_df[1:len(input_split_df)], dtype=np.float32)
output_array_df = np.asarray(output_df_df[1:len(output_df_df)], dtype=np.float32)
tup=(input_array_df, output_array_df)
listOfTuplesOfInputsLabels.append(tup)

//打乱输入向量

 random.shuffle(listOfTuplesOfInputsLabels) 

//定义顺序模型

num_minibatches = len(features) // minibatch_size
    epoch_size = len(features)*1

    feature = o.input_variable((input_dim),np.float32)
    label = o.input_variable((output_dim),np.float32)

    netout=Sequential([For(range(1), lambda i: Recurrence(LSTM(lstm_cell_dimension,use_peepholes=LSTM_USE_PEEPHOLES,init=glorot_uniform(seed=8888)))),Dense(output_dim,bias=BIAS,init=glorot_uniform(seed=8888))])(feature)

    learner = momentum_sgd(netout.parameters, lr = learning_rate_schedule([(4,0.003),(16,0.002)], unit=UnitType.sample,epoch_size=epoch_size),
                               momentum=momentum_as_time_constant_schedule(minibatch_size / -m.log(0.9)), gaussian_noise_injection_std_dev = gaussian_noise,l2_regularization_weight =l2_regularization_weight)

//拆分成小批量

tf = np.array_split(features,num_minibatches)
tl = np.array_split(labels,num_minibatches)

//训练

features = np.ascontiguousarray(tf[i%num_minibatches])
labels = np.ascontiguousarray(tl[i%num_minibatches])
trainer.train_minibatch({feature : features, label : labels})

不幸的是,即使我能够在我的代码中成功定义种子值,我仍然可以在最终结果中观察到一些较小的变化。这是因为浮点计算吗?或者你能在我的代码中找到我应该设置种子值的任何东西,我还没有这样做吗?

谢谢!

[1]Defining a seed value in Branscripts for CNTK sequential machine learning models

【问题讨论】:

    标签: python deep-learning lstm cntk


    【解决方案1】:

    你可以试试下面的:

    from _cntk_py import set_fixed_random_seed, force_deterministic_algorithms
    set_fixed_random_seed(1)
    force_deterministic_algorithms()
    

    【讨论】:

    • 感谢您的回复!。我在 CNTK 的 Github 个人资料上也发布了这个问题 Github Question,其中一个建议与您上面所说的相同。但不幸的是,即使这相对减少了结果的初始方差,我仍然可以在最终输出矩阵中观察到小的方差。
    • 显然在该页面中,其中一位开发人员已指示直接调用 cntk.cntk_py.set_fixed_random_seed(1) 而不是使用 _cntk_py
    • 我可以通过将我的代码升级到 CNTK 2.0 来解决这个问题。您可以在我在 CNTK 的 GitHub 个人资料 (github.com/Microsoft/CNTK/issues/1965) 上发布的问题中找到解决此问题的详细步骤
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 2018-11-12
    • 1970-01-01
    • 2018-01-04
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多