我想构建一个文本分类网络,它使用一袋单词表示输入 . 第一层是密集字嵌入的查找和求和 . 我想用小批量训练 .

https://github.com/tensorflow/tensorflow/issues/342#issuecomment-160354041之后,我使用 nn.embedding_lookup_sparse() 从一个小批量加载Bag of Words到一个张量 .

其余的几乎是标准的神经网络,具有 nn.sparse_softmax_cross_entropy_with_logits() 损失函数和随机梯度下降优化 .

当我在具有更多40个CPU内核的机器上运行我的代码时,我只实现了~200%的cpu利用率,因此38个内核没有任何有用的工作 . 我发现这很奇怪,因为小批量的梯度计算可以大规模并行化,梯度下降优化应该是非阻塞的Hogwild风格......

这是一些更多的代码

vocabularySize = 146532
embeddingSize = 300
batchsize = 640

trainInputs = tf.placeholder(tf.int64)
trainLabels = tf.placeholder(tf.int64,shape=(batchsize))
sp_indices = tf.placeholder(tf.int64)

sp_shape = tf.constant([batchsize, vocabularySize],tf.int64)
embeddings = tf.Variable(tf.random_uniform([vocabularySize, embeddingSize], -1.0, 1.0))
sparseBOW = tf.SparseTensor(sp_indices, trainInputs, sp_shape)
h1 = tf.nn.relu(tf.nn.embedding_lookup_sparse(embeddings,sparseBOW,None))

inner = tf.Variable(tf.truncated_normal([embeddingSize, 32],stddev=1.0 / math.sqrt(float(embeddingSize))))
biases = tf.Variable(tf.zeros([32]))
h2 = tf.nn.relu(tf.matmul(h1,inner) + biases)

softmax_weights = tf.Variable(tf.truncated_normal([32, 2],stddev=1.0 / math.sqrt(32)))
softmax_biases = tf.Variable(tf.zeros([2]))
logits = tf.matmul(h2,softmax_weights) + softmax_biases

loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits, trainLabels))

optimizer = tf.train.GradientDescentOptimizer(0.001).minimize(loss)

#...ommiting the data loading part. I load everything into python datastructures first, so there is no file io involved.
_, loss_val = sess.run([optimizer, loss], feed_dict=feedDict)

有没有办法分析精确计算时间的位置并确定瓶颈?这甚至是处理稀疏输入数据的正确方法吗?我应该去密集吗?