栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

2021-10-06

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-10-06

theano python 神经网络

什么是神经网络
为计算机量身定制的神经系统,计算机神经网络是一种模仿生物神经网络或动物的神经中枢,或者是大脑的结构和功能,是一种数学模型或计算机模型,神经网络有、由大量的人工神经元连接进行计算,但大多数情况下,人工神经网络能够在外界信息的基础上改变内部结构,是一种自己逐渐适应的过程,现代神经网络是一种基于传统统计学建模的工具,常用来对输入和输出间复杂的关系进行建模,或者来探索数据间的模式。
梯度下降机制
神经网络黑盒不黑
神经网络在做什么?

Theano最基础的用法

import numpy
import theano .tensor as T
from theano import function
#basic
x=T.dscalar(‘x’) #定义x的存量
y=T.dscalar(‘x’)
z=x+y

f=function([x,y],z) #theano中的定义方式,列表中放入输入量,后面放output
print(f(2,3)) #给定输入量

#to preety-print the function
from theano import pp
print(pp(z)) #输出为x+y,在较复杂的情况中看出z是有什么组成的

#how about matrix #如何定义一个矩阵
x=T.dmatrix(‘x’) #前面d表示64位。f表示32位
y=T.dmatrix(‘y’)
z=x+y #z=T.dot(xy)矩阵的乘法
f=function([x,y],z)
print(f((np,arange(12).reshape((3,4)), #x为三行四列的矩阵0,1,2,3
10*np.ones((3,4))))) #y为都为10的三行四列的矩阵

theano中function用法

import numpy as np
import theano.tensor as T
import theano

## activation function example

x=T.dmatrix(‘x’)
s=1/(1+T.exp(-x)) #logistic or soft step计算概率
logistic=theano.function([x],s)
print(logistic([[0,1],[-2,-3]]))

## multiply outputs for a function
多个同类型变量定义
a,b=T.dmatrices(‘a’,‘b’)
diff=a-b
abs_diff=abs(diff)
diff_squared=diff**2
f=theano.function([a,b],[diff,abs_diff,diff_squared])
print(f(np.ones((2,2)),np.arange(4).reshape((2,2)))) #两行两列的矩阵0,1,2,3

## name for a function
x,y,w=T.dscalar(‘x’,‘y’,‘w’) #先定义三个存量的值
z=(x+y)*w
f=theano.function([x,
theano.In(y,value=1),
theano.In(w,value=2,name=‘weights’)],
z) #相当于def(x,y=1,w=2) z为output

print(f(23,))
print(f(23,2,weights=4))

shared变量

import numpy as np
import theano.tensor as T
import theano
state=theano.shared(np.array(0,dtype=np.float64),‘state’) #定义shared value 初始值是0
inc=T.scalar(‘inc’,dtype=state.dtype)
accumulator=theano.function([inc],state,updates=[(state,state+inc)]) #等于state=state+inc
print(accumulator(10))
print(accumulator(10)) #第二次输出才能输出更新以后的值

#to get variable value
print(state.get_value())
accumulator(1)
print(state.get_value())
accumulator(10)
print(state.get_value())
#to set variable value
state.set_value(-1) #重新定义为-1
accumulator(3)
print(state.get_value())
#temporarily replace shared variable with another value in another function
只想暂时用shared value 不用更新

tmp_func=state*2+inc
a=T.scalar(dtype=state.dtype)
skip_shared=theano.function([inc,a],tmp_func,givens=[(state,a)]) #把state用a代替
print(skip_shared(2,3))
print(state.get_value())

什么是激励函数

解决现实生活中不能用线性方程解决的问题 linear
y=AF(Wx)
AF就是激励函数,强行把线性函数掰弯


activation function激励函数

Layer类,定义神经层

神经网络由各种不一样的神经层组成,输入层、隐藏层、输出层,神经层之间互相连接,而且全连接。

import numpy as np
import theano.tensor as T
import theano

class Layer(object): def__init__(self,inputs,in_size,out_size,activation_function=None):
self.W=theano.shared(np.random.normal(0,1,(int_size,out_size)))
self.b=theano.shared(np.zeros((out_size,))+0.1)
self.Wx_plus_b=T.dot(inputs,self.W)+self.b
self.activation_function=activation_function
if activation_function is None:
self.outputs=self。Wx_plus_b
else:
self.outputs=self.activation_function(self.Wx_plus_b)

“”"
to define the layer like this:
L1=Layer(inputs,in_size=1,out_size=10,activation_function=T.nnet.relu)
L2=Layer(L1.outputs,10,2,None) #L1的outputs就是L2的inputs

regression回归学习

import numpy as np
import theano.tensor as T
import theano

class Layer(object): def__init__(self,inputs,in_size,out_size,activation_function=None):
self.W=theano.shared(np.random.normal(0,1,(int_size,out_size)))
self.b=theano.shared(np.zeros((out_size,))+0.1)
self.Wx_plus_b=T.dot(inputs,self.W)+self.b
self.activation_function=activation_function
if activation_function is None:
self.outputs=self。Wx_plus_b
else:
self.outputs=self.activation_function(self.Wx_plus_b)

regression可视化学习
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/309031.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号