我想从text8语料库中用SVD (奇异值分解)在Tensorflow中创建向量表示。我使用了以下代码,但它没有使用多少维数:
u,s,v = tf.svd(coocurrence_matrix)我需要像科学学习中的TruncatedSVD这样的东西。我该怎么办?在Tensorflow中可以做同样的事情吗?
发布于 2017-07-10 17:42:04
我认为这是因为你正在做cs20si的第一份作业。形成你想要的任意维度的共生矩阵,比如(1000,1000)。一旦您有了单词(列表)和映射单词到索引的字典,就可以使用ndarray来形成并发矩阵,例如
cooccurrence_matrix = np.zeros((VOCAB_SIZE, VOCAB_SIZE))
n_words = len(words)
for i, current_word in enumerate(words):
if current_word not in dictionary:
current_word = 'UNK'
if i != 0:
left_word = words[i-1]
if left_word not in dictionary:
left_word = 'UNK'
cooccurrence_matrix[dictionary[current_word]][dictionary[left_word]] += 1
if i < n_words-1:
right_word = words[i+1]
if right_word not in dictionary:
right_word = 'UNK'
cooccurrence_matrix[dictionary[current_word]][dictionary[right_word]] += 1
print cooccurrence_matrix.shape在此之后,您可以直接使用tf.svd,因为它只需要一个张量。
tf_svd = tf.svd(matrix, compute_uv=True)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
svd, u, v = sess.run(tf_svd, feed_dict={matrix:cooccurrence_matrix})tf.svd的输出将有三个值,如tf.svd文档中所提到的。我会从字典大小100开始,看看事情是否还好。
https://stackoverflow.com/questions/44224914
复制相似问题