tf.multiply dot tf.matmul用法

import tensorflow as tf
a = tf.constant([[1,2,3],[1,2,3]])

b = tf.constant([[2,3,4]])

b_1 = tf.constant([[2,3],[1,2],[3,4]])
print("a",a)
print("b",b)
print("b_1",b_1)

c = a*b # (2,3)*(1,3)->(2,3) 两个矩阵中对应元素各自相乘
print("c",c)

d = tf.multiply(a,b)# (2,3)*(1,3)->(2,3) 两个矩阵中对应元素各自相乘
print("d",d)

e = tf.matmul(a,b_1)# 矩阵乘法(2,3)*(3,2)->(2,2)
print("e",e)
c = tf.Print(c,[c])

with tf.Session() as sess:
    print(sess.run(c))
    print(sess.run(d))
    print(sess.run(e))

output:

a Tensor("Const:0", shape=(2, 3), dtype=int32)
b Tensor("Const_1:0", shape=(1, 3), dtype=int32)
b_1 Tensor("Const_2:0", shape=(3, 2), dtype=int32)
c Tensor("mul:0", shape=(2, 3), dtype=int32)
d Tensor("Mul_1:0", shape=(2, 3), dtype=int32)
e Tensor("MatMul:0", shape=(2, 2), dtype=int32)
2018-10-22 13:46:41.956674: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
[[ 2  6 12]
 [ 2  6 12]]
[[2 6 12]...]
[[ 2  6 12]
 [ 2  6 12]]
[[13 19]
 [13 19]]

Process finished with exit code 0

你可能感兴趣的:(tensorflow实战)