探究functools模块wraps装饰器的用途

内联函数online的定义和作用 说明: 内联函数是一个小型函数,以牺牲空间来节省函数调用带来的额外开销,一般用以比较小函数或者使用频率很高的函数,函数内部内部没有复杂的语句,如开关语句switch,循环语句for等 定义: (1)在 C++ 中,可以在定义函数时,在返回值类型前面加上 inline 关键字 inline int max() { ...... return 0; } (二)直接在类的内部进行函数的声明和定义,则默认其是内联函数 class A { public: int max() { ...... ret 阅读详情

探究functools模块wraps装饰器的用途

《A Byte of Python》17.8节讲decorator的时候,用到了functools模块中的一个装饰器:wraps。因为之前没有接触过这个装饰器,所以特地研究了一下。

何谓“装饰器”?

《A Byte of Python》中这样讲:

“Decorators are a shortcut to applying wrapper functions. This is helpful to “wrap” functionality with the same code over and over again.”

《Python参考手册(第4版)》6.5节描述如下:

“装饰器是一个函数,其主要用途是包装另一个函数或类。这种包装的首要目的是透明地修改或增强被包装对象的行为。”

Python官方文档中这样定义:

“A function returning another function, usually applied as a function transformation using the @wrapper syntax. Common examples for decorators are classmethod() and staticmethod().”

让我们来看一下《Python参考手册》上6.5节的一个例子(有些许改动):

# coding: utf-8
# Filename: decorator_wraps_test.py
# 2014-07-05 18:58
import sys

debug_log = sys.stderr

def trace(func):
    if debug_log:
        def callf(*args, **kwargs):
            """A wrapper function."""
            debug_log.write('Calling function: {}\n'.format(func.__name__))
            res = func(*args, **kwargs)
            debug_log.write('Return value: {}\n'.format(res))
            return res
        return callf
    else:
        return func

@trace
def square(x):
    """Calculate the square of the given number."""
    return x * x

if __name__ == '__main__':
    print(square(3))

输出:

Calling function: square
Return value: 9
9

这个例子中,我们定义了一个装饰器trace,用于追踪函数的调用过程及函数调用的返回值。如果不用装饰器语法,我们也可以这样写:

def _square(x):
    return x * x

square = trace(_square)

上面两段代码,使用装饰器语法的版本和不用装饰器语法的版本实际上是等效的。只是当我们使用装饰器时,我们不必再手动调用装饰器函数。

嗯。trace装饰器看起来棒极了!假设我们把如上代码提供给其他程序员使用,他可能会想看一下square函数的帮助文档:

from decorator_wraps_test import square
help(square) # print(square.__doc__)
Help on function callf in module decorator_wraps_test:

callf(*args, **kwargs)
    A wrapper function.

看到这样的结果,使用decorator_wraps_test.py模块的程序员一定会感到困惑。他可能会带着疑问敲入如下代码:

print(square.__name__) 
callf

这下,他可能会想看一看decorator_wraps_test.py的源码,找一找问题究竟出现在了哪里。我们知道,Python中所有对象都是“第 一类”的。比如,函数(对象),我们可以把它当作普通的数据对待:我们可以把它存储到容器中,或者作为另一个函数的返回值。上面的程序中,在 debug_log为真的情况下,trace会返回一个函数对象callf。这个函数对象就是一个“闭包”,因为当我们通过:

def _square(x): return x * x
square = trace(_square)

把trace返回的callf存储到square时,我们得到的不仅仅是callf函数执行语句,还有其上下文环境:

>>> print('debug_log' in square.__globals__)
True
>>> print('sys' in square.__globals__)
True

因此,使用装饰器修饰过的函数square,实际上是一个trace函数返回的“闭包”对象callf,这就揭示了上面help(square)以及print(square.__name__)的输出结果了。

那么,怎样才能在使用装饰器的基础上,还能让help(square)及print(square.__name__)得到我们期待的结果呢?这就是functools模块的wraps装饰器的作用了。

让我们先看一看效果:

# coding: utf-8
# Filename: decorator_wraps_test.py
# 2014-07-05 18:58
import functools
import sys

debug_log = sys.stderr

def trace(func):
    if debug_log:
        @functools.wraps(func)
        def callf(*args, **kwargs):
            """A wrapper function."""
            debug_log.write('Calling function: {}\n'.format(func.__name__))
            res = func(*args, **kwargs)
            debug_log.write('Return value: {}\n'.format(res))
            return res
        return callf
    else:
        return func

@trace
def square(x):
    """Calculate the square of the given number."""
    return x * x

if __name__ == '__main__':
    print(square(3))
    print(square.__doc__)
    print(square.__name__)

输出:

Calling function: square
Return value: 9
9
Calculate the square of the given number.
square

很完美!哈哈。这里,我们使用了一个带参数的wraps装饰器“装饰”了嵌套函数callf,得到了预期的效果。那么,wraps的原理是什么呢?

首先,简要介绍一下带参数的装饰器:

def trace(log_level):
    def impl_f(func):
        print(log_level, 'Implementing function: "{}"'.format(func.__name__))
        return func
    return impl_f

@trace('[INFO]')
def print_msg(msg): print(msg)

[INFO] Implementing function: "print_msg"
@trace('[DEBUG]')
def assert_(expr): assert expr

[DEBUG] Implementing function: "assert_"
print_msg('Hello, world!')
Hello, world!

这段代码定义了一个带参数的trace装饰器函数。因此:

@trace('[INFO]')
def print_msg(msg): print(msg)

等价于:

temp = trace('[INFO]')
def _print_msg(msg): print(msg)
print_msg = temp(_print_msg)

相信这样类比一下,带参数的装饰器就很好理解了。(当然,这个例子举得并不好。《Python参考手册》上有一个关于带参数的装饰器的更好的例子,感兴趣的童鞋可以自己看看 。)

接下来,让我们看看wraps这个装饰器的代码吧!

让我们先找到functools模块文件的路径:

import functools
functools.__file__
'D:\\Program Files\\Python34\\lib\\functools.py'

下面,把wraps相关的代码摘录出来:

WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__qualname__', '__doc__',
                       '__annotations__')
WRAPPER_UPDATES = ('__dict__',)
def update_wrapper(wrapper,
                   wrapped,
                   assigned = WRAPPER_ASSIGNMENTS,
                   updated = WRAPPER_UPDATES):
    """Update a wrapper function to look like the wrapped function

       wrapper is the function to be updated
       wrapped is the original function
       assigned is a tuple naming the attributes assigned directly
       from the wrapped function to the wrapper function (defaults to
       functools.WRAPPER_ASSIGNMENTS)
       updated is a tuple naming the attributes of the wrapper that
       are updated with the corresponding attribute from the wrapped
       function (defaults to functools.WRAPPER_UPDATES)
    """
    for attr in assigned:
        try:
            value = getattr(wrapped, attr)
        except AttributeError:
            pass
        else:
            setattr(wrapper, attr, value)
    for attr in updated:
        getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
    # Issue #17482: set __wrapped__ last so we don't inadvertently copy it
    # from the wrapped function when updating __dict__
    wrapper.__wrapped__ = wrapped
    # Return the wrapper so this can be used as a decorator via partial()
    return wrapper

def wraps(wrapped,
          assigned = WRAPPER_ASSIGNMENTS,
          updated = WRAPPER_UPDATES):
    """Decorator factory to apply update_wrapper() to a wrapper function

       Returns a decorator that invokes update_wrapper() with the decorated
       function as the wrapper argument and the arguments to wraps() as the
       remaining arguments. Default arguments are as for update_wrapper().
       This is a convenience function to simplify applying partial() to
       update_wrapper().
    """
    return partial(update_wrapper, wrapped=wrapped,
                   assigned=assigned, updated=updated)

从代码中可以看到,wraps是通过functools模块中另外两个函数:partial和update_wrapper来实现其功能的。让我们看一看这两个函数:

1. partial函数

partial函数实现对函数参数进行部分求值(《Python参考手册》中4.9有这么一句:函数参数的部分求值与叫做柯里化(currying)的过程关系十分密切。虽然不是太明白,但感觉很厉害的样子!:

这里,我们通过partial为foo提供参数z的值,得到了一个新的“函数对象”(这里之所以加个引号是因为foo_without_z和一般的函数对象有些差别。比如,foo_without_z没有__name__属性。)foo_without_z。因此,本例中:

foo_without_z(10, 20)

等价于:

foo(10, 20, z = 100)

(比较有趣的一点是,foo_without_z没有__name__属性,而其文档字符串__doc__也和partial的文档字符串很相像。此外, 我认为,这里的partial和C++标准库中的bind1st、bind2nd这些parameter binders有异曲同工之妙。这里没有把partial函数的实现代码摘录出来,有兴趣的童鞋可以自己研究一下它的工作原理。)

因此,wraps函数中:

return partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)

实际上是返回一个对update_wrapper进行部分求值的“函数对象”。因此,上例中使用了wraps装饰器的decorator_wraps_test.py的等价版本如下:

def trace(func): if debug_log: def _callf(*args, **kwargs): """A wrapper function.""" debug_log.write('Calling function: {}\n'.format(func.__name__)) res = func(*args, **kwargs) debug_log.write('Return value: {}\n'.format(res)) return res _temp = functools.wraps(func) callf = _temp(_callf) return callf else: return func

对wraps也进行展开:

def trace(func): if debug_log: def _callf(*args, **kwargs): """A wrapper function.""" debug_log.write('Calling function: {}\n'.format(func.__name__)) res = func(*args, **kwargs) debug_log.write('Return value: {}\n'.format(res)) return res _temp = functools.partial(functools.update_wrapper, wrapped = func, assigned = functools.WRAPPER_ASSIGNMENTS, updated = functools.WRAPPER_UPDATES) callf = _temp(_callf) return callf else: return func

最后,对partial的调用也进行展开:

def trace(func): if debug_log: def _callf(*args, **kwargs): """A wrapper function.""" debug_log.write('Calling function: {}\n'.format(func.__name__)) res = func(*args, **kwargs) debug_log.write('Return value: {}\n'.format(res)) return res callf = functools.update_wrapper(_callf, wrapped = func, assigned = functools.WRAPPER_ASSIGNMENTS, updated = functools.WRAPPER_UPDATES) return callf else: return func

这次,我们看到的是很直观的函数调用:用_callf和func作为参数调用update_wrapper函数。

2. update_wrapper函数

update_wrapper做的工作很简单,就是用参数wrapped表示的函数对象(例如:square)的一些属性(如:__name__、 __doc__)覆盖参数wrapper表示的函数对象(例如:callf,这里callf只是简单地调用square函数,因此可以说callf是 square的一个wrapper function)的这些相应属性。

因此,本例中使用wraps装饰器“装饰”过callf后,callf的__doc__、__name__等属性和trace要“装饰”的函数square的这些属性完全一样。

经过上面的分析,相信你也了解了functools.wraps的作用了吧。

最后,《A Byte of Python》一书讲装饰器的时候提到了一篇博客:DRY Principles through Python Decorators 。。

functools.wrap的使用 是 Python 标准库中的一个函数,用于帮助创建装饰器时保留被装饰函数的元数据(如函数名、文档字符串等)。在使用装饰器时,如果不使用,则被装饰函数的一些元数据可能会丢失或被覆盖。 阅读详情

相关推荐

通俗易懂的分析——python装饰器之@functools.warps

首先,宝宝觉着网上没有比我这还透彻的@functools.warps分析了~~~害羞自恋ing……栗子详情:http://stackoverflow.com/questions/308999/what-does-functools-wraps-do

遇到问题写下来,为了以后方便查啊~ 3269

Python基础总结之functools.wraps介绍与应用

是一个简洁而实用的工具,它在编写装饰器时起到了重要的作用,帮助我们保留原函数的元信息,增强代码的可读性和可维护性。无论是在日志记录、访问控制还是异步编程中,都是一个不可或缺的利器。希望本文对你理解和使用能有所帮助。

载_酒i 1716

《Python 装饰器进阶指南:正名之术与 functools.wraps 的魔法》

本文深入探讨Python装饰器使用中常见的元信息丢失问题,并提出functools.wraps的解决方案。装饰器虽然强大,但会覆盖原函数的__name__、__doc__等元数据,影响调试和文档生成。通过@functools.wraps(func)可以完美保留函数身份信息,文中提供了计时器、日志记录等实用案例,并强调多层装饰器都应使用wraps。文章还分析了装饰器在现代框架(如FastAPI、pytest)中的广泛应用,总结出"始终使用wraps"等最佳实践,帮助开发者构建更健壮、可维护

铭渊老黄 835

@functools.wraps的作用

@functools.wraps的作用

kindtester的博客 873

Python functools之wraps装饰器详解

在Python的functools模块中,wraps是一个非常有用的装饰器,主要用于保留被装饰函数的元信息(如函数名、文档字符串等)。当我们使用装饰器时,被装饰的函数会丢失其原有的元信息,而wraps可以帮助我们解决这个问题。Python的functools.wraps是一个强大的工具,能够帮助我们在使用装饰器时保留原函数的重要元信息。它不仅提高了代码的可读性和可维护性,还确保了与其他依赖函数签名的工具和框架的兼容性。在编写装饰器时,使用wraps几乎已经成为了一种最佳实践。

Web安全工具库 902

基于51单片机的简易计算器设计(原理图+仿真+源代码+参考论文)

本设计以51单片机为核心,矩阵键盘输入数字,通过1602显示屏显示计算结果,能够实现整数的加、减、乘、除四则运算,具备清除功能。

u012759563的博客 7735

python装饰器以及functools的wraps使用

一、闭包函数 在函数内部再定义一个函数,并且这个函数用到了外边函数的变量,那么将这个函数以及用到的一些变量称之为闭包 import sys debug_log = sys.stderr def trace(func): if debug_log: def callf(*args, **kwargs): """A wrapper function.""...

sheqianweilong的博客 1865

Robocorp+Robot Framework学习记录【初学者课程:一级课程】

以下内容均来自:https://robotframework.org/?tab=2#getting-started。通过官网的学习和本地实际安装使用体验而来,本文仅记录学习过程。 Robot Framework介绍 Robot Framework 是一个通用的开源自动化框架。它可用于测试自动化和机器人过程自动化 (RPA)。 Robot Framework环境配置及安装 具体请见:Win10环境下安装pytho、pip与Robot Framework 学习过程与等级划分 官方认为Robot Framew.

OwenDom的博客 5379

functools.wraps 有什么作用?

在对此 answer to another question 的评论中,有人说他们不确定 functools.wraps 在做什么。所以,我问这个问题,以便在 StackOverflow 上记录它以供将来参考:functools.wraps 究竟做了什么?

HuntsBot的博客 1316

手把手搭建摄像头驱动(一):I2C设备树插件与OV5640实战

本文手把手指导如何搭建OV5640摄像头驱动,重点讲解I2C设备树插件的编写与实战应用。通过硬件连接验证、I2C驱动框架搭建、设备树插件编译及动态加载调试,系统介绍Linux驱动开发全流程,并提供常见问题解决方案,帮助开发者快速掌握摄像头驱动开发技术。

git9versioner的博客 757

别光看原理图了!手把手教你用Altium Designer搞定STM32F411最小系统的PCB布局(附电源/晶振/复位电路实战)

本文详细介绍了使用Altium Designer进行STM32F411最小系统PCB布局的实战技巧,涵盖电源分配网络优化、高速信号布线及系统级布局策略。通过规范的工程结构建立、关键设计规则预设和核心电路模块布局,帮助工程师提升PCB设计效率与信号完整性。特别针对STM32最小系统的电源、晶振和复位电路提供了专业解决方案。

weixin_42653691的博客 175

Python进阶:装饰器保留原函数信息(functools.wraps)

《Python进阶:装饰器保留原函数信息(functools.wraps)》深入讲解了Python装饰器保留函数元信息的技术要点。文章从核心概念入手,阐述了functools.wraps的作用原理及其重要性,指出它能有效解决装饰器导致的函数元信息丢失问题。通过代码示例演示了基础用法和进阶应用,包括类装饰器的实现方法。同时提供了常见问题解决方案,如环境配置错误等。本文属于《Python从入门到精通教程》进阶篇,适合已掌握Python基础语法的开发者学习,帮助提升代码质量和开发效率。

未来 95% 的 AI 应用将变得隐形,广泛融入日常生活。 2431

Linux脚本实战之SSH执行远端脚本

Linux脚本实战之SSH连接主机

jks212454的博客 2135

python:functools.partial和functools.wraps使用

python内置的functools模块,提供了一些非常好用的类或者方法,其中functools.partial和functools.wraps的使用频率较高,本文将针对其分析使用。

a232884c的博客 1544

极智AI | 详解 ViT 算法实现

大家好,我是极智视界,本文详细介绍一下 ViT 算法的设计与实现,包括代码。

极智视界 5614

11.11华为OD机试真题 新系统 - 矿区最短开采段 (Java/Py/C/C++/Js/Go)

11.11华为OD机试真题 新系统 - 矿区最短开采段 (Java/Py/C/C++/Js/Go)

算法大师! 334

python装饰器中functools.wraps的作用详解

直接上代码看效果: # 定义一个最简单的装饰器   def user_login_data(f):     def wrapper(*args, **kwargs):       return f(*args, **kwargs)     return wrapper   # 用装饰器装饰以下两个函数      @user_login_data   def num1():     print("aaa")   @user_login_data   def num2():     print("b

python学习者的博客 2757

CAMERALINK硬件封装说明

闲言少叙,直接上活,Cameralink硬件接口当前主要分为MDR26和SDR26两种,其中MDR对应封装较大价格比较便宜,SDR对应尺寸较小价格比较贵,这点我个人总是记错的图1 MDR26实物图图2 SDR26实物图。

6196

python装饰器补充之functools包中的wraps

Python装饰器(decorator)在实现的时候,有一些细节需要被注意。例如,被装饰后的函数其实已经是另外一个函数了(函数名等函数属性会发生改变)。这样有时候会对程序造成一些不便,例如笔者想对flask框架中的一些函数添加自定义的decorator,添加后由于函数名和函数的doc发生了改变,对测试结果有一些影响。所以,Python的functools包中提供了一个叫wraps的decorato...

学海无涯 6686

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Python 中 functools.wraps 简介

在人类世界中有一些大佬,经过细心装扮之后,自身的属性可以变得让人雌雄莫辨。(我没有在说 Abbily,我是在说花木兰!) 在 Python 世界中也是一样,一些函数经过装饰器的悉心装饰之后,一些属性也会变得令人迷惑。 先定义一个啥都不干,但很淳朴的函数,顺便看看它的函数名和文档字符串: def iron_egg(): """我是铁蛋儿~""" return print(iron_egg.__name__) print(iron_egg.__doc__) """输出: iron_egg .

团子大圆帅的博客 1159

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

Python的functools模块完全教程

在python中。Java中则为。当一个函数将另一个函数作为输入或返回另一个函数作为输出时,这些函数称为高阶函数。functools模块是Python的标准库的一部分,它是为高阶函数而实现的,用于增强函数功能。

Good Luck 1369

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Python装饰器元数据保全指南:functools.wraps原理与实践

Python装饰器元数据保全指南:functools.wraps原理与实践

KE17RS的博客 1068

python functools import wraps_Python - 关于functools模块的wraps装饰器用途

#有关wraps的源码,有兴趣的可以自行研究下WRAPPER_ASSIGNMENTS= ('__module__', '__name__', '__qualname__', '__doc__','__annotations__')WRAPPER_UPDATES= ('__dict__',)defupdate_wrapper(wrapper,wrapped,assigned=WRAPPER_ASSI...

weixin_39946029的博客 320

Python functools.wraps功能

当需要复制额外的自定义属性时,可以扩展wraps"""扩展的wraps,支持自定义属性复制"""default_assigned = wraps.__wrapped__.__defaults__[0] # 获取默认assigneddefault_updated = wraps.__wrapped__.__defaults__[1] # 获取默认updated# 使用示例"""示例函数"""passexample.my_custom_attr = "自定义属性"

m0_46322965的博客 1002
上一篇: Python网站开发库大全
下一篇: Selenium基本命令和操作
唐僧不爱八戒
博客等级 码龄9年 158粉丝 84原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址