我正在尝试使用高级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,但结果与运行不同。
我错过了什么?
答案 0 :(得分:1)
Per keveman的评论,这似乎是(现在非常古老的)0.12-RC候选版本中的一个错误。有一些与已确定的确定性随机种子设置相关的错误。