gpt4 book ai didi

python - Tensorflow DNNClassifier 返回错误的预测

转载 作者:行者123 更新时间:2023-11-30 09:21:02 32 4
gpt4 key购买 nike

我尝试使用tensorflow制作一个句子分类器,如官方网站tf.contrib.learn Quickstart的示例所示。但使用我自己的数据,首先我通过使用字典将所有数据(不同长度的字符串)转换为 id,然后将每个句子转换为整数数组。

每条训练记录都有自己指定的标签。

问题是预测不准确,只有一些预测不准确,但其他预测即使输入等于训练库的记录,结果也是错误的。
我的代码看起来像这样:

def launchModelData(values, labels, sample, actionClasses):

#Tensor for trainig data
v = tf.Variable(values)
l = tf.Variable(labels)

#Data Sample
s = tf.Variable(sample)

# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.contrib.learn.DNNClassifier(hidden_units=[10, 20, 10], n_classes=actionClasses)

# Add an op to initialize the variables.
init_op = tf.initialize_all_variables()

# Later, when launching the model
with tf.Session() as sess:
# Run the init operation.
sess.run(init_op)

# Fit model.
classifier.fit(x=v.eval(), y=l.eval(), steps=200)

# Classify one new sample.
new_sample = np.array(s.eval(), dtype=int)
y = classifier.predict(new_sample)
print ('Predictions: {}'.format(str(y)))

return y

值和类示例:

[0 1] 0  
[0 2] 0
[0 4] 0
[7 8] 1
[7 9] 1
[ 7 13] 1
[14 15] 2
[14 16] 2
[14 18] 2
[20 21] 3
[26 27] 5
[29 27] 5
[31 32] 5
...

我是 tensorflow 新手,因此我尝试使其尽可能简单,欢迎任何帮助。

编辑
我的实际训练数据是this.

我尝试了 8 个类,预测结果很好,所以也许我需要更大的语料库,我会尝试在新的编辑中显示我的输出。

编辑2

现在我使用五层 [n,2n,4n,8n,16n] 的组合,其中 n = 类和步骤 = 20000,这可以很好地减少损失并提高准确性,但同样它只适用于几个目标(10 aprox) 数量越大,预测就会出错。

最佳答案

tf.learn 中的估计器负责创建 session 和图形。它通过 input_fn 获取输入张量。每次拟合/评估/预测都会创建一个新的 session 和图表。代码应类似于以下内容:

# Build 3 layer DNN with 10, 20, 10 units respectively.
my_feature = tf.contrib.layers.real_valued_column('my_feature')
classifier = tf.contrib.learn.DNNClassifier(feature_columns=[my_feature], hidden_units=[10, 20, 10], n_classes=actionClasses)

def _my_train_data():
return {'my_feature': tf.constant(values), tf.constant(labels)

classifier.fit(input_fn=_my_train_data, steps=200)

# Classify one new sample.
def _my_predict_data():
return {'my_feature': tf.Constant(s)
y = classifier.predict(input_fn=_my_predict_data)
print ('Predictions: {}'.format(str(y)))

return y

关于python - Tensorflow DNNClassifier 返回错误的预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38725224/

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