tensorflow点乘与矩阵乘

原文链接:https://www.cnblogs.com/liuq/p/9330134.html

点乘和矩阵乘的区别:

1. 点乘(即“ * ”) ---- 各个矩阵对应元素做乘法

若 w 为 m*1 的矩阵,x 为 m*n 的矩阵,那么通过点乘结果就会得到一个 mn 的矩阵。
在这里插入图片描述
若 w 为 m*n 的矩阵,x 为 m*n 的矩阵,那么通过点乘结果就会得到一个 m
n 的矩阵。
在这里插入图片描述
w的列数只能为 1 或与x的列数相等(即n),w的行数与x的行数相等 才能进行乘法运算。

2. 矩阵乘 ---- 按照矩阵乘法规则做运算

若 w 为 m*p 的矩阵,x 为 p*n 的矩阵,那么通过矩阵相乘结果就会得到一个 m*n 的矩阵。
只有w 的列数 == x的行数 时,才能进行乘法运算。
在这里插入图片描述

代码示例

1. numpy

点乘

import numpy as np

w = np.array([[0.4], [1.2]])
x = np.array([range(1,6), range(5,10)])

print("w:")
print(w)
print("x:")
print(x)
print("点乘w * x:")
print(w * x)

运行结果

w:
[[0.4]
 [1.2]]
x:
[[1 2 3 4 5]
 [5 6 7 8 9]]
点乘w * x:
[[ 0.4  0.8  1.2  1.6  2. ]
 [ 6.   7.2  8.4  9.6 10.8]]

矩阵乘

import numpy as np
w = np.array([[0.4, 1.2]])
x = np.array([range(1,6), range(5,10)])

print("w:")
print(w)
print("x:")
print(x)
print("矩阵乘w * x:")
print(np.dot(w, x))

运行结果

w:
[[0.4 1.2]]
x:
[[1 2 3 4 5]
 [5 6 7 8 9]]
矩阵乘w * x:
[[ 6.4  8.   9.6 11.2 12.8]]

tensorflow

点乘

import tensorflow as tf
w = tf.Variable([[0.4], [1.2]], dtype=tf.float32)
x = tf.Variable([[1.0, 2.0, 3.0, 4.0, 5.0],[6.0, 7.0, 8.0, 9.0, 10.0]], dtype=tf.float32)
y1 = w * x
y2 = tf.multiply(w, x)

w2 = tf.Variable(0.1, dtype=tf.float32)
y3 = w2 * x
y4 = tf.multiply(w2, x)

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print("w:")
    print(sess.run(w))
    print("w.shape:")
    print(w.shape)
    print("x:")
    print(sess.run(x))
    print("x.shape:") 
    print(x.shape)
    print("y1:")
    print(sess.run(y1))
    print("y1.shape:")
    print(y1.shape)
    print("y2:")
    print(sess.run(y2))
    print("y2.shape:")
    print(y2.shape)
    print("--------")
    print("w2:")
    print(sess.run(w2))
    print("w2.shape")
    print(w2.shape)
    print("y3:")
    print(sess.run(y3))
    print("y3.shape:")
    print(y3.shape)
    print("y4:")
    print(sess.run(y4))
    print("y4.shape:")
    print(y4.shape)

运行结果

w:
[[0.4]
 [1.2]]
w.shape:
(2, 1)
x:
[[ 1.  2.  3.  4.  5.]
 [ 6.  7.  8.  9. 10.]]
x.shape:
(2, 5)
y1:
[[ 0.4        0.8        1.2        1.6        2.       ]
 [ 7.2000003  8.400001   9.6       10.8       12.       ]]
y1.shape:
(2, 5)
y2:
[[ 0.4        0.8        1.2        1.6        2.       ]
 [ 7.2000003  8.400001   9.6       10.8       12.       ]]
y2.shape:
(2, 5)
--------
w2:
0.1
w2.shape
()
y3:
[[0.1        0.2        0.3        0.4        0.5       ]
 [0.6        0.7        0.8        0.90000004 1.        ]]
y3.shape:
(2, 5)
y4:
[[0.1        0.2        0.3        0.4        0.5       ]
 [0.6        0.7        0.8        0.90000004 1.        ]]
y4.shape:
(2, 5)

矩阵乘

import tensorflow as tf
w = tf.Variable([[0.4, 1.2]], dtype=tf.float32)
x = tf.Variable([[1.0, 2.0, 3.0, 4.0, 5.0],[6.0, 7.0, 8.0, 9.0, 10.0]], dtype=tf.float32)
y = tf.matmul(w, x)

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print("w:")
    print(sess.run(w))
    print("w.shape:")
    print(w.shape)
    print("x:")
    print(sess.run(x))
    print("x.shape:") 
    print(x.shape)
    print("y:")
    print(sess.run(y))
    print("y.shape:")
    print(y.shape)

运行结果

w:
[[0.4 1.2]]
w.shape:
(1, 2)
x:
[[ 1.  2.  3.  4.  5.]
 [ 6.  7.  8.  9. 10.]]
x.shape:
(2, 5)
y:
[[ 7.6000004  9.200001  10.8       12.400001  14.       ]]
y.shape:
(1, 5)
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值