ENAS的代码详解——common_ops.py

本系列采用GitHub上的tensorflow实现的ENAS。
github:https://github.com/melodyguan/enas
首先是操作集文件common_ops.py。


import numpyas np

import tensorflowas tf

def lstm(x, prev_c, prev_h, w):

    ifog = tf.matmul(tf.concat([x, prev_h],axis=1), w)

    i, f, o, g = tf.split(ifog,4,axis=1)

    i = tf.sigmoid(i)

    f = tf.sigmoid(f)
 
    o = tf.sigmoid(o)

    g = tf.tanh(g)

    next_c = i * g + f * prev_c

    next_h = o * tf.tanh(next_c)

    return next_c, next_h

def stack_lstm(x, prev_c, prev_h, w):

    next_c, next_h = [], []

    for layer_id, (_c, _h, _w) in enumerate(zip(prev_c, prev_h, w)):

        inputs = xif layer_id ==0 else next_h[-1]

        curr_c, curr_h = lstm(inputs, _c, _h, _w)

        next_c.append(curr_c)

        next_h.append(curr_h)

    return next_c, next_h

def create_weight(name, shape, initializer=None, trainable=True, seed=None):

    if initializeris None:

        initializer = tf.contrib.keras.initializers.he_normal(seed=seed)

        return tf.get_variable(name, shape,initializer=initializer,trainable=trainable)

def create_bias(name, shape, initializer=None):

    if initializeris None:
 
        initializer = tf.constant_initializer(0.0,dtype=tf.float32)

        return tf.get_variable(name, shape,initializer=initializer)

定义一段基本的LSTM程序。

def lstm(x, prev_c, prev_h, w):

    ifog = tf.matmul(tf.concat([x, prev_h],axis=1), w)

    i, f, o, g = tf.split(ifog,4,axis=1)

    i = tf.sigmoid(i)

    f = tf.sigmoid(f)
 
    o = tf.sigmoid(o)

    g = tf.tanh(g)

    next_c = i * g + f * prev_c

    next_h = o * tf.tanh(next_c)

    return next_c, next_h

定义层叠的LSTM,即把多个LSTM接起来。这里的prev_c,prev_h,w都是List。
具体来说,就是先建立了空的LSTM中间状态和输出列表。然后对每一层执行如下:
如果是第0层,那么输入设置为x,如果不是,那么输入为上一个的输出。next_h[-1]为列表的倒数第一个元素。
用刚才的LSTM函数求出当前的中间状态c和输出h。
用新建的next_c和next_h的List收集curr_c和curr_h。

def stack_lstm(x, prev_c, prev_h, w):

    next_c, next_h = [], []

    for layer_id, (_c, _h, _w) in enumerate(zip(prev_c, prev_h, w)):
    //enumerate将一个可迭代变量变成编号加元素的形式
    //zip将一个
        inputs = x if layer_id ==0 else next_h[-1]

        curr_c, curr_h = lstm(inputs, _c, _h, _w)

        next_c.append(curr_c)

        next_h.append(curr_h)

    return next_c, next_h

新建权重。
在没有initializer的情况下,给出he normal的initializer。其中he normal参考https://www.cv-foundation.org/openaccess/content_iccv_2015/html/He_Delving_Deep_into_ICCV_2015_paper.html
返回一个形状为shape的矩阵。

def create_weight(name, shape, initializer=None, trainable=True, seed=None):

    if initializeris None:

        initializer = tf.contrib.keras.initializers.he_normal(seed=seed)

        return tf.get_variable(name, shape,initializer=initializer,trainable=trainable)

新建偏置。
细节和上面类似。

def create_bias(name, shape, initializer=None):

    if initializeris None:
 
        initializer = tf.constant_initializer(0.0,dtype=tf.float32)

        return tf.get_variable(name, shape,initializer=initializer)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 杨宁宁 焦点解决网络初17中19坚持分享274天(2020.04.19) 本周约练 5次,总计27次; 读书打卡第...
    3e5c5362403c阅读 242评论 0 0
  • 2020年4月19日 周复盘 1.这周做了什么事?有什么成果? a. 读书1本,《认知天性》:学习的三个步骤:编码...
    我爱语雯阅读 233评论 0 0
  • 闭包是指有权访问另一个函数作用域中的变量的函数。 闭包的作用: 可以在函数的外部访问到函数内部的局部变量。 让这些...
    sll_阅读 192评论 0 0
  • 东望七闽,南望五岭,览群山之参差,俯章贡之奔流,云烟出没,草木蕃丽,邑屋相望,鸡犬之声相闻. ...
    不惑_cf78阅读 260评论 0 0
  • 《不似春》 文/木青灯 桥过出新柳,春风冷面寒。 是春不闹意,冬恋是明欢。
    木青灯阅读 428评论 2 17