【发布时间】:2016-05-13 02:51:43
【问题描述】:
我正在寻找一个简单的 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})
【问题讨论】:
标签: python machine-learning tensorflow word2vec