关于tf.keras.layers.Dense与tf.keras.layers.Flatten的区别

tf.keras.layers.Dense 全连接层

该层的效果是将输入的最高维(也就是最后一维)转成指定的维度数

如下例,将[5,2]形状的向量输入给Dense(8),最后输出形状维[5,8]

import tensorflow as tf
import numpy as np

input_data = np.arange(10).reshape(5,2)
input_data
'''
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])
'''
x1 = tf.keras.layers.Dense(8)(input_data)
x1
'''
<tf.Tensor: shape=(5, 8), dtype=float32, numpy=
array([[-0.12366056, -0.6986265 ,  0.77080464, -0.24608284,  0.40985382,
         0.28273118,  0.30870295, -0.1619857 ],
       [ 0.5133357 , -0.72846746,  1.3266207 ,  0.33265072,  2.6006622 ,
        -0.22271645,  0.5417254 ,  0.72683674],
       [ 1.150332  , -0.7583084 ,  1.8824368 ,  0.91138434,  4.7914705 ,
        -0.7281641 ,  0.77474785,  1.6156592 ],
       [ 1.7873282 , -0.78814936,  2.438253  ,  1.4901178 ,  6.982279  ,
        -1.2336117 ,  1.0077703 ,  2.5044816 ],
       [ 2.4243245 , -0.8179903 ,  2.9940686 ,  2.0688515 ,  9.173087  ,
        -1.7390594 ,  1.2407928 ,  3.393304  ]], dtype=float32)>
'''
tf.keras.layers.Flatten扁平层

该层的效果是将除第一维度(也就是batch_size这一维)外的所有维度,都转成一个维度。

如下例: 向量形状为[2,2,5],通过Flatten层后,变为[2,10]形状。即将第一维之外的所有维数相乘:2x5

input_data1 = np.arange(20).reshape(2,2,5)
input_data1
'''
array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9]],

       [[10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19]]])
'''

x2_f = tf.keras.layers.Flatten()(x2)
x2_f
'''
<tf.Tensor: shape=(2, 10), dtype=int32, numpy=
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]])>
'''
  • 14
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值