元素乘法
要在張量上執行元素乘法,可以使用以下任一方法:
a*b
tf.multiply(a, b)
這是使用兩種方法的元素乘法的完整示例。
import tensorflow as tf
import numpy as np
# Build a graph
graph = tf.Graph()
with graph.as_default():
# A 2x3 matrix
a = tf.constant(np.array([[ 1, 2, 3],
[10,20,30]]),
dtype=tf.float32)
# Another 2x3 matrix
b = tf.constant(np.array([[2, 2, 2],
[3, 3, 3]]),
dtype=tf.float32)
# Elementwise multiplication
c = a * b
d = tf.multiply(a, b)
# Run a Session
with tf.Session(graph=graph) as session:
(output_c, output_d) = session.run([c, d])
print("output_c")
print(output_c)
print("
output_d")
print(output_d)
列印出以下內容:
output_c
[[ 2. 4. 6.]
[ 30. 60. 90.]]
output_d
[[ 2. 4. 6.]
[ 30. 60. 90.]]