美文网首页
装饰器助力Tensor flow 模型构筑, =,= , “材

装饰器助力Tensor flow 模型构筑, =,= , “材

作者: 炎阳狮子_______头 | 来源:发表于2017-12-31 14:57 被阅读0次

Class Model

class Model:

    def __init__(self, data, target):
        data_size = int(data.get_shape()[1])
        target_size = int(target.get_shape()[1])
        weight = tf.Variable(tf.truncated_normal([data_size, target_size]))
        bias = tf.Variable(tf.constant(0.1, shape=[target_size]))
        incoming = tf.matmul(data, weight) + bias
        self._prediction = tf.nn.softmax(incoming)
        cross_entropy = -tf.reduce_sum(target, tf.log(self._prediction))
        self._optimize = tf.train.RMSPropOptimizer(0.03).minimize(cross_entropy)
        mistakes = tf.not_equal(
            tf.argmax(target, 1), tf.argmax(self._prediction, 1))
        self._error = tf.reduce_mean(tf.cast(mistakes, tf.float32))

    @property
    def prediction(self):
        return self._prediction

    @property
    def optimize(self):
        return self._optimize

    @property
    def error(self):
        return self._error

@property装饰器可以将类函数与其属性相关联。但是以上的方法可读性和复利用性太差。

Use property

class Model:

    def __init__(self, data, target):
        self.data = data
        self.target = target
        self._prediction = None
        self._optimize = None
        self._error = None

    @property
    def prediction(self):
        if not self._prediction:
            data_size = int(self.data.get_shape()[1])
            target_size = int(self.target.get_shape()[1])
            weight = tf.Variable(tf.truncated_normal([data_size, target_size]))
            bias = tf.Variable(tf.constant(0.1, shape=[target_size]))
            incoming = tf.matmul(self.data, weight) + bias
            self._prediction = tf.nn.softmax(incoming)
        return self._prediction

    @property
    def optimize(self):
        if not self._optimize:
            cross_entropy = -tf.reduce_sum(self.target, tf.log(self.prediction))
            optimizer = tf.train.RMSPropOptimizer(0.03)
            self._optimize = optimizer.minimize(cross_entropy)
        return self._optimize

    @property
    def error(self):
        if not self._error:
            mistakes = tf.not_equal(
                tf.argmax(self.target, 1), tf.argmax(self.prediction, 1))
            self._error = tf.reduce_mean(tf.cast(mistakes, tf.float32))
        return self._error

Emmm 存在lazy_loading 的问题 , 这里还不太理解。
Code is still a bit bloated due to the lazy loading logic

Lazy Property Decorator

import functools

def  lazy_property(function):
    attribute = '_cache_'  +  function.__name__
    
    @property
    @functools.wrap(function)
    def  decorator(self, attribute):
        if not hasattr(self, attribute):
            setattr(self, attribute, function(self))
        return getattr(self , attribute)

    return decorator

装饰器——以后翻译

通过这个装饰器可以简化模型

class Model:

    def __init__(self, data, target):
        self.data = data
        self.target = target
        self.prediction
        self.optimize
        self.error

    @lazy_property
    def prediction(self):
        data_size = int(self.data.get_shape()[1])
        target_size = int(self.target.get_shape()[1])
        weight = tf.Variable(tf.truncated_normal([data_size, target_size]))
        bias = tf.Variable(tf.constant(0.1, shape=[target_size]))
        incoming = tf.matmul(self.data, weight) + bias
        return tf.nn.softmax(incoming)

    @lazy_property
    def optimize(self):
        cross_entropy = -tf.reduce_sum(self.target, tf.log(self.prediction))
        optimizer = tf.train.RMSPropOptimizer(0.03)
        return optimizer.minimize(cross_entropy)

    @lazy_property
    def error(self):
        mistakes = tf.not_equal(
            tf.argmax(self.target, 1), tf.argmax(self.prediction, 1))
        return tf.reduce_mean(tf.cast(mistakes, tf.float32))

Note that we mention the properties in the constructor. This way the full graph is ensured to be defined by the time we run tf.initialize_variables().

定义计算图的范围

同过函数定义

import functools

def define_scope(function):
    attribute = '_cache_' + function.__name__

    @property
    @functools.wraps(function)
    def decorator(self):
        if not hasattr(self, attribute):
            with tf.variable_scope(function.__name__):
                setattr(self, attribute, function(self))
        return getattr(self, attribute)

    return decorator

插入tf.variable_scope(function.name) 或者 tf.name_scope(function.name) 来定义Scope。

自定义Scope

def doublewrap(function):
    """
    A decorator decorator, allowing to use the decorator to be used without parentheses
    if not arguments are provided. All arguments must be optional.
    """
    @functools.wraps(function)
    def decorator(*args, **kwargs):
        if len(args)  == 1 and len(kwargs) == 0 and callable(args[0]):
            return function(args[0])
        else:
            return lambda wrapee: function(wrapee, *args, **kwargs)
    return decorator

@doublewrap
def define_scope(function, scope=None, *args, **kwargs):
    """
     A decorator for functions that define TensorFlow operations. The wrapped
    function will only be executed once. Subsequent calls to it will directly
    return the result so that operations are added to the graph only once.
    The operations added by the function live within a tf.variable_scope(). If
    this decorator is used with arguments, they will be forwarded to the
    variable scope. The scope name defaults to the name of the wrapped
    function
    """
    attribute = '_cache_' + function.__name__
    name = scope or function.__name__

    @property
    @functools.wraps(function)
    def decorator(self):
        if not hasattr(self, attribute):
            with tf.variable_scope(name, *args, **kwargs):
                setattr(self, attribute, function(self))
        return getattr(self, attribute)

    return decorator

双层装饰器以保证无参时也可照常调用。

本文参考翻译自 Danijar Hafner

相关文章

  • 装饰器助力Tensor flow 模型构筑, =,= , “材

    Class Model @property装饰器可以将类函数与其属性相关联。但是以上的方法可读性和复利用性太差。 ...

  • TensorFlow入门

    TensorFlow计算模型--计算图 计算图的概念 TensorFlow两个重要概念:Tensor和Flow,T...

  • 2018-12-29

    一 、tensorflow=tensor(张量)+flow(流) (代码+演示) 1、tensor(张量) ...

  • Docker 常用命令记录

    模型训练 由于按照tensor-flow 或者pytorch 等依赖众多,目前基本上就是走docker 镜像的模式...

  • 深度学习可视化之Tensorboard

    一、什么是tensorboard 让tensor可视化地flow起来。(来来来,让tensor先flow一会。) ...

  • Tensor flow

    Tensorflow 十一个深度学习的框架

  • tensor flow教程

    1.coursera-tensorflow-in-practice2.tensorflow官网tutorials

  • 20161225Three things list

    1. First --> tensor flow white paper. 2. second level -->...

  • 重绘和重排

    参考 浏览器渲染机制 浏览器采用流式布局模型(Flow Based Layout)。 浏览器会把 HTML 解析成...

  • web前端重点内容

    布局、选择器、权重、盒模型、Hack、CSS预处理器,页面加载如何优化 Flexbox、Document flow...

网友评论

      本文标题:装饰器助力Tensor flow 模型构筑, =,= , “材

      本文链接:https://www.haomeiwen.com/subject/eatagxtx.html