tf-矩阵相关知识

1、向量 or 矩阵

    # 向量(3,)
    v1 = tf.constant([ 1, 2, 3])
    print(v1.shape)

    ## 矩阵(3, 1)
    v2 = tf.constant([ [1], [2], [3]])
    print(v2.shape)
[
   [
	   [ 1.  4.  9. 16.]
	   [ 2.  6. 12. 20.]
	   [ 3.  8. 15. 24.]
   ],
   [ 
		[ 2.  6. 12. 20.]
		[ 4.  9. 16. 25.]
		[ 6. 12. 20. 30.]
   ]
]

2x3x4

2、最后一维升维

    ## 矩阵最后一维增加维度
    q = tf.constant([
                        [ 1, 2, 3],
                        [ 2, 4, 6]
                    ])

    q_expand = tf.expand_dims(q, axis=-1)  # ?, h, m, 1
    print(q_expand)
tf.Tensor(
[[[1]
  [2]
  [3]]

 [[2]
  [4]
  [6]]], shape=(2, 3, 1), dtype=int32)
import tensorflow as tf

if __name__ == '__main__':
    y_deep = tf.constant([[1,2,3],
                          [1,3, 4]], dtype=tf.int64)
    res = tf.reshape(y_deep, shape=[-1])

    print(res)


    y_deep_2 = tf.constant([[[1, 2, 3],[1, 3 , 7]],
                            [[2, 4, 6], [1, 3, 5]],
                            [[3, 6, 9], [1, 3, 6]]], dtype=tf.int64)
    res_2 = tf.reshape(y_deep_2, shape=[-1, 6])

    print(res_2)

拓展:模型训练中三维矩阵如何表示 (batch_size, rows, cols, dim)

先不考虑batch,例如图中xk为(3, 4) x0为(4, 4) 按照图中的计算方式,最后的计算结果维度为(3, 4, 4),其实有个误区就是容易把最终结果表示成(dim,hk, m) 即最后的2维度切片为hk*m,但是这个不是个
实现过程如下:

在这里插入图片描述

    #2.遍历D列,对于x0与xl所在的第i列进行外积计算,存在feature_maps中
    for i in range(D):
        # transpose_b=True 将 x0_cols[i] 转置
        feature_map = tf.matmul(xl_cols[i], x0_cols[i], transpose_b=True)  # 外积 ?, h, m
        print("i:", i, "first part:",  "xl_cols[i]", xl_cols[i], "x0_cols[i]:", x0_cols[i], feature_map)
        feature_map = tf.expand_dims(feature_map, axis=-1)  # ?, h, m, 1
        feature_maps.append(feature_map)
        print("i:", i,  "feature_map:", feature_map)
    print("feature_maps:",feature_maps)
    # 3.得到 h × m × D 的三维tensor
    feature_maps = Concatenate(axis=-1)(feature_maps)  # ?, h, m, D
i: 0 first part: xl_cols[i] tf.Tensor(
[[[1.]
  [2.]]], shape=(1, 2, 1), dtype=float32) x0_cols[i]: tf.Tensor(
[[[1.]
  [2.]
  [3.]]], shape=(1, 3, 1), dtype=float32) tf.Tensor(
[[[1. 2. 3.]
  [2. 4. 6.]]], shape=(1, 2, 3), dtype=float32)
i: 0 feature_map: tf.Tensor(
[[[[1.]
   [2.]
   [3.]]

  [[2.]
   [4.]
   [6.]]]], shape=(1, 2, 3, 1), dtype=float32)
i: 1 first part: xl_cols[i] tf.Tensor(
[[[2.]
  [3.]]], shape=(1, 2, 1), dtype=float32) x0_cols[i]: tf.Tensor(
[[[2.]
  [3.]
  [4.]]], shape=(1, 3, 1), dtype=float32) tf.Tensor(
[[[ 4.  6.  8.]
  [ 6.  9. 12.]]], shape=(1, 2, 3), dtype=float32)
i: 1 feature_map: tf.Tensor(
[[[[ 4.]
   [ 6.]
   [ 8.]]

  [[ 6.]
   [ 9.]
   [12.]]]], shape=(1, 2, 3, 1), dtype=float32)
i: 2 first part: xl_cols[i] tf.Tensor(
[[[3.]
  [4.]]], shape=(1, 2, 1), dtype=float32) x0_cols[i]: tf.Tensor(
[[[3.]
  [4.]
  [5.]]], shape=(1, 3, 1), dtype=float32) tf.Tensor(
[[[ 9. 12. 15.]
  [12. 16. 20.]]], shape=(1, 2, 3), dtype=float32)
i: 2 feature_map: tf.Tensor(
[[[[ 9.]
   [12.]
   [15.]]

  [[12.]
   [16.]
   [20.]]]], shape=(1, 2, 3, 1), dtype=float32)
i: 3 first part: xl_cols[i] tf.Tensor(
[[[4.]
  [5.]]], shape=(1, 2, 1), dtype=float32) x0_cols[i]: tf.Tensor(
[[[4.]
  [5.]
  [6.]]], shape=(1, 3, 1), dtype=float32) tf.Tensor(
[[[16. 20. 24.]
  [20. 25. 30.]]], shape=(1, 2, 3), dtype=float32)
i: 3 feature_map: tf.Tensor(
[[[[16.]
   [20.]
   [24.]]

  [[20.]
   [25.]
   [30.]]]], shape=(1, 2, 3, 1), dtype=float32)
feature_maps: [<tf.Tensor: shape=(1, 2, 3, 1), dtype=float32, numpy=
array([[[[1.],
         [2.],
         [3.]],

        [[2.],
         [4.],
         [6.]]]], dtype=float32)>, <tf.Tensor: shape=(1, 2, 3, 1), dtype=float32, numpy=
array([[[[ 4.],
         [ 6.],
         [ 8.]],

        [[ 6.],
         [ 9.],
         [12.]]]], dtype=float32)>, <tf.Tensor: shape=(1, 2, 3, 1), dtype=float32, numpy=
array([[[[ 9.],
         [12.],
         [15.]],

        [[12.],
         [16.],
         [20.]]]], dtype=float32)>, <tf.Tensor: shape=(1, 2, 3, 1), dtype=float32, numpy=
array([[[[16.],
         [20.],
         [24.]],

        [[20.],
         [25.],
         [30.]]]], dtype=float32)>]
Concatenate feature_maps: tf.Tensor(
[[[[ 1.  4.  9. 16.]
   [ 2.  6. 12. 20.]
   [ 3.  8. 15. 24.]]

  [[ 2.  6. 12. 20.]
   [ 4.  9. 16. 25.]
   [ 6. 12. 20. 30.]]]], shape=(1, 2, 3, 4), dtype=float32)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值