目录

一.python代码基础

1.1.原始数据类型与操作

1.1.1.数值型

1.1.2.布尔型

1.1.3.字符串

1.1.4.其他

1.2.变量与集合

1.2.1.输入输出

1.2.2.列表(List)

1.2.3.元组(Tuple)

1.2.4.字典(Dictionaries)

1.2.5.集合(Set)

1.3.控制流

1.3.1.分支结构

1.3.2.循环结构

1.3.3.异常处理

1.3.4.迭代器

1.3.5.函数

1.3.6.函数的作用域

1.3.7.类

1.3.8.模块

1.3.9.高级

二.机器学习基础(了解与认识)

2.1机器学习的本质

2.2.机器学习的流程和步骤

2.2.1.收集输入与输出的例子

2.2.2.建立模型

2.2.3.确定输入输出与模型可接收返回的数值之间如何转换

2.2.4.使用输入与输出的例子调整模型的参数

2.2.5.使用没有参与训练的输入与输出评价模型是否成功摸索处规律(检验)

2.3.机器学习、深度学习和人工智能的区别

2.4.一个最简单的例子

2.5.机器学习

2.6.让参数调整量依赖于损失的

三.pytorch与矩阵计算入门

3.1.Pytorch简介

3.2.学Pytorch好还是Tensorflow好?

3.2.1.Dynamic Graph 与 Static Graph

3.3.安装Pytorch

3.4.Pytorch

3.5.Pytorch保存tensor使用的数据结构

3.6.矩阵乘法简介

3.7.使用Pytorch进行矩阵乘法计算

3.8.Pytorch的自动微分功能(autograd)

3.9.Pytorch的损失计算器封装(Loss function)

3.10.Pytorch的参数调整器封装(optimizer)

3.11.使用Pytorch实现二中的例子

3.12.使用矩阵乘法实现批次训练

3.13.划分训练集,验证集和测试机的例子

3.14.定义模型类(torch.nn.Module)

四.线性模型,激活函数和多层线性模型

4.1.生物神经元与人工神经元

4.2.单层线性模型

4.3.激活函数

4.4.多层线性模型

4.5.根据码农条件求工资


一.python代码基础

1.1.原始数据类型与操作

1.1.1.数值型

# 数值型
3 # => 3# 数学计算
1 + 1 # => 2
8 - 1 # => 7
10 * 2 # => 20
35 / 5 # => 7# 整型(int)的除法只会获得整型结果,余数自动舍弃 Python 2.7
5 / 2 # => 2
# Python会自动返回浮点数
5 / 2 # => 2.5# 浮点型(float)的计算可以有小数
2.0 # 这是一个浮点型
11.0 / 4.0 # => 2.75# 如果只需要整数部分,使用"//"做除法,又叫地板除(foored division)
5 // 3 # => 1
5.0 // 3.0 # => 1.0 浮点型效果也一样
-5 // 3 # => -2
-5.0 // 3.0 # => -2.0# 取余操作
7 % 3 # => 1# 幂操作
2 ** 4 # => 16# 使用括号改变优先级
(1+3) * 2 # => 8

1.1.2.布尔型

# 注: "and" 和 "or" 都是大小写敏感的
True and False # => False
False or True # => True# 注:int型也可以使用布尔操作
0 and 2 # => 0
-5 or 0 # => -5
0 == False # => True
2 == True # => False
1 == True # => True# 非操作
not True # => False
not False # => True# 判断相等
1 == 1 # => True
2 == 1 # => False# 判断不相等
1 != 1 # => False
2 != 1 # => True# 其它比较操作
1 < 10 # => True
1 > 10 # => False
2 <= 2 # => True
2 >= 2 # => True# 比较操作可以串联
1 < 2 < 3 # => True
1 < 3 < 2 # => False# is 与 == 比较
# is 是判断两个变量是否引用了同一个类
# == 是判断两个变量是否有同样的值
a = [1, 2, 3, 4]  # 将 a 指向一个新的数组 [1, 2, 3, 4]
b = a             # 将 b 指向 a 所指向的对象
b is a            # => True, a 和 b 引用了同一个对象
b == a            # => True, a 和 b 的对象值也相同
b = [1, 2, 3, 4]  # 将 b 指向一个新的数组 [1, 2, 3, 4]
b is a            # => False, a 和 b 不是引用了同一个对象
b == a            # => True, a 和 b 的对象值相同

1.1.3.字符串

# 字符串使用 单引号 或者 双引号 表示
"这是一个字符串"
'这也是一个字符串'# 字符串可以使用"+"连接
"Hello " + "world!" # "Hello world!"
# 不使用"+"也可以让字符串连接
"Hello " "world!" # "Hello world!"# 字符串乘法
"Hello" * 3 # => "HelloHelloHello"# 字符串可以当作字符数组操作
"This is a string"[0] # => "T"# 使用 % 对字符串进行格式化
# 从Python 3.1 开始已经不推荐使用了,但了解一下如何使用还是有必要的
x = 'apple'
y = 'lemon'
z = "The items in the basket are %s and %s " % (x,y)# 新的格式化字符串的方式是使用format方法
"{} is a {}".format("This", "placeholder")
"{0} can be {1}".format("strings", "formatted")
# 如果不想用下标方式,可以使用关键字的方式
"{name} wants to eat {food}".format(name="Bob", food="lasagna")

1.1.4.其他

# None 是一个对象
None # => None# 如果想要将一个对象与None进行比较,使用 is 而不要使用 == 符号
"etc" is None # => False
None is None # => True# is 操作用来判断对象的类型。在对象操作时非常有用# 任何一个对象都可以放在一个布尔上下文中进行判断
# 下面的情况会被当作False
#   - None
#   - 值为0的任何数值类型,(例如,0,0L,0.0,0j)
#   - 空列表,(例如,'',(),[])
#   - 空的容器,(例如,{},set())
#   - 符合条件的用户自定义对象实例,参看:https://docs.python.org/2/reference/datamodel.html#object.__nonzero__
#
# 其它情况下的值会被当作True,可以使用bool()函数来判断
bool(0)   # => False
bool("")  # => False
bool([])  # => False
bool({})  # => False

1.2.变量与集合

1.2.1.输入输出

# Python有一个打印语句
print "I'm Python. Nice to meet you!" # => I'm Python. Nice to meet you!# 获取控制台输入的简单方法
input_string_var = raw_input("Enter some data: ") # 返回字符串类型的数据
input_var = input("Enter some data: ") # 返回数值型的数据
# Warning: Caution is recommended for input() method usage
# 注意:在Python3中,input()已经不再使用,raw_input()重命名为input()# 不需要先声明变量再使用
some_var = 5    # 通常使用小写字母与下划线命名变量
some_var  # => 5# 访问一个未定义的变量会抛出一个异常
# 在“控制流”里会介绍更多异常处理
some_other_var  # 这里会抛出一个NameError# if 可以用来写成类似C语言的 '?:' 条件表达式
"yahoo!" if 3 > 2 else 2  # => "yahoo!"

1.2.2.列表(List)

# List用来存储列表
li = []
# 可以有初始数据
other_li = [4, 5, 6]# 添加元素到列表的尾
li.append(1) # [1]
li.append(2) # [1,2]
li.append(4) # [1,2,4]
li.append(3) # [1,2,4,3]
# 使用pop方法移除列表末尾的元素
li.pop()        # => 3 列表现在为 [1, 2, 4]
# 把3再放回去
li.append(3)    # li 现在为 [1, 2, 4, 3]# 像数组一样访问一个list
li[0]  # => 1
# 通过下标重新定义一个元素的值
li[0] = 42
li[0]  # => 42
li[0] = 1  # 注意:设置回原始值
# 查看最后一个元素
li[-1]  # => 3# 查找超过数据长度的值会抛出异常: IndexError
li[4]  # 抛出 IndexError# 可以使用切片句法访问列表
# 这里类型数学上的开/闭区间
li[1:3]  # => [2, 4]
li[2:]  # => [4, 3]
li[:3]  # => [1, 2, 4]
li[::2]   # =>[1, 4]
li[::-1]   # => [3, 4, 2, 1]
# 使用高级切片句法
# li[start:end:step]# 使用切片进行深层拷贝
li2 = li[:]  # => li2 = [1, 2, 4, 3] 但 (li2 is li) 返回 false# 使用“del”直接从列表中删除元素
del li[2] #liisnow[1,2,3]# 可以直接添加一个列表
li+other_li #=>[1,2,3,4,5,6]
# 注: 变量li 和 other_li 值并没有变化# 使用extend()方法,把列表串起来
li.extend(other_li)   # Now li is [1, 2, 3, 4, 5, 6]# 删除第一个值与参数相同的元素
li.remove(2)  # li is now [1, 3, 4, 5, 6]
li.remove(2)  # 抛出异常 ValueError 列表中没有2# 在指定下标插入元素
li.insert(1, 2)  # li is now [1, 2, 3, 4, 5, 6] again# 获得第一个匹配的元素的下标
li.index(2)  # => 1
li.index(7)  # 抛出异常 ValueError 列表中没有7# 使用“in”来检查列表中是否包含元素
1 in li #=>True
# 使用“len()”来检查列表的长度
len(li)   # => 6

1.2.3.元组(Tuple)

# 元组(Tuple)与列表类似但不可修改
tup = (1, 2, 3)
tup[0]   # => 1
tup[0] = 3  # 抛出异常 TypeError# 注意:如果一个元组里只有一个元素,则需要在元素之后加一个逗号;如果元组里没有元素,反而不用加逗号
type((1))   # => <class 'int'>
type((1,))  # => <class 'tuple'>
type(())    # => <class 'tuple'># 以下对列表的操作,也可以用在元组上
len(tup) # => 3
tup+(4,5,6) #=>(1,2,3,4,5,6)
tup[:2] #=>(1,2)
2intup #=>True# 可以把元组的值分解到多个变量上
a,b,c= (1, 2, 3) #a is now 1,b  is now 2 and c is now 3
d,e,f= 4,5,6 # 也可以不带括号
# 如果不带括号,元组会默认带上
g = 4, 5, 6 #=>(4,5,6)
# 非常容易实现交换两个变量的值
e, d = d, e # d is now 5 and e is now 4

1.2.4.字典(Dictionaries)

# 字典用来存储(键-值)映射关系
empty_dict = {}
# 这里有个带初始值的字典
filled_dict = {"one": 1, "two": 2, "three": 3}# 注意:字典 key 必须是不可以修改类型,以确保键值可以被哈希后进行快速检索
# 不可修改的类型包括:int, float, string, tuple
invalid_dict = {[1,2,3]: "123"}  # => Raises a TypeError: unhashable type: 'list'
valid_dict = {(1,2,3):[1,2,3]}   # Values can be of any type, however.# 使用[]查找一个元素
filled_dict["one"]   # => 1
# 使用"keys()"获得所有的“键”
filled_dict.keys()   # => ["three", "two", "one"]
# 注:字典的key是无序的,结果可以不匹配
# 使用"values()"获得所有的“值”
filled_dict.values()   # => [3, 2, 1]
# 注:同样,这是无序的
# 查询字条中是否存在某个”键“用 "in"
"one" in filled_dict   # => True
1 in filled_dict   # => False
# 查找一个不存在的key会抛出异常 KeyError
filled_dict["four"]   # KeyError
# 使用 "get()" 会避免抛出异常 KeyError
filled_dict.get("one") # => 1
filled_dict.get("four") # => None
# get方法支持默认参数,当key不存在时返回该默认参数
filled_dict.get("one", 4) #=>1
filled_dict.get("four", 4)   # => 4
# 注 filled_dict.get("four") 仍然返回 None
# (get不会把值插入字典中)
# 向字典中插入值的方式与list相同
filled_dict["four"] = 4  # now, filled_dict["four"] => 4
# "setdefault()" 只有首次插入时才会生效
filled_dict.setdefault("five", 5)  # filled_dict["five"] is set to 5
filled_dict.setdefault("five", 6)  # filled_dict["five"] is still 5# 使用 del 从字典中删除一个键
del filled_dict["one"]  # Removes the key "one" from filled dict# 从 Python 3.5 开始,可以使用**操作
{'a': 1, **{'b': 2}}  # => {'a': 1, 'b': 2}
{'a': 1, **{'a': 2}}  # => {'a': 2}

1.2.5.集合(Set)

# Set与list类似,但不存储重复的元素
empty_set = set()
some_set = set([1, 2, 2, 3, 4])   # some_set is now set([1, 2, 3, 4])# 与字典类型一样,Set 里的元素也必须是不可修改的
invalid_set = {[1], 1}  # => Raises a TypeError: unhashable type: 'list'
valid_set = {(1,), 1}# Set也是无序的,尽管有时看上去像有序的
another_set = set([4, 3, 2, 2, 1])  # another_set is now set([1, 2, 3, 4])
# 从Python 2.7开妈, {}可以用来声明一个Set
filled_set={1,2,2,3,4} #=>{1,2,3,4}
# Can set new variables to a set
filled_set = some_set
# 向Set中添加元素
filled_set.add(5)   # filled_set is now {1, 2, 3, 4, 5}
# 用 & 求 Set的交集
other_set = {3, 4, 5, 6}
filled_set & other_set # =>{3,4,5}
# 用 | 求 Set的合集
filled_set | other_set # =>{1,2,3,4,5,6}
# Do set difference with -
{1,2,3,4}-{2,3,5} # => {1,4}
# Do set symmetric difference with ^
{1,2,3,4}^{2,3,5} #=>{1,4,5}
# 检查右边是否是左边的子集
{1, 2} >= {1, 2, 3} # => False
# 检查左边是否是右边的子集
{1, 2} <= {1, 2, 3} # => True
# 检查元素是否在集合中
2 in filled_set   # => True
10 in filled_set   # => False

1.3.控制流

1.3.1.分支结构

# 先定义一个变量
some_var = 5
# 这里用到了if语句 缩进是Python里的重要属性
# 打印 "some_var is smaller than 10"
if some_var > 10:print "some_var is totally bigger than 10."
elif some_var < 10:    # This elif clause is optional.print "some_var is smaller than 10."
else:           # This is optional too.print "some_var is indeed 10."

1.3.2.循环结构

# For 循环用来遍历一个列表
for animal in ["dog", "cat", "mouse"]:# 使用{0}格式 插入字符串print "{0} is a mammal".format(animal)# "range(number)" 返回一个包含数字的列表
for i in range(4):
print i# "range(lower, upper)" 返回一个从lower数值到upper数值的列表
for i in range(4, 8):
print i# "range(lower, upper, step)" 返回一个从lower数值到upper步长为step数值的列表
# step 的默认值为1
for i in range(4, 8, 2):print(i)
# While 循环会一致执行下去,直到条件不满足
x=0
while x < 4:
print x
x+=1 #x=x+1的简写

1.3.3.异常处理

# 使用 try/except 代码块来处理异常
# 自 Python 2.6 以上版本支持:
try:# 使用 "raise" 来抛出一个异常raise IndexError("This is an index error")
except IndexError as e:pass    # Pass 表示无操作. 通常这里需要解决异常.
except (TypeError, NameError):pass    # 如果需要多个异常可以同时捕获
else:   # 可选项. 必须在所有的except之后print "All good!"   # 当try语句块中没有抛出异常才会执行
finally: #  所有情况都会执行print "We can clean up resources here"# with 语句用来替代 try/finally 简化代码
with open("myfile.txt") as f:for line in f:print line

1.3.4.迭代器

# Python offers a fundamental abstraction called the Iterable.
# An iterable is an object that can be treated as a sequence.
# The object returned the range function, is an iterable.filled_dict = {"one": 1, "two": 2, "three": 3}
our_iterable = filled_dict.keys()
print(our_iterable)  # => dict_keys(['one', 'two', 'three']). This is an object that implements our Iterable interface.# We can loop over it.
for i in our_iterable:print(i)  # Prints one, two, three# However we cannot address elements by index.
our_iterable[1]  # Raises a TypeError# An iterable is an object that knows how to create an iterator.
our_iterator = iter(our_iterable)# Our iterator is an object that can remember the state as we traverse through it.
# We get the next object with "next()".
next(our_iterator)  # => "one"# It maintains state as we iterate.
next(our_iterator)  # => "two"
next(our_iterator)  # => "three"# After the iterator has returned all of its data, it gives you a StopIterator Exception
next(our_iterator)  # Raises StopIteration# You can grab all the elements of an iterator by calling list() on it.
list(filled_dict.keys())  # => Returns ["one", "two", "three"]

1.3.5.函数

# 使用 "def" 来创建一个新的函数
def add(x, y):print "x is {0} and y is {1}".format(x, y)return x + y    # 用 return 语句返回值
# 调用函数
add(5,6) #=>prints out "x is 5 and y is 6" 返回值为11
# 使用关键字参数调用函数
add(y=6, x=5)   # 关键字参数可以不在乎参数的顺序
# 函数的参数个数可以不定,使用*号会将参数当作元组
def varargs(*args):return args
varargs(1, 2, 3)   # => (1, 2, 3)# 也可以使用**号将参数当作字典类型
def keyword_args(**kwargs):return kwargs# 调用一下试试看
keyword_args(big="foot", loch="ness") # => {"big": "foot", "loch": "ness"}# 两种类型的参数可以同时使用
def all_the_args(*args, **kwargs):print argsprint kwargs
"""
all_the_args(1, 2, a=3, b=4) prints:(1, 2){"a": 3, "b": 4}
"""# When calling functions, you can do the opposite of args/kwargs!
# Use * to expand positional args and use ** to expand keyword args.
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args)   # 相当于 foo(1, 2, 3, 4)
all_the_args(**kwargs)   # 相当于 foo(a=3, b=4)
all_the_args(*args, **kwargs)   # 相当于 foo(1, 2, 3, 4, a=3, b=4)
# you can pass args and kwargs along to other functions that take args/kwargs
# by expanding them with * and ** respectively
def pass_all_the_args(*args, **kwargs):all_the_args(*args, **kwargs)print varargs(*args)print keyword_args(**kwargs)# Returning multiple values (with tuple assignments)
def swap(x, y):return y, x  # Return multiple values as a tuple without the parenthesis.# (Note: parenthesis have been excluded but can be included)x = 1
y = 2
x, y = swap(x, y)     # => x = 2, y = 1
# (x, y) = swap(x,y)  # Again parenthesis have been excluded but can be included.

1.3.6.函数的作用域

x=5
def set_x(num):# 局部变量x与全局变量x不相同x = num # => 43print x # => 43
def set_global_x(num):global xprint x # => 5x = num # 全局变量被设置成为6print x # => 6
set_x(43)
set_global_x(6)
# 函数也可以是对象
def create_adder(x):def adder(y):return x + y
return adder
add_10 = create_adder(10)
add_10(3)   # => 13
# 匿名函数
(lambda x: x > 2)(3)   # => True
(lambda x, y: x ** 2 + y ** 2)(2, 1) # => 5
# 高阶函数
map(add_10, [1, 2, 3])   # => [11, 12, 13]
map(max, [1, 2, 3], [4, 2, 1])   # => [4, 2, 3]
filter(lambda x: x > 5, [3, 4, 5, 6, 7])   # => [6, 7]
# We can use list comprehensions for nice maps and filters
[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13]
[x for x in[3,4,5,6,7] if x>5] #=>[6,7]

1.3.7.类

# 继承 object 创建一个子类
class Human(object):# 一个类属性,所有该类的实例都可以访问species = "H. sapiens"# 基础实例化方法,在创建一个实例时调用# 注意在名称前后加双下划线表示对象或者属性是 Python 的特殊用法,但用户可以自己控制# 最好不要在自己的方法前这样使用def __init__(self, name):# 将参数赋值给实例属性self.name = name# 初始化属性self.age = 0# 一个实例方法。所有实例方法的第一个属性都是selfdef say(self, msg):return "{0}: {1}".format(self.name, msg)# A class method is shared among all instances# They are called with the calling class as the first argument@classmethoddef get_species(cls):return cls.species # A static method is called without a class or instance reference@staticmethoddef grunt():return "*grunt*"# A property is just like a getter.# It turns the method age() into an read-only attribute# of the same name.@propertydef age(self):return self._age# This allows the property to be set@age.setterdef age(self, age):self._age = age# This allows the property to be deleted@age.deleterdef age(self):del self._age# 创建一个实例
i = Human(name="Ian")
print i.say("hi") # prints out "Ian: hi"j = Human("Joel")
print j.say("hello")  # prints out "Joel: hello"# 调用类方法
i.get_species()   # => "H. sapiens"# 访问共有变量
Human.species = "H. neanderthalensis"
i.get_species()   # => "H. neanderthalensis"
j.get_species()   # => "H. neanderthalensis"# 调用静态方法
Human.grunt()   # => "*grunt*"# Update the property
i.age = 42# Get the property
i.age # => 42# Delete the property
del i.age
i.age  # => raises an AttributeError

1.3.8.模块

# 可以直接引用其它模块
import math
print math.sqrt(16)  # => 4
# 也可以引用模块中的函数
from math import ceil, floor
print ceil(3.7)  # => 4.0
print floor(3.7)   # => 3.0# 你可以引用一个模块中的所有函数
# 警告:这是不推荐的
from math import *
# 可以给模块起个简短的别名
import math as m
math.sqrt(16) == m.sqrt(16)   # => True
# you can also test that the functions are equivalent
from math import sqrt
math.sqrt == m.sqrt == sqrt  # => True
# Python modules are just ordinary python files. You
# can write your own, and import them. The name of the
# module is the same as the name of the file.
# You can find out which functions and attributes
# defines a module.
import math
dir(math)

1.3.9.高级

# Generators help you make lazy code
def double_numbers(iterable):for i in iterable:yield i + i
# A generator creates values on the fly.
# Instead of generating and returning all values at once it creates one in each
# iteration.  This means values bigger than 15 wont be processed in
# double_numbers.
# Note xrange is a generator that does the same thing range does.
# Creating a list 1-900000000 would take lot of time and space to be made.
# xrange creates an xrange generator object instead of creating the entire list
# like range does.
# We use a trailing underscore in variable names when we want to use a name that
# would normally collide with a python keyword
xrange_ = xrange(1, 900000000)
# will double all numbers until a result >=30 found
for i in double_numbers(xrange_):print iif i >= 30:
break
# Decorators
# in this example beg wraps say
# Beg will call say. If say_please is True then it will change the returned
# message
from functools import wrapsdef beg(target_function):@wraps(target_function)def wrapper(*args, **kwargs):msg, say_please = target_function(*args, **kwargs)if say_please:return "{} {}".format(msg, "Please! I am poor :(")return msgreturn wrapper
@beg
def say(say_please=False):msg = "Can you buy me a beer?"return msg, say_please
print say()  # Can you buy me a beer?
print say(say_please=True)  # Can you buy me a beer? Please! I am poor :(

二.机器学习基础(了解与认识)

2.1机器学习的本质

在讲解具体的例子与模型之前,我们先来了解一下什么是机器学习。在业务中我们有很多需要解决的问题,例如用户提交订单时如何根据商品列表计算订单金额,用户搜索商品时如何根据商品关键字得出商品搜索结果,用户查看商品一览时如何根据用户已买商品计算商品推荐列表,这些问题都可以分为输入,操作,输出,如下图所示。

其中操作部分我们通常会直接编写程序代码实现,程序代码会查询数据库,使用某种算法处理数据等,这些工作可能很枯燥,一些程序员受不了了就会自称码农,因为日复一日编写这些逻辑就像种田一样艰苦和缺乏新意。你有没有想过如果有一套系统,可以只给出一些输入和输出的例子就能自动实现操作中的逻辑?如果有这么一套系统,在处理很多问题的时候就可以不需要考虑使用什么逻辑从输入转换到输出,我们只需提供一些例子这套系统就可以自动帮我们实现。

好消息是这样的系统是存在的,我们给出一些输入与输出的例子,让机器自动摸索出它们之间的规律并且建立一套转换它们的逻辑,就是所谓的机器学习。目前机器学习可以做到从图片识别出物体类别,从图片识别出文字,从文本识别出大概含义,也可以做到上图中的从已买商品列表计算出推荐商品列表,这些操作都不需要编写具体逻辑,只需要准备一定的例子让机器自己学习即可,如果成功摸索出规律,机器在遇到例子中没有的输入时也可以正确的计算出输出结果,如下图所示:

可惜的是机器学习不是万能的,我们不能指望机器可以学习到所有规律从而实现所有操作,机器学习的界限主要有:

  • 做不到 100% 的精度,例如前述的根据商品列表计算订单价格要求非常准确,我们不能用机器学习来实现这个操作
  • 需要一定的数据量,如果例子较少则无法成功学习到规律
  • 无法实现复杂的判断,机器学习与人脑之间仍然有相当大的差距,一些复杂的操作无法使用机器学习代替

到这里我们应该对机器学习是什么有了一个大概的印象,如何根据输入与输出摸索出规律就是机器学习最主要的命题,接下来我们会更详细分析机器学习的流程与步骤。需要注意的是,不是所有场景都可以明确的给出输入与输出的例子,可以明确给出例子的学习称为有监督学习 (supervised learning),而只给出输入不给出输出例子的学习称为无监督学习 (unsupervised learning),无监督学习通常用于实现数据分类,虽然不给出输出但是会按一定的规律控制学习的过程,因为无监督学习应用范围不广,这个系列讲的基本上都是有监督学习。

2.2.机器学习的流程和步骤

先来了解一下机器学习的流程:

比如我这次要做的项目,识别是否佩戴口罩,输入一张图片转为灰度图读入计算机,设置相应的模型与参数,根据正确的数据集(即有监督学习)进行训练,计算损失根据损失调整参数,最后得到精度较高的训练模型以及参数。

而实现机器学习需要以下的步骤:

  • 收集输入与输出的例子
  • 建立模型
  • 确定输入输出与模型可接收返回的数值之间如何转换
  • 使用输入与输出的例子调整模型的参数
  • 使用没有参与训练的输入与输出评价模型是否成功摸索出规律

2.2.1.收集输入与输出的例子

在开始机器学习之前我们需要先收集输入与输出的例子,收集到的例子又称数据集 (Dataset),收集工作一般是个苦力活,例如学习从图片判断物体类别需要收集一堆图片并手动对它们进行分类,学习从图片识别文字需要收集一堆图片并手动记录图片对应的文本,这样的工作通常称为打标签 (Labeling),标签 (Label) 就相当于这个数据对应的输出结果。有些时候我们也可以偷懒,例如实现验证码识别的时候我们可以反过来根据文本生成图片,然后把图片当作输入文本当作输出,再例如实现商品推荐的时候我们可以把用户购买过的商品分割成两部分,一部分作为已购买商品 (输入),另一部分作为推荐商品 (输出)。注意输入与输出可以有多个,例如视频网站可以根据用户的年龄,性别,所在地 (3 个输入) 来判断用户喜欢看的视频类型 (1 个输出),再例如自动驾驶系统可以根据视频输入,雷达输入与地图路线 (3 个输入) 计算汽车速度与方向盘角度 (2 个输出),后面会介绍如何处理多个输入与输出,包括数量可变的输入。

如果你只是想试试手而不是解决实际的业务问题,可以直接用别人收集好的数据集,以下是包含了各种公开数据集链接的 Github 仓库:

https://github.com/awesomedata/awesome-public-datasets

2.2.2.建立模型

用于让机器学习与实现操作的就是模型 (Model),模型可以分为两部分,第一部分是计算方法,这部分需要我们来决定并且不会在学习过程中改变;第二部分是参数,这部分会随着学习不断调整,最终实现我们想要的操作。模型的计算方法需要根据业务(输入与输出的类型)来决定,例如分类可以使用多层线性模型,图像识别可以使用 CNN 模型,趋势预测可以使用 RNN 模型,文本翻译可以使用 Transformer 模型,对象识别可以使用 R-CNN 模型等 (这些模型会在后续的章节详细介绍),通常我们可以直接用别人设计好的模型再加上一些细微调整(只会做这种工作的也叫调参狗

机器学习基础以及在pynq-Z2上部署Faster-RCNN的项目学习1相关推荐

  1. .NET开发框架(五)-IIS上部署ASP.NET Core项目教程

    在之前教程中,我们分享了框架的功能与视频演示介绍(文尾底部提供往期教程快捷链接) 系列教程:从初学者到架构师的一步步蜕变 本篇经验将和大家介绍如何在IIS上部署ASP.NET Core项目,希望对初学 ...

  2. 在Linux上部署第一个web项目

    如何在Linux上部署第一个web项目(未更新完) 一.向服务器中的数据库添加数据(本篇以Mysql为例) 1.通过SQLyog将项目的数据库中的所有数据表导出为sql文件 2.为服务器上的数据库授权 ...

  3. python画图库哪个好_机器学习基础5--python画图库matplotlib(上)

    图像是我们最直观的数据表达方式,python的matplotlib库可以用来画图.下面来简单总结下matplotlib的使用方法. 上篇讲matplot画图中用到的基础对象,包括图像Figure,平面 ...

  4. 机器学习基础:模型评估(上)

    目录 1. 什么是一个优秀的分类器 2. 选择测试数据集 2.1 切分训练测试集 2.1.1 简单随机划分(random handout) 2.1.2 留一法(leave-one-out) 2.1.3 ...

  5. 阿里云服务器ECS上部署简单的SSM项目

    一.引言(为什么选择Linux部署) 1.1 开发环境(dev) 外部用户无法访问,开发人员使用,版本变动很大 平时大家大多是在Windows或者Mac操作系统下去编写代码进行开发,在开发环境中安装大 ...

  6. [Python人工智能] 三.theano实现分类神经网络及机器学习基础

    从本篇文章开始,作者正式开始研究Python深度学习.神经网络及人工智能相关知识.前两篇文章讲解了神经网络基础概念.Theano库的安装过程及基础用法.theano实现回归神经网络,这篇文章主要讲解机 ...

  7. 机器学习——基础知识

    机器学习--基础知识 机器学习 概述 机器学习(Machine Learning,ML) 是使用计算机来彰显数据背后的真实含义,它为了把无序的数据转换成有用的信息.是一门多领域交叉学科,涉及概率论.统 ...

  8. 第1章 机器学习基础

    机器学习 概述 机器学习(Machine Learning,ML) 是使用计算机来彰显数据背后的真实含义,它为了把无序的数据转换成有用的信息.是一门多领域交叉学科,涉及概率论.统计学.逼近论.凸分析. ...

  9. 【机器学习系列】之机器学习基础

    作者:張張張張 github地址:https://github.com/zhanghekai [转载请注明出处,谢谢!] 文章目录 一.机器学习概述 二.研究内容 三.研究意义及现状 四.机器学习组成 ...

  10. 机器学习基础:模型评估(下)

    目录 1. 引言 2. 进一步评估 3. 机器学习中的偏差和方差种类 3.1 过拟合 3.2 欠拟合 3.3 训练曲线观察泛化性 3.4 模型偏差和方差(bias and variance) 3.4. ...

最新文章

  1. Django从理论到实战(part17)--模板概述
  2. 【SIGGRAPH 2015】【巫师3 狂猎 The Witcher 3: Wild Hunt 】顶级的开放世界游戏的实现技术。...
  3. 将源字符串的前count个字符拷贝到目的字符串中strncpy()
  4. linux的基础知识——全局变量异步I/O
  5. HP DVD-ROM TS-L663M ATA Devices
  6. 轻松解决远程链接的“Gtk-WARNING **: cannot open display;”或“Cannot connect to display;”问题
  7. 下行物理信道rs_这些CH结尾的LTE物理信道傻傻分不清楚?快来看这篇文章!
  8. 21天jmeter打卡day2-环境搭建
  9. Stacking:Catboost、Xgboost、LightGBM、Adaboost、RF etc
  10. 读取csv文件中的IMU数据并以sensor_msgs/Imu格式发送
  11. 基于JXTA开发的组内聊天和共享文件P2P软件
  12. 数论之费马大定理及怀尔斯的证明
  13. 台式计算机的打印机端口,打印机端口设置,高手教你如何搞定电脑打印机端口设置...
  14. python爬虫-批量下载qq音乐
  15. 华硕固件无线打印机服务器设置,华硕路由器远程打印机LPD设置-Windows.pdf
  16. Redis+Zookeeper+NIO+JVM+Dubbo+mq+Kafka+ElasticSearch+POI相关面试题
  17. 使用神器vscode代替beyond compare进行文本比较高亮显示
  18. android手机屏幕总是闪烁,手机屏幕闪烁是什么原因
  19. 一个小兔子的大数据见解2
  20. coso全称是什么_京东方全称是什么

热门文章

  1. 史上最全的冲压模具资料
  2. Cesium设置模型朝向速度矢量方向
  3. 2020一城一味厨王争霸赛圆满成功
  4. adobe flash player安装失败
  5. 理解残差神经网络(Resnet)
  6. 计算机学科a类排名,中国科学技术大学学科评估排名!附中科大a类学科名单
  7. 美的空气能计算机故障维修,请问一下美的空气能热水器出现E0故障如何维修
  8. 携win32api给大家拜个早年
  9. hao123网址之家--智能计算器 html源代码
  10. [CODE【VS】]江哥的DP题a