我尝试使用tensorflow作为官方网站tf.contrib.learn快速入门的示例中的句子分类器,但是使用我自己的数据,首先我通过使用字典将所有数据(长度不同的字符串)转换为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的新手,所以我尝试使其变得不太复杂,欢迎任何帮助。
编辑
我的实际训练数据是这个。
我尝试了8类,并且预测很好,所以也许我需要一个更大的语料库,我将尝试在新的编辑中显示我的输出。
编辑2
现在,我使用五层[n,2n,4n,8n,16n]组成,其中n =类和步数= 20000,这确实减少了损失并提高了准确度,但它仍然可以与几个目标一起使用(10 aprox )的数量越大,预测就越错误。
毕竟,我对代码进行了一些更改,但是根本没有任何进展,因此我更改了DNN分类器的参数,并增加了语料库的大小,并且它可以正常工作。
最后,这是我的参数aprox:
-Steps = 25000+
-Layers = [n / 2,n,n * 2,n * 4,n * 8]
* n =数字类别
-Corpus大小= 30000个样本
-数字类别= 40
因此,这样做的损失等于0.0945 ...,准确度= 0.896 ...,不知道进行此更改是否可以帮助某人,但对我有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句