Skip to main content
 首页 » 编程设计

python之无法在 tensorflow 中实现可重复性

2025年02月15日27emanlee

我正在尝试通过用于学习 TensorFlow 的高级 API 来学习 tensorflow,但遇到无法获得可重复结果的问题。

TensorFlow 0.12.0-rc0(仅限 CPU) python 3.5

import numpy as np 
import tensorflow as tf 
import os 
import shutil 
import random 
 
#print(tf.__version__) 
#0.12.0-rc0 
 
LOG_OUTPUT_DIR = "/home/tensorflow/model" 
 
def clear_logs(): 
    for root, dirs, files in os.walk(LOG_OUTPUT_DIR): 
        for f in files: 
            os.unlink(os.path.join(root, f)) 
        for d in dirs: 
            shutil.rmtree(os.path.join(root, d)) 
 
 
data = np.array( 
    [ 
        [   1   ,   2   ,   3   ,   4   ,   5   ], 
        [   2   ,   3   ,   4   ,   5   ,   6   ], 
        [   3   ,   4   ,   5   ,   6   ,   7   ], 
        [   4   ,   5   ,   6   ,   7   ,   8   ], 
        [   5   ,   6   ,   7   ,   8   ,   9   ], 
        [   6   ,   7   ,   8   ,   9   ,   10  ], 
        [   7   ,   8   ,   9   ,   10  ,   11  ], 
        [   8   ,   9   ,   10  ,   11  ,   12  ], 
        [   9   ,   10  ,   11  ,   12  ,   13  ], 
        [   10  ,   11  ,   12  ,   13  ,   14  ], 
        [   11  ,   12  ,   13  ,   14  ,   15  ], 
        [   12  ,   13  ,   14  ,   15  ,   16  ], 
        [   13  ,   14  ,   15  ,   16  ,   17  ], 
        [   14  ,   15  ,   16  ,   17  ,   18  ], 
        [   15  ,   16  ,   17  ,   18  ,   19  ], 
        [   16  ,   17  ,   18  ,   19  ,   20  ], 
        [   17  ,   18  ,   19  ,   20  ,   21  ], 
        [   18  ,   19  ,   20  ,   21  ,   22  ], 
        [   19  ,   20  ,   21  ,   22  ,   23  ] 
    ]) 
 
target = np.array([ 
    [   6   ], 
    [   7   ], 
    [   8   ], 
    [   9   ], 
    [   10  ], 
    [   11  ], 
    [   12  ], 
    [   13  ], 
    [   14  ], 
    [   15  ], 
    [   16  ], 
    [   17  ], 
    [   18  ], 
    [   19  ], 
    [   20  ], 
    [   21  ], 
    [   22  ], 
    [   23  ], 
    [   24  ] 
    ]) 
 
#out of range data 
data_out = np.array([[  20  ,   21  ,   22  ,   23  ,   24  ]]) 
 
INPUT_COUNT = data.shape[1] 
clear_logs() 
 
 
MY_SEED = 1234 
tf.set_random_seed(MY_SEED) 
tf.logging.set_verbosity(tf.logging.ERROR) 
 
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=INPUT_COUNT)] 
HIDDEN_UNITS = [INPUT_COUNT * 2, INPUT_COUNT * 4, INPUT_COUNT * 2]  
 
with tf.Graph().as_default() as g: 
    random.seed(MY_SEED) 
    g.seed = MY_SEED 
 
    regressor = tf.contrib.learn.DNNRegressor( 
            feature_columns=feature_columns, hidden_units=HIDDEN_UNITS, 
            model_dir=LOG_OUTPUT_DIR, 
            config=tf.contrib.learn.RunConfig(tf_random_seed=MY_SEED)) 
 
    regressor.fit(data, target, steps=300, batch_size=data.shape[0]) 
 
    accuracy_score = regressor.evaluate(x=data,y=target)["loss"] 
    print('Accuracy: {0:f}'.format(accuracy_score)) 
 
    y = regressor.predict(data_out, as_iterable=False) 
    final_cost = np.sqrt(np.mean((y-[25])**2)) 
    print('#RMSE:', final_cost, '; Result:', y) 

如您所见,我尽可能地尝试放置随机种子 MY_SEED,但每次运行的结果都不同。

我错过了什么?

请您参考如下方法:

根据 keveman 的评论,这似乎是(现在很旧的)0.12-RC 候选版本中的一个错误。修复了一些与确定性随机种子设置相关的错误。