2017-05-28 99 views
1

我想在Tensorflow創建從text8語料庫矢量表示與SVD(奇異值分解)。我用下面這段代碼,但它沒有采取維數:SVD在Tensorflow

u,s,v = tf.svd(coocurrence_matrix) 

我需要這樣的東西TruncatedSVD in scikit-learn。我該怎麼辦?是否可以在Tensorflow中做同樣的事情?

回答

1

我把它當作你從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開始,看看事情是否會好起來。