深度学习之Python常用函数

1.常用参数解析工具

argparse       #解析命令行参数
configparser   #解析配置文件
json           #解析配置文件
tf.app.flags   #采用的是tensorflow库中自带的tf.app.flags模块实现命令行参数的解析。
               #如果用终端运行tf程序,用上述两种方式都可以,如果用spyder之类的工具,那么只有argparse方式有用,
               #tf.app.flags模块方式会报错。    

2.常用函数

获取当前目录
os.getcwd()
参考:http://www.runoob.com/python/os-getcwd.html

xml文件解析工具
xml.etree.ElementTree
ElementTree生来就是为了处理XML,它在Python标准库中有两种实现:一种是纯Python实现的,如xml.etree.ElementTree,另一种是速度快一点的xml.etree.cElementTree。注意:尽量使用C语言实现的那种,因为它速度更快,而且消耗的内存更少。
参考:https://www.cnblogs.com/xiaobingqianrui/p/8405813.html

参考:http://www.runoob.com/python/python-func-enumerate.html
list()函数将元组转换为列表
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中

参考:http://www.runoob.com/python3/python3-func-reversed.html
reversed() 函数返回一个反转的迭代器

strip()函数
用于移除字符串头尾指定的字符(默认为空格)。
参考:http://www.runoob.com/python3/python3-string-strip.html

split()函数
通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串
参考:http://www.runoob.com/python3/python3-string-split.html

int() 函数
用于将一个字符串或数字转换为整型。
参考:http://www.runoob.com/python/python-func-int.html

numpy.save
numpy.savez
numpy.load
numpy文件存取函数,提供了多种存取数组内容的文件操作函数。保存数组数据的文件可以是二进制格式或者文本格式。二进制格式的文件又分为NumPy专用的格式化二进制类型和无格式类型。
参考:https://docs.scipy.org/doc/numpy/reference/generated/numpy.savez.html
参考:https://blog.csdn.net/pursuitbeauty/article/details/42169821

numpy产生随机数
参考:https://blog.csdn.net/jinxiaonian11/article/details/53143141

list 列表,python内置的一种数据类型是列表:list是一种有序的数据集合,可以随意的添加和删除其中的数据。
tuple 元组,和列表类似,元组也是一种有序列表,虽然tuple和list非常之类似,list初始化之后使可以改变的,但是,元组一旦初始化之后就不可以改变。
现在tuple不能变了,它也没有append(),insert()这样的方法。其他获取元素的方法和list是一样的,你可以正常地使用tuple[0],tuple[-1],但不能赋值成另外的元素。
不可变的tuple有什么意义?因为tuple不可变,所以代码更安全。如果可能,能用tuple代替list就尽量用tuple。
参考:https://blog.csdn.net/liuyanfeier/article/details/53731239

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
副标题: Mastering Basic Algorithms in the Python Language 作者: Magnus Lie Hetland 出版社: Apress 出版年: 2010-11-24 页数: 336 定价: USD 49.99 装帧: Paperback ISBN: 9781430232377 内容简介 · · · · · ·   Python Algorithms explains the Python approach to algorithm analysis and design. Written by Magnus Lie Hetland, author of Beginning Python, this book is sharply focused on classical algorithms, but it also gives a solid understanding of fundamental algorithmic problem-solving techniques. * The book deals with some of the most important and challenging areas of programming and computer science, but in a highly pedagogic and readable manner. * The book covers both algorithmic theory and programming practice, demonstrating how theory is reflected in real Python programs. * Well-known algorithms and data structures that are built into the Python language are explained, and the user is shown how to implement and evaluate others himself. What you'll learn * Transform new problems to well-known algorithmic problems with efficient solutions, or show that the problems belong to classes of problems thought not to be efficiently solvable. * Analyze algorithms and Python programs both using mathematical tools and basic experiments and benchmarks. * Prove correctness, optimality, or bounds on approximation error for Python programs and their underlying algorithms. * Understand several classical algorithms and data structures in depth, and be able to implement these efficiently in Python. * Design and implement new algorithms for new problems, using time-tested design principles and techniques. * Speed up implementations, using a plethora of tools for high-performance computing in Python. Who this book is for The book is intended for Python programmers who need to learn about algorithmic problem-solving, or who need a refresher. Students of computer science, or similar programming-related topics, such as bioinformatics, may also find the book to be quite useful. Table of Contents * Introduction * The Basics * Counting 101 * Induction and Recursion ...and Reduction * Traversal: The Skeleton Key of Algorithmics * Divide, Combine, and Conquer * Greed Is Good? Prove It! * Tangled Dependencies and Memoization * From A to B with Edsger and Friends * Matchings, Cuts, and Flows * Hard Problems and (Limited) Sloppiness
深度学习常用的损失函数有很多种,下面是几种常见的损失函数及其实现方式: 1. 均方误差(Mean Squared Error,MSE)损失函数: MSE损失函数适用于回归问题,计算预测值与真实值之间的平均平方差。它可以用来评估模型的预测效果。 公式:MSE = 1/n * Σ(y_pred - y_true)^2 在Python中,可以使用以下代码实现MSE损失函数的计算: ```python import numpy as np def mse_loss(y_pred, y_true): return np.mean(np.square(y_pred - y_true)) ``` 2. 交叉熵(Cross Entropy)损失函数: 交叉熵损失函数适用于分类问题,对于多分类问题可以使用分类交叉熵损失函数,对于二分类问题可以使用二分类交叉熵损失函数。 公式(多分类):CrossEntropy = -1/n * Σ(y_true * log(y_pred)) 公式(二分类):BinaryCrossEntropy = -1/n * (y_true * log(y_pred) + (1 - y_true) * log(1 - y_pred)) 在Python中,可以使用以下代码实现交叉熵损失函数的计算: ```python import numpy as np def cross_entropy_loss(y_pred, y_true): return -np.mean(y_true * np.log(y_pred)) def binary_cross_entropy_loss(y_pred, y_true): return -np.mean(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred)) ``` 3. 对数损失(Log Loss)损失函数: 对数损失函数常用于二分类问题中,特别适用于处理概率预测问题,将模型输出的概率与真实标签的对数概率之差作为损失值。 公式:LogLoss = -1/n * Σ(y_true * log(y_pred) + (1 - y_true) * log(1 - y_pred)) 在Python中,可以使用以下代码实现对数损失函数的计算: ```python import numpy as np def log_loss(y_pred, y_true): return -np.mean(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred)) ``` 这些是深度学习常用的损失函数及其实现方式,根据具体的任务和需求,选择合适的损失函数能够提高模型的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿尔发go

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值