我正在寻找使用 tensorflow 解决简单文本分类问题的解决方案。我用 IMDB 数据集制作了一个模型来了解评论是正面的还是负面的。数据是通过 word2vec 处理的,所以现在我有一堆向量可以分类。我认为我的问题是由于 y_labels 的形状不好,因为它们是一维的,我想通过带有两类输出的 tensorflow 对它们进行分类,或者我错了。最终信息,该模型运行良好,精度为 1.0,可能太好了!感谢您的帮助!
X_train called train_vecs = (25000, 300) dtype: float64
X_test called test_vecs = (25000, 300) dtype: float64
y_test = shape (25000, 1) dtype: int64
y_train = shape: (25000, 1) dtype: int64
x = tf.placeholder(tf.float32, shape = [None, 300])
y = tf.placeholder(tf.float32, shape = [None, 2])
# Input -> Layer 1
W1 = tf.Variable(tf.zeros([300, 2]))
b1 = tf.Variable(tf.zeros([2]))
#h1 = tf.nn.sigmoid(tf.matmul(x, W1) + b1)
# Calculating difference between label and output
pred = tf.nn.softmax(tf.matmul(x, W1) + b1)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred,y))
train_step = tf.train.GradientDescentOptimizer(0.3).minimize(cost)
with tf.Session() as sess:
for i in xrange(200):
init_op = tf.initialize_all_variables()
sess.run(init_op)
train_step.run(feed_dict = {x: train_vecs, y: y_train})
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
# Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print "Accuracy:", accuracy.eval({x: test_vecs, y: y_test})
请您参考如下方法:
您在示例中使用了 softmax。 Softmax 将概率分配给 N 个不同的类别,其中概率加起来为 1。基本上,模型正在选择 N 个选项中的一个。为了使这个有意义,您需要 N 至少为 2。当 N == 1 时,该类的概率将始终为 1。您有两个可能的修复方法:
- 创建两个类,一个用于“积极情绪”,一个用于“消极情绪”,将 N 设置为 2。
- 使用逻辑回归代替 Softmax。在逻辑回归中,每个类都是独立的。这意味着您有 N 个问题,每个问题都有自己的"is"或“否”答案,这对于 N == 1 是有意义的。
