tf.reduce_prod用法及tf.placehoder用法
阿新 • • 發佈:2019-02-15
''' tf.reduce_prod( input_tensor, axis=None, keepdims=None, name=None, reduction_indices=None, keep_dims=None ) 此函式計算一個張量的各個維度上元素的乘積(張量沿著某一維度計算乘積)。 Computes the product of elements across dimensions of a tensor. (deprecated arguments)。 這裡的prod應該是product(乘積) 函式中的input_tensor是按照axis中已經給定的維度來減少的;除非 keep_dims 是true,否則張量的秩將在axis的每個條目中減少1; 如果keep_dims為true,則減小的維度將保留為長度1。 如果axis沒有條目,則縮小所有維度,並返回具有單個元素的張量。 引數: input_tensor:要減少的張量。應該有數字型別。 axis:要減小的尺寸。如果為None(預設),則將縮小所有尺寸。必須在[-rank(input_tensor), rank(input_tensor))範圍內。沿著哪個維度縮減, 哪個維度就不存在了。 keep_dims:如果為true,則保留長度為1的縮小維度。 name:操作的名稱(可選)。 reduction_indices:axis的廢棄的名稱。 返回: 結果返回減少的張量。 ''' import tensorflow as tf a = tf.constant([i+1 for i in range(6)], shape=[2, 3]) sess = tf.Session() b = tf.reduce_prod(a) c = tf.reduce_prod(a, 0) d = tf.reduce_prod(a, 1) e = tf.reduce_prod(a, 1, keep_dims=True) f = tf.reduce_prod(a, [0, 1]) print("b: ", sess.run(b)) print("c: ", sess.run(c)) print("d: ", sess.run(d)) print("e: ", sess.run(e)) print("f: ", sess.run(f)) sess.close() output: b: 720 c: [ 4 10 18] d: [ 6 120] e: [[ 6] [120]] f: 720 ''' tf.placeholder( dtype, shape=None, name=None ) 佔位符,可以理解成C語言中的巨集定義 ''' import tensorflow as tf import numpy as np s_1_flex = (None, None, None) a = tf.placeholder(dtype=tf.int32, shape=s_1_flex, name="my_input") b = tf.reduce_prod(a, name="prod_b") c = tf.reduce_sum(a, name="sum_c") d = tf.add(b, c, name="add_d") sess = tf.Session() #input_array = np.array([i+1 for i in range(24)]).reshape([2, 2, 3]) input_array = np.arange(1, 25, 1).reshape([3, 2, 4]) #tensor.reshape(a, b, c)表示每一維度的元素數目 input_dict = {a: input_array} sess.run(c, feed_dict=input_dict) output: 300