'''
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
參考文檔:https://blog.csdn.net/jshmjs45/article/details/82935660#tf.reduce_prod