tensorflow
行列とベクトルの算術演算
サーチ…
要素単位の乗算
テンソルで要素ごとの乗算を実行するには、次のいずれかを使用します。
-
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("\noutput_d")
print(output_d)
以下を印刷します。
output_c
[[ 2. 4. 6.]
[ 30. 60. 90.]]
output_d
[[ 2. 4. 6.]
[ 30. 60. 90.]]
Scalar Times a Tensor
次の例では、2 x 3テンソルにスカラー値(2)を掛けます。
# 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)
# Scalar times Matrix
c = 2 * a
# Run a Session
with tf.Session(graph=graph) as session:
output = session.run(c)
print(output)
これが印刷されます
[[ 2. 4. 6.]
[ 20. 40. 60.]]
ドットプロダクト
2つのテンソル間の内積は、以下を使用して実行できます。
tf.matmul(a, b)
完全な例を以下に示します。
# Build a graph
graph = tf.Graph()
with graph.as_default():
# A 2x3 matrix
a = tf.constant(np.array([[1, 2, 3],
[2, 4, 6]]),
dtype=tf.float32)
# A 3x2 matrix
b = tf.constant(np.array([[1, 10],
[2, 20],
[3, 30]]),
dtype=tf.float32)
# Perform dot product
c = tf.matmul(a, b)
# Run a Session
with tf.Session(graph=graph) as session:
output = session.run(c)
print(output)
プリントアウトする
[[ 14. 140.]
[ 28. 280.]]
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow