Navigation

    Gpushare.com

    • Register
    • Login
    • Search
    • Popular
    • Categories
    • Recent
    • Tags

    【推荐库】fastcore

    技术交流
    1
    1
    94
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 183****0229
      183****0229 last edited by 183****0229

      文档

      https://fastcore.fast.ai/

      安装

      pip install fastcore

      介绍

      Python是一种功能强大的动态语言。它不是把所有东西都融入到语言中,而是让程序员自定义语言,使之为自己工作。fastcore利用这种灵活性为Python添加了一些特性,这些特性的灵感来自于我们喜欢的其他语言,比如来自Julia的多重调度,来自Ruby的mixins,以及来自Haskell的curry, binding等等。它还添加了一些“缺失的特性”,并清理了Python标准库中的一些粗糙之处,比如简化并行处理,并将NumPy的想法引入到Python的列表类型中。

      L(类似python的list,但含有很多其他功能)

      from fastcore.all import *
      l = L(1,2,3)
      print(l)
      # (#3) [1,2,3]
      p = L.range(20).shuffle()
      print(p)
      # (#20) [0,10,7,16,5,1,14,17,9,8...]
      p.argwhere(ge(15))
      # (#5) [3,7,11,14,18]
      

      patch 和 patch_to

      使用这个可以轻松地给你所需要的类添加实例方法,当然也可以覆盖旧的实例方法
      patch(f=None, as_prop=False, cls_method=False)
      patch_to(cls, as_prop=False, cls_method=False)
      装饰器:将f添加到第一个参数的类中(基于f的类型注释)

      例子1

      # 自定义一个类_T8,继承自int
      class _T8(int): pass  
      # 给_T8添加一个方法,self+a
      @patch
      def func(self:_T8, a): return self+a
      
      # 给_T8添加一个方法,self+a
      @patch_to(_T8)
      def func2(self, a): return self+a
      
      # 初始化_T8,赋值为1
      t = _T8(1)
      print(t.func(10)) 
      print(t.func2(10))
      
      # 11 输出结果是11
      


      例子2(给tensorflow的tf.Tensor添加实例方法)

      这里我们来实现一个类似pytorch的tensor.sum()方法。

      from fastcore.utils import patch_to
      import tensorflow as tf
      x  = tf.random.normal((3,5))
      s_x1 = tf.reduce_sum(x,axis=1)
      
      # tf.Tensor没有sum的实例方法,这里我们给它自定义一个sum方法
      @patch_to(cls=[tf.Tensor, tf.Variable])
      def sum(self, axis=None, keepdims=False, name=None):
          return tf.reduce_sum(self, axis=axis, keepdims=keepdims, name=name)
      
      s_x2 = x.sum(axis=1)
      print(all(s_x1==s_x2))
      
      

      例子3 (当我们既想重写别人库里面某个类的实例方法,又不想修改其源代码的时候,可以使用这个函数来重写原来的方法。)

      from fastcore.all import *
      # 假设别人库定义了个Cat类,并且定义里实例方法call
      class Cat:
          def __init__(self, name):
              self.name = name
          
          def call(self):
              print(self.name, 'miao~')
      
      c = Cat(name="mimi")
      # 原先Cat的call方法打印miao~
      c.call()
      
      # 我们使用patch函数重写了该方法
      @patch
      def call(self:Cat): print(self.name, 'miaomiao~')
      # patch覆盖后,Cat的call方法打印miaomiao~
      c.call()
      
      # 我们使用patch_to函数重写了该方法
      # patch_to覆盖后,Cat的call方法打印miaomiaomiao~
      @patch_to(Cat)
      def call(self): print(self.name, 'miaomiaomiao~')
      c.call()
      

      更多的例子可以看
      https://github.com/JunnYu/tensorflow_fast_api

      1 Reply Last reply Reply Quote 2
      • First post
        Last post