在word2vec训练的时候,生成处理随机词向量的时候,可以根据tf.random_uniform()获取随机矩阵。
#-*-coding:utf8-*-
import tensorflow as tf
with tf.Session() as sess:
print(sess.run(tf.random_uniform(
(4, 4), minval=-1,
maxval=1, dtype=tf.float32)))
实验结果:
"C:\Program Files\Anaconda3\python.exe" C:/Users/User/PycharmProjects/nlpdemo/word2vec/test_random_uniform.py
2018-01-09 11:04:04.450100: I C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
[[ 0.66339421 -0.11007571 0.06089711 -0.2449348 ]
[ 0.92638063 -0.21169639 -0.77011728 -0.75142145]
[ 0.82014132 0.24973822 -0.56280303 -0.15671754]
[ 0.2885654 -0.05570364 0.14831567 0.39260006]]
Process finished with exit code 0