Theano入门神经网络(三)

Theano是Bengio大神课题组弄得。

一、Theano初步

Theano编程三个步骤: 初始化、编译和执行,就是定义符号(变量对象)、编译代码和执行代码

举个例子:计算 z=x1*w1+wo 代码如下:

# -*- coding: utf-8 -*-
__author__ = 'Administrator'

import theano
import  theano.tensor as T
import  random
import  numpy as np
from itertools import izip
import matplotlib.pyplot as plt

#初始化
x1 = T.scalar()
w1 = T.scalar()
w0 = T.scalar()
z = w1*x1+w0

#编译

net_input = theano.function(
    inputs=[w1, x1, w0],
    outputs=z
)

#执行

print 'net input %.2f'% (net_input(2.0, 1.0 ,0.5))

在写Theano代码时,要注意变量的类型 dtype ,要分清楚我们要使用 64或者32为的int or floats

二、 配置Theano

一般来说 采用CPU时,我们设置float64 ,采用GPU时,需要设置float32

print (theano.config.floatX)
print (theano.config.device)

在window中设置CPU or GPU模式,是在运行cmd后出现的路径下,新建一个.theanorc.txt的文件。文件内容如下图所示:

三、使用array结构

   一个简单的求和例子

 

#初始化
x = T.fmatrix(name ='x')
x_sum = T.sum(x, axis=1) # 0  是按列 1 是按行

#编译
calc_sum = theano.function(
    inputs=[x],
    outputs=x_sum
)

#执行
ary = np.array([[1,2,4],[1,2,3]],dtype=theano.config.floatX)
print calc_sum(ary)

 shared variable 和 权值更新

#初始化
x =  T.fmatrix('x')
w = theano.shared(np.asarray([[0.0,0.0,0.0]],dtype=theano.config.floatX))

z = T.dot(x,w.T) # 这个就是两个矩阵相乘 w是1*3  x是1*3

update = [[w, w+1.0]]

# 编译
net_input = theano.function(
    inputs=[x],
    updates=update,
    outputs=z
)

# 执行

data = np.array([[1,2,3]],dtype=theano.config.floatX)
for i in range(5):
    print w.get_value()
    print '%d   %.2f' % (i,net_input(data))

可以提取指定数据,代码如下

#初始化
data = np.array([[1,2,3]],dtype=theano.config.floatX)
x =  T.fmatrix('x')
w = theano.shared(np.asarray([[0.0,0.0,0.0]],dtype=theano.config.floatX))

z = T.dot(x,w.T) # 这个就是两个矩阵相乘 w是1*3  x是1*3

update = [[w, w+1.0]]

# 编译
net_input = theano.function(
    inputs=[],
    updates=update,
    givens={x : data},
    outputs=z
)

# 执行

for i in range(5):
    print w.get_value()
    print '%d   %.2f' % (i,net_input())

 四、 一个线性回归的例子

X_train = np.asarray(
    [ [0,0] , [1, 0],
      [2,0] , [3, 0],
      [4,0] , [5, 0],
      [6,0] , [7, 0],
      [8,0] , [9, 0]],
    dtype= theano.config.floatX
    )

y_train = np.asarray(
    [ 1.0,1.3,3.1,2.0,
      5.0,6.3,6.6,7.4,
      8.0,9.0],
    dtype= theano.config.floatX
    )

y_train_new = np.asarray(
    [ [1.0],[1.3],[3.1],[2.0],
      [5.0],[6.3],[6.6],[7.4],
      [8.0],[9.0]],
    dtype= theano.config.floatX
    )
print X_train.shape[1] # shape 0 获取行 shape 1 获取列

def train_linreg(X_train, y_train ,eta, epochs):
    costs = []
    #初始化array0
    eta0 = T.fscalar('eta0')
    y = T.fvector(name='y')
    X = T.fmatrix(name='X')
    w = theano.shared(np.zeros(
        shape=(X_train.shape[1] +1 ),dtype= theano.config.floatX), name = 'w')

    #计算损失函数
    net_input = T.dot(X, w[1:]) + w[0] # w[1:] 从第二个到最后一个数据
    errors = y - net_input
    cost = T.sum(T.pow(errors,2))

    #梯度更新
    gradient = T.grad(cost,wrt=w)
    update = [(w, w- eta0* gradient)]

    #定义模型
    train = theano .function(inputs=[eta0],
                             outputs=cost,
                             updates=update,
                             givens={X:X_train,
                                     y: y_train})


    for i in range(epochs):
        costs.append(train(eta))
        print w.get_value()
    return  costs, w

def predict_linreg(X,w):
    Xt = T.matrix(name='X')
    net_input = T.dot(Xt, w[1:])+w[0]
    predict = theano.function(inputs=[Xt],
                              givens={w:w},
                              outputs= net_input)
    return  predict(X)


costs,w= train_linreg(X_train,y_train,eta=0.001,epochs=10)
plt.plot(range(1,len(costs)+1),costs)
plt.tight_layout()
plt.xlabel("epoch")
plt.ylabel('cost')
plt.show()
    #
#plt.scatter(X_train, y_train ,marker='s',s=50)
plt.plot(range(X_train.shape[0]),predict_linreg(X_train,w),color='gray',marker='o',markersize=4,linewidth=3)
plt.xlabel("x")
plt.ylabel('y')
plt.show()

结果图: 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值