tf.constant
constant( value, dtype=None, shape=None, name='Const', verify_shape=False )
功能說明:
根據 value 的值生成一個 shape 維度的常量張量
參數列表:
參數名 | 必選 | 類型 | 說明 |
---|---|---|---|
value | 是 | 常量數值或者 list | 輸出張量的值 |
dtype | 否 | dtype | 輸出張量元素類型 |
shape | 否 | 1 維整形張量或 array | 輸出張量的維度 |
name | 否 | string | 張量名稱 |
verify_shape | 否 | Boolean | 檢測 shape 是否和 value 的 shape 一致,若為 Fasle,不一致時,會用最后一個元素將 shape 補全 |
#!/usr/bin/python import tensorflow as tf import numpy as np a = tf.constant([1,2,3,4,5,6],shape=[2,3]) b = tf.constant(-1,shape=[3,2]) c = tf.matmul(a,b) e = tf.constant(np.arange(1,13,dtype=np.int32),shape=[2,2,3]) f = tf.constant(np.arange(13,25,dtype=np.int32),shape=[2,3,2]) g = tf.matmul(e,f) with tf.Session() as sess: print (sess.run(a)) print ("##################################") print (sess.run(b)) print ("##################################") print (sess.run(c)) print ("##################################") print (sess.run(e)) print ("##################################") print (sess.run(f)) print ("##################################") print (sess.run(g))