没有合适的资源?快使用搜索试试~ 我知道了~
首页CS231 卷积神经网络(中文版,带书签)
CS231 卷积神经网络(中文版,带书签)

CS231 卷积神经网络(中文版,带书签), 整理 知乎:https://zhuanlan.zhihu.com/p/21930884, 部分小节自己翻译。
资源详情
资源评论
资源推荐

Python Numpy 教程
*
原文如下
这篇教程由Justin Johnson创作。
我们将使用 Python 编程语言来完成本课程的所有作业。Python 是一门伟大的通用编程语言,在
一些常用库(numpy, scipy, matplotlib)的帮助下,它又会变成一个强大的科学计算环境。
我们期望你们中大多数人对于 Python 语言和 Numpy 库比较熟悉,而对于没有 Python 经验的同
学,这篇教程可以帮助你们快速了解 Python 编程环境和如何使用 Python 作为科学计算工具。
一部分同学对于 Matlab 有一定经验。对于这部分同学,我们推荐阅读 numpy for Matlab users页
面。
你们还可以查看本教程的IPython notebook版。该教程是由Volodymyr Kuleshov和Isaac Caswell 为
课程CS 228创建的。
内容列表:
• Python
– 基本数据类型
– 容器
* 列表
* 字典
* 集合
* 元组
– 函数
– 类
• Numpy
– 数组
– 访问数组
– 数据类型
– 数组计算
– 广播
• SciPy
– 图像操作
*
Raby&Draby 整理. 原文来自 [知乎:https://zhuanlan.zhihu.com/p/21930884]
1

– MATLAB 文件
– 点之间的距离
• Matplotlib
– 绘制图形
– 绘制多个图形
– 图像
Python
Python 是一种高级的,动态类型的多范型编程语言。很多时候,大家会说 Python 看起来简直
和伪代码一样,这是因为你能够通过很少行数的代码表达出很有力的思想。举个例子,下面是用
Python 实现的经典的 quicksort 算法例子:
def quicksort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) / 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot ]
right = [x for x in arr if x > pivot]
return quicksort (left ) + middle + quicksort(right)
print quicksort ([3,6,8,10 ,1,2,1])
# Prints "[1, 1, 2, 3, 6, 8, 10]"
Python 版本
Python 有两个支持的版本,分别是 2.7 和 3.4。这有点让人迷惑,3.0 向语言中引入了很多不向
后兼容的变化,2.7 下的代码有时候在 3.4 下是行不通的。在这个课程中,我们使用的是 2.7 版本。
如何查看版本呢?使用 python –version命令。
基本数据类型
和大多数编程语言一样,Python 拥有一系列的基本数据类型,比如整型、浮点型、布尔型和字
符串等。这些类型的使用方式和在其他语言中的使用方式是类似的。
数字:整型和浮点型的使用与其他语言类似。
x = 3
print type(x) # Prints "<type 'int '>"
print x # Prints "3"
print x + 1 # Addition; prints "4"
print x - 1 # Subtraction ; prints "2"
print x * 2 # Multiplication ; prints "6"
print x ** 2 # Exponentiation ; prints "9"
x += 1
2

print x # Prints "4"
x *= 2
print x # Prints "8"
y = 2.5
print type(y) # Prints "<type 'float '>"
print y, y + 1, y * 2, y ** 2 # Prints "2.5 3.5 5.0 6.25"
需要注意的是,Python 中没有 x++ 和 x– 的操作符。
Python 也有内置的长整型和复杂数字类型,具体细节可以查看文档。
布尔型:Python 实现了所有的布尔逻辑,但用的是英语,而不是我们习惯的操作符(比如 && 和 ||
等)
t = True
f = False
print type(t) # Prints "<type 'bool '>"
print t and f # Logical AND; prints "False"
print t or f # Logical OR; prints " True"
print not t # Logical NOT; prints "False "
print t != f # Logical XOR; prints "True"
字符串:Python 对字符串的支持非常棒。
hello = 'hello ' # String literals can use single quotes
world = "world " # or double quotes; it does not matter .
print hello # Prints " hello"
print len(hello ) # String length ; prints "5"
hw = hello + ' ' + world # String concatenation
print hw # prints "hello world"
hw12 = '%s %s %d' % (hello , world , 12) # sprintf style string formatting
print hw12 # prints " hello world 12"
字符串对象有一系列有用的方法,比如:
s = "hello"
print s.capitalize() # Capitalize a string; prints " Hello"
print s.upper() # Convert a string to uppercase ; prints "HELLO"
print s.rjust(7) # Right - justify a string , padding with spaces ; prints " hello "
print s.center(7) # Center a string , padding with spaces; prints " hello "
print s.replace ('l', '(ell)') # Replace all instances of one substring with another ;
# prints " he( ell)(ell)o"
print ' world '.strip () # Strip leading and trailing whitespace ; prints "world"
如果想详细查看字符串方法,请看文档。
容器 Containers
译者注:有知友建议 container 翻译为复合数据类型,供读者参考。
Python 有以下几种容器类型:列表(lists)、字典(dictionaries)、集合(sets)和元组(tuples)。
3

列表 Lists
列表就是 Python 中的数组,但是列表长度可变,且能包含不同类型元素。
xs = [3, 1, 2] # Create a list
print xs, xs[2] # Prints "[3, 1, 2] 2"
print xs[-1] # Negative indices count from the end of the list ; prints "2"
xs[2] = 'foo ' # Lists can contain elements of different types
print xs # Prints "[3, 1, 'foo ']"
xs.append('bar ') # Add a new element to the end of the list
print xs # Prints
x = xs.pop() # Remove and return the last element of the list
print x, xs # Prints "bar [3, 1, 'foo ']"
列表的细节,同样可以查阅文档。
切片 Slicing:为了一次性地获取列表中的元素,Python 提供了一种简洁的语法,这就是切片。
nums = range (5) # range is a built -in function that creates a list of integers
print nums # Prints "[0, 1, 2, 3, 4]"
print nums[2:4] # Get a slice from index 2 to 4 ( exclusive ); prints "[2, 3]"
print nums[2:] # Get a slice from index 2 to the end; prints "[2, 3, 4]"
print nums[:2] # Get a slice from the start to index 2 ( exclusive ); prints "[0, 1]"
print nums[:] # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]"
print nums[:-1] # Slice indices can be negative ; prints ["0 , 1, 2, 3]"
nums[2:4] = [8, 9] # Assign a new sublist to a slice
print nums # Prints "[0, 1, 8, 8, 4]"
在 Numpy 数组的内容中,我们会再次看到切片语法。
循环 Loops:我们可以这样遍历列表中的每一个元素:
animals = ['cat ', 'dog', 'monkey ']
for animal in animals:
print animal
# Prints " cat", "dog", "monkey", each on its own line .
如果想要在循环体内访问每个元素的指针,可以使用内置的 enumerate 函数
animals = ['cat ', 'dog', 'monkey ']
for idx , animal in enumerate(animals):
print '#%d: %s' % (idx + 1, animal)
# Prints "#1: cat", "#2: dog", "#3: monkey", each on its own line
列表推导 List comprehensions:在编程的时候,我们常常想要将一种数据类型转换为另一种。下面
是一个简单例子,将列表中的每个元素变成它的平方。
nums = [0, 1, 2, 3, 4]
squares = []
for x in nums:
squares.append(x ** 2)
print squares # Prints [0, 1, 4, 9, 16]
使用列表推导,你就可以让代码简化很多:
4

nums = [0, 1, 2, 3, 4]
squares = [x ** 2 for x in nums]
print squares # Prints [0, 1, 4, 9, 16]
列表推导还可以包含条件:
nums = [0, 1, 2, 3, 4]
even_squares = [x ** 2 for x in nums if x % 2 == 0]
print even_squares # Prints "[0, 4, 16]"
字典 Dictionaries
字典用来储存(键, 值)对,这和 Java 中的 Map 差不多。你可以这样使用它:
d = {'cat ': 'cute', 'dog': 'furry'} # Create a new dictionary with some data
print d['cat '] # Get an entry from a dictionary ; prints "cute"
print 'cat' in d # Check if a dictionary has a given key; prints "True "
d['fish'] = 'wet' # Set an entry in a dictionary
print d['fish'] # Prints "wet"
# print d[' monkey '] # KeyError: 'monkey ' not a key of d
print d.get ('monkey', 'N/A') # Get an element with a default ; prints "N/A"
print d.get ('fish ', 'N/A') # Get an element with a default; prints "wet"
del d['fish '] # Remove an element from a dictionary
print d.get ('fish ', 'N/A') # "fish " is no longer a key; prints "N/A"
想要知道字典的其他特性,请查阅文档。
循环 Loops:在字典中,用键来迭代更加容易。
d = {'person ': 2, 'cat ': 4, 'spider': 8}
for animal in d:
legs = d[animal]
print 'A %s has %d legs ' % (animal , legs)
# Prints "A person has 2 legs ", "A spider has 8 legs", "A cat has 4 legs"
如果你想要访问键和对应的值,那就使用 iteritems方法:
d = {'person ': 2, 'cat ': 4, 'spider': 8}
for animal , legs in d.iteritems ():
print 'A %s has %d legs ' % (animal , legs)
# Prints "A person has 2 legs ", "A spider has 8 legs", "A cat has 4 legs"
字典推导 Dictionary comprehensions:和列表推导类似,但是允许你方便地构建字典。
nums = [0, 1, 2, 3, 4]
even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}
print even_num_to_square # Prints "{0: 0, 2: 4, 4: 16}"
5
剩余136页未读,继续阅读

















安全验证
文档复制为VIP权益,开通VIP直接复制

评论2