机器学习中的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)]

 

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

 

 

 

 

"theme": "Adaptive.sublime-theme"

转载于:https://www.cnblogs.com/LXP-Never/p/10074603.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值