zoukankan      html  css  js  c++  java
  • 机器学习中的python常用函数

    lstrip()方法

    lstrip() 方法用于截掉字符串左边的空格或指定字符

    str.lstrip([chars])    截掉指定的字符char

    返回截掉指定字符的字符串

    str = "     this is string example....wow!!!     ";
    print( str.lstrip() );# this is string example....wow!!!     
    str = "88888888this is string example....wow!!!8888888";
    print( str.lstrip('8') );    # this is string example....wow!!!8888888

     

    random.seed()

    放一个改变随机数生成器的种子,每个seed()值对应着一个固定的随机操作(生成随机数、随机洗牌)

    import random
    random.seed ([x])

    x :改变随机数生成器的种子seed。如果不设置,Python会帮你选择seed值。

    import random
    
    random.seed(10)    # 生成同一个随机数
    print("带种子的随机数10: ", random.random())
    # 带种子的随机数10:  0.57140259469
    
    random.seed(10)    # 生成同一个随机数
    print("带种子的随机数10: ", random.random())
    # 带种子的随机数10 10 :  0.57140259469

    random.shuffle()

    将序列的所有元素随机排序。

    import random

    random.shuffle(lit)

    import random
    
    lit = [20, 16, 10, 5]
    
    random.shuffle(lit)
    print("随机排序列表 : ", lit)     # 随机排序列表 :  [20, 10, 5, 16]

    tf.cast()

    tf.cast(x, dtype, name=None)

    将x的数据格式转化成dtype,例如,原来x的数据格式是bool,那么将其转化成float以后,就能将其转化成 0 和 1 的序列

    import tensorflow as tf
    
    a = tf.Variable([1,0,0,1,1])
    b = tf.cast(a,dtype=tf.bool)
    sess = tf.Session()
    sess.run(tf.initialize_all_variables())
    print(sess.run(b))
    #[ True False False  True  True]

    tf.concat

    tf.concat相当于numpy中的np.concatenate函数,用于将两个张量在某一维度(axis)合并起来,

    a = tf.constant([[1,2,3],[3,4,5]]) # shape (2,3)
    b = tf.constant([[7,8,9],[10,11,12]]) # shape (2,3)
    ab1 = tf.concat([a,b], axis=0) # shape(4,3)
    ab2 = tf.concat([a,b], axis=1) # shape(2,6)

    tf.stack

    tf.stack 产生新的阶,并进行拼接张量,增加维度

    a = tf.constant([[1,2,3],[3,4,5]]) # shape (2,3)
    b = tf.constant([[7,8,9],[10,11,12]]) # shape (2,3)
    ab = tf.stack([a,b], axis=0) # shape (2,2,3)

    axis是决定其层叠(stack)张量的维度方向的,改变参数axis=2

    import tensorflow as tf
    a = tf.constant([[1,2,3],[3,4,5]]) # shape (2,3)
    b = tf.constant([[7,8,9],[10,11,12]]) # shape (2,3)
    ab = tf.stack([a,b], axis=2) # shape (2,3,2)

    tf.unstack

    tf.unstacktf.stack的操作相反,是将一个高阶数的张量在某个axis上分解为低阶数的张量

    a = tf.constant([[1,2,3],[3,4,5]]) # shape (2,3)
    b = tf.constant([[7,8,9],[10,11,12]]) # shape (2,3)
    ab = tf.stack([a,b], axis=0) # shape (2,2,3)
    
    a1 = tf.unstack(ab, axis=0)
    
    # a1的输出为
    # [<tf.Tensor 'unstack_1:0' shape=(2, 3) dtype=int32>,
    #  <tf.Tensor 'unstack_1:1' shape=(2, 3) dtype=int32>]

    tf.transpose()函数

    这个函数主要适用于交换输入张量的不同维度用,如果输入张量是二维,就相当是转置。如果张量是三维,就是用0,1,2来表示。这个列表里的每个数对应相应的维度。如果是[2,1,0],就把输入张量的第三维度和第一维度交换。

    import tensorflow as tf
    import numpy as np
    
    A = np.array([[1, 2, 3],
                  [4, 5, 6]])
    print(A.shape)      # (2,3)
    x = tf.transpose(A, [1, 0])
    print(x.shape)      # (3,2)
    
    B = np.array([[[1, 2, 3, 4],
                   [5, 6, 7, 8],
                   [9, 10, 11, 12]],
    
                  [[13, 14, 15, 16],
                   [17, 18, 19, 20],
                   [21, 22, 23, 24]]])
    print(B.shape)      # (2,3,4)
    y = tf.transpose(B, [2, 1, 0])
    print(y.shape)      # (4,3,2)

    enumerate()

    enumerate() 函数用于将一个可遍历的数据对象组合为元组,同时返回数据下标数据,一般用在 for 循环当中

    seasons = ['Spring', 'Summer', 'Fall', 'Winter']
    print(list(enumerate(seasons)))
    # [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
    
    seq = ['one', 'two', 'three']
    for i, element in enumerate(seq):
        print(i, element)
    
    # 0 one
    # 1 two
    # 2 three

    zip()

      将可迭代对象打包成一个个元组,然后返回包含这些元组的列表

    语法:zip([iterable, ...])

    a = [1, 2, 3]
    b = [4, 5, 6]
    c = [4, 5, 6, 7, 8]
    zipped = zip(a, b)     # 打包为元组的列表
    # [(1, 4), (2, 5), (3, 6)]
    zip(a, c)              # 元素个数与最短的列表一致
    # [(1, 4), (2, 5), (3, 6)]
    zip(*zipped)          # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式
    # [(1, 2, 3), (4, 5, 6)]

    tf.clip_by_global_norm理解

    梯度剪裁一般的应用场景为

    optimizer = tf.train.AdamOptimizer(self.learning_rate)
    gradients, v = zip(*optimizer.compute_gradients(self.loss))
    gradients, _ = tf.clip_by_global_norm(gradients, self.grad_clip)
    updates_train_optimizer = optimizer.apply_gradients(zip(gradients, v), global_step=self.global_step)

    梯度剪裁最直接的目的就是防止梯度暴躁,手段就是控制梯度的最大范式

    tf.clip_by_global_norm(t_list, clip_norm, use_norm=None, name=None)

    参数:

    • t_list:输入梯度
    • clip_norm:裁剪率
    • clip_norm:要使用的全球规范

    返回:

    • list_clipped:裁剪后的梯度列表
    • global_norm:全局的规约数

    但是,它比clip_by_norm()慢,因为在执行剪裁操作之前,必须准备好所有参数

    tensorflow中的多线程管理

      Tensorflow的Session对象支持多线程,可以在一个会话中创建多个线程,并执行,在Session中所有线程都必须同步终止。

      Tensorflow提供两个类来实现对Session中多线程的管理,tf.Coordinator()tf.QueueRunner(),往往一起使用

    tf.Coordinator()类 用来停止Session中的多个工作线程,并且向那个在等待的工作线程 发送终止程序报告异常,该线程捕获到这个异常之后就会终止所有线程。使用 tf.train.Coordinator()来创建一个线程管理器(协调器)对象。

    tf.QueueRunner()类 用来启动tensor的入队线程,可以用来启动多个线程同时将多个tensor训练数据推送到队列中,具体执行函数是tf.train.start_queue_runners

      只有调用 tf.train.start_queue_runners 之后,才会真正把tensor推入内存序列中,供计算单元调用,否则会由于内存序列为空,数据流图会处于一直等待状态。

    调用 tf.train.Coordinator  创建一个线程协调器,用来管理之后在Session中启动所有线程 

    调用 tf.train.start_queue_runners  启动入队线程,由多个或单个线程,按规则把文件读入Filename Queue中。

    参考:tensorflow中协调器 tf.train.Coordinator 和入队线程启动器 tf.train.start_queue_runners

    rand生成一个[0~1]之间2行100列的数组

    randn生成服从正态分布的数组

    tf.ConfigProto()

    https://blog.csdn.net/lanchunhui/article/details/50163669

    https://blog.csdn.net/dcrmg/article/details/79780331

    https://www.cnblogs.com/adong7639/p/8136273.html

    https://www.cnblogs.com/MY0213/p/9208503.html

    https://blog.csdn.net/u011509971/article/details/70244688

    tf.contrib.layers.xavier_initializer_conv2d

    xavier_initializer初始化的基本思想是保持输入和输出的方差一致,这样就避免了所有的输出值都趋向于0。

    这个初始化器是用来保持每一层的梯度大小都差不多相同。

  • 相关阅读:
    (七)mysql 记录长度
    (六)列类型
    (五)校对集
    (四)中文数据问题
    Spring Boot Jpa 的使用
    Spring Boot:如何优雅的使用 Mybatis
    Spring Boot:定时任务
    Spring Boot 小技巧
    【重磅】Spring Boot 2.0权威发布
    Spring Boot + Jpa + Thymeleaf 增删改查示例
  • 原文地址:https://www.cnblogs.com/LXP-Never/p/10074603.html
Copyright © 2011-2022 走看看