zoukankan      html  css  js  c++  java
  • 神经网络之全连接层(线性层)

    对于神经网络的全连接层,前面已经使用矩阵的运算方式实现过,本篇将引入tensorflow中层的概念, 正式使用deep learning相关的API搭建一个全连接神经网络。下面是全连接神经网络的结构图

     其中,x1,x2,x3为输入,a1,a2,a3为输出,运算关系如下:

      
      
      
    x1,x2,x3所在的层叫神经网络的输入层,a1,a2,a3所在的层叫神经网络的输出层,如果两层中间还有若干层,那么中间的这些层叫做隐藏层。
    那么,如何使用tensorflow去创建这样的层呢?其实非常简单,只需要调用tf.keras.layers API即可,示例如下:
    # 模拟生成四张 28*28的图片数据
    x = tf.random.normal([4,784])
    # 搭建全连层,参数代表神经元个数
    net = tf.keras.layers.Dense(512)
    # 将x喂入net层,得到输出层
    out = net(x)
    print(out.shape)
    print(net.kernel.shape,net.bias.shape)

    net = tf.keras.layers.Dense(10)
    # 只是声明层结构并不会完成w和b的创建
    print(net.get_weights(),net.weights)
    # 使用build函数创建层
    net.build(input_shape=(None,4))
    print(net.kernel.shape,net.bias.shape)
    net.build(input_shape=(None,20))
    print(net.kernel.shape,net.bias.shape)
    net.build(input_shape=(2,4))
    print(net.kernel)

    如果要实现多层的嵌套,又该怎么做呢?示例如下:

    x = tf.random.normal([2,3])
    model = keras.Sequential([
        keras.layers.Dense(2,activation='relu'),
        keras.layers.Dense(2,activation='relu'),
        keras.layers.Dense(2)
    ])
    model.build(input_shape=[None,3])
    # summary用来显示网络具体信息,相当于print
    model.summary()
    # 通过下面的语句可以查看所有的w和b
    for p in model.trainable_variables:
        print(p.name,p.shape)

  • 相关阅读:
    工作所得所思之一
    angular路由
    html中offsetTop、clientTop、scrollTop、offsetTop各属性介绍
    您无权输入许可证密钥,请请使用系统管理员账户重试
    nginx的配置说明
    flexbox弹性盒子模型
    移动web开发规范
    为什么要使用响应式开发
    移动web开发
    如何给图片做缓存
  • 原文地址:https://www.cnblogs.com/zdm-code/p/12235800.html
Copyright © 2011-2022 走看看