nic*_*vid 1 python machine-learning word2vec tensorflow
我正在寻找一个解决一个简单的文本分类问题与tensorflow.我用IMDB数据集建立了一个模型,知道评论是积极的还是消极的.数据是通过word2vec处理的,所以现在我有一堆矢量来分类.我认为我的问题是由于y_labels的形状不好,因为它们是一个维度,我想通过张量流对两个类输出进行分类,或者我错了.最后的信息,该模型运行良好,精度为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})
Run Code Online (Sandbox Code Playgroud)
您在示例中使用softmax.Softmax将概率分配给N个不同的类,其中概率加起来为1.基本上,模型恰好选择N个选项中的一个.为了理所当然,你需要N至少为2.当N == 1时,该类的概率总是为1.你有两个可能的修复:
| 归档时间: |
|
| 查看次数: |
600 次 |
| 最近记录: |