random.uniform()详解
生活随笔
收集整理的這篇文章主要介紹了
random.uniform()详解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、函數(shù)介紹
函數(shù)原型: numpy.random.uniform(low,high,size)
功能:從一個均勻分布[low,high)中隨機采樣,注意定義域是左閉右開,即包含low,不包含high.
random.uniform(shape,minval=0,maxval=None,dtype=dtypes.float32,seed=None,name=None)參數(shù)解釋:
- shape: 張量形狀
- minval: 隨機值范圍下限,默認0
- maxval: ? 隨機值范圍上限(若薇浮點數(shù),則默認為1)
- dtype: ? 輸出的類型:float16、float32、float64、int32、orint64
- seed: ? ?整數(shù)作為隨機數(shù)種子
- name: ?操作的名稱(可選)
二、舉例
代碼:
# -------創(chuàng)建詞匯查找表--------- vocab = ["<1H OCEAN", "INLAND", "NEAR OCEAN", "NEAR BAY", "ISLAND"] indices = tf.range(len(vocab),dtype=tf.int64) # 創(chuàng)建索引張量 table_init = tf.lookup.KeyValueTensorInitializer(vocab, indices) # 查找表初始化程序 num_oov_buckets = 2 # 詞匯表外的捅的大小(分配給未知單詞,如果太小則會出現(xiàn)重復) table = tf.lookup.StaticVocabularyTable(table_init, num_oov_buckets) # 創(chuàng)建查找表embedding_dim = 2 embed_init = tf.random.uniform([len(vocab) + num_oov_buckets, embedding_dim]) # 生成7*2的張量 embedding_matrix = tf.Variable(embed_init) print(embedding_matrix)輸出:
<tf.Variable 'Variable:0' shape=(7, 2) dtype=float32, numpy= array([[0.542737 , 0.8715787 ],[0.67160535, 0.14418924],[0.454713 , 0.17986405],[0.96360207, 0.4519266 ],[0.8082293 , 0.6823478 ],[0.33527482, 0.545668 ],[0.48199546, 0.8183578 ]], dtype=float32)>總結
以上是生活随笔為你收集整理的random.uniform()详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 考研不歧视双非的院校计算机专业,考研23
- 下一篇: 信息奥赛一本通《走迷宫》