Ich nehme es als Sie arbeiten die erste Aufgabe von cs20si. Bilden Sie die Kookkurrenzmatrix, die eine beliebige Dimension hat, sagen Sie (1000,1000). Sobald Sie die Wörter (eine Liste) und das Wörterbuch haben, die Worte auf Indizes abbildet, können Sie gleichzeitig Matrix wie
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
Danach bilden verwenden ndarray können Sie einfach direkt verwenden tf.svd, da es nur einen Tensor nimmt .
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})
Ausgabe von tf.svd werden drei Werte haben wie in der tf.svd Dokumentation erwähnt. Ich würde mit der Wörterbuchgröße 100 beginnen, um zu sehen, ob die Dinge in Ordnung sind.