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

标签 python machine-learning tensorflow skflow

我正在尝试通过用于学习 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 候选版本中的一个错误。修复了一些与确定性随机种子设置相关的错误。

关于python - 无法在 tensorflow 中实现可重复性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41317117/

相关文章:

python - 如何在python电子邮件脚本中的发件人地址之前添加发件人姓名

python - 从 Jupyter 使用 Python 设置 Drive API 时出错

data-structures - 当大多数/所有属性都是离散且距离相等时,KD 树仍然有效吗?

python - 解释深度神经网络的训练轨迹 : very low training loss and even lower validation loss

python - 使用 TensorFlow 和 Jupyter 实例化 InteractiveSession 时出错

python - Python 中的本地导入语句

python - 渲染 View 后如何做某事? ( Django )

machine-learning - GridSearchCV 是否使用 rbf 内核和不同程度计算 SVC?

python - 如何在 Keras 的自定义批量训练中获得每个时期的损失?

python - 在 Tensorflow 中恢复保存的神经网络