1. 程式人生 > >tf從截斷的正態分佈中生成隨機值

tf從截斷的正態分佈中生成隨機值

在word2vec中計算初始權重的時候,生成正態分佈隨機權重,正態分佈隨機權重的函式tf.truncated_normal()

#-*-coding:utf8-*-
import tensorflow as tf
a = tf.Variable(tf.random_normal([2,2],seed=1))
b = tf.Variable(tf.truncated_normal([2,2],seed=2))
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print(sess.run(a))
    print(sess.run(b))
實驗結果:
"C:\Program Files\Anaconda3\python.exe" C:/Users/User/PycharmProjects/nlpdemo/word2vec/test_truncated_normal.py
2018-01-09 11:45:45.673100: 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.81131822  1.48459876]
 [ 0.06532937 -2.4427042 ]]
[[-0.85811085 -0.19662298]
 [ 0.13895045 -1.22127676]]

Process finished with exit code 0