gpt4 book ai didi

python - 无论 classifier.fit 中的步数如何,Tensorflow 都会返回相同的精度

转载 作者:行者123 更新时间:2023-11-30 08:49:20 24 4
gpt4 key购买 nike

classifier.fit 函数中的步长从 2000 更改为 1 可返回相同的准确度结果。我预计准确度结果会有所不同,有人可以告诉我为什么吗?

代码来自 Tensorflow 示例:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import numpy as np
import tensorflow as tf

# Data sets
IRIS_TRAINING = os.path.join(os.path.dirname(__file__), "iris_training.csv")
IRIS_TEST = os.path.join(os.path.dirname(__file__), "iris_test.csv")

def main(unused_argv):
# Load datasets.
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename=IRIS_TRAINING, target_dtype=np.int, features_dtype=np.float32)

test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename=IRIS_TEST, target_dtype=np.int, features_dtype=np.float32)

# Specify that all features have real-value data
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]

# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=3,
model_dir="/tmp/iris_model")

# Fit model.
classifier.fit(x=training_set.data,
y=training_set.target,
steps=1)

# Evaluate accuracy.
accuracy_score = classifier.evaluate(x=test_set.data,
y=test_set.target)["accuracy"]
print('Accuracy: {0:f}'.format(accuracy_score))

# Classify two new flower samples.
new_samples = np.array(
[[6.4, 3.2, 4.5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=float)
y = list(classifier.predict(new_samples, as_iterable=True))
print('Predictions: {}'.format(str(y)))

if __name__ == "__main__":
tf.app.run()

最佳答案

原因很简单:您已经定义了分类器的 model_dir 参数。医生是这样说的:

model_dir: Directory to save model parameters, graph and etc. This can also be used to load checkpoints from the directory into a estimator to continue training a previously saved model.

当您多次运行模型时,它不会从头开始学习,而是从“/tmp/iris_model”中获取先前的权重。

如果您想进行公平的测试,请删除此参数,您将看到小步长时准确度如何下降,高步长时准确度如何升高。

关于python - 无论 classifier.fit 中的步数如何,Tensorflow 都会返回相同的精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44468674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com