2009年8月27日 星期四

Artificial Neural Network--Random Learning Perceptron

最近開始研讀類神經網路的相關文章
打算開發類神經網路訓練來解讀股價走勢
以下程式碼改自別人的範例
是我的蹲馬步練基本功的初步成果
最基本的神經網路--感知器



import random, math
import string

from sympy import *

def hardlims(inputM,weightM,bias):
     n=(weightM*inputM)[0].evalf()-bias
     if n<0:
     a = -1
     if n>=0:
     a = 1
     return a

def sum_array(a):
     acc = 0
     for i in a:
     acc += i
     return acc

class NN:
def __init__(self):
     self.w1 = [random.random() for x in range(10)]
     self.w2 = [random.random() for x in range(10)]
     self.w3 = [random.random() for x in range(10)]
     self.bias = 0

def learn(self,input,target):
     errorAbsSum = 1
     epsilon = 1e-6

     iterations = 0
     while abs(errorAbsSum) > epsilon:
     iterations +=1
     errorAbsSum = 0
     counter = 0
     tmp1 = (sum_array(self.w1) / len(self.w1))
     tmp2 = (sum_array(self.w2) / len(self.w2))
     tmp3 = (sum_array(self.w3) / len(self.w3))
     for i,x in enumerate(input):
     activation = hardlims(Matrix(3,1,x),Matrix(1,3,[tmp1,tmp2,tmp3]),self.bias)
     error = target[i] - activation
     learningRate = random.random() / 10
     tmp1 += (error * learningRate * x[0])
     tmp2 += (error * learningRate * x[1])
     tmp3 += (error * learningRate * x[2])
     errorAbsSum += abs(error)
     print 'Learning:w1=%f w2=%f w3=%f learningRate=%f error=%f activation=%f' % ( tmp1, tmp2,tmp3,learningRate,error,activation)
     del self.w1[0], self.w2[0],self.w3[0]
     self.w1.append(tmp1)
     self.w2.append(tmp2)
     self.w3.append(tmp3)
     print 'Iterations: %d \n w1=%f w2=%f w3=%f' % (iterations, tmp1, tmp2,tmp3)


def test(self,x):
     tmp1 = (sum_array(self.w1) / len(self.w1))
     tmp2 = (sum_array(self.w2) / len(self.w2))
     tmp3 = (sum_array(self.w3) / len(self.w3))
     activation = hardlims(Matrix(3,1,x),Matrix(1,3,[tmp1,tmp2,tmp3]),self.bias)
     print 'test(%s):result=%d w1=%f w2=%f w3=%f' % (x, activation, tmp1, tmp2,tmp3)


if __name__ == '__main__':
     input =[[-1,-1,-1],[1,1,1],[1,-1,-1]]
     target = [-1,1,1]

     nn=NN();
     nn.learn(input,target)
     nn.test([1,1,1])
     nn.test([1,1,-1])
     nn.test([1,-1,1])
     nn.test([1,-1,-1])
     nn.test([-1,1,1])
     nn.test([-1,1,-1])
     nn.test([-1,-1,1])
     nn.test([-1,-1,-1])


驗收訓練成果

test([1, 1, 1]):result=1 w1=0.663924 w2=0.276574 w3=0.333518
test([1, 1, -1]):result=1 w1=0.663924 w2=0.276574 w3=0.333518
test([1, -1, 1]):result=1 w1=0.663924 w2=0.276574 w3=0.333518
test([1, -1, -1]):result=1 w1=0.663924 w2=0.276574 w3=0.333518
test([-1, 1, 1]):result=-1 w1=0.663924 w2=0.276574 w3=0.333518
test([-1, 1, -1]):result=-1 w1=0.663924 w2=0.276574 w3=0.333518
test([-1, -1, 1]):result=-1 w1=0.663924 w2=0.276574 w3=0.333518
test([-1, -1, -1]):result=-1 w1=0.663924 w2=0.276574 w3=0.333518


縮排有點亂
有誰能指點一下?

2009年8月18日 星期二

FP10 Native 3D Diffuse Shading demo(STOK3D Filters)

我的Banner目前以FP10 Native 3D改寫
而濾鏡則用stok3d所提供的以Pixel Bender開發的各式濾鏡
改天試試看PV3D+Pixel Bender
不過自從Flash Player 10加上Native 3D特性後
用不用PV3D倒不是那麼重要了
不過還是很感謝PV3D相關開發人員的無私奉獻!!!

2009年8月5日 星期三

GAE--使用Memcache Python API

隨著股票看盤程式固定時間間隔要抓取的資料變多
資料回應的時間也越來越慢了,甚至每次程式啟動時都需要等上10幾秒
該是加入Memcache(快取)的時候了
很簡單,只要加入下列幾行code即可
from google.appengine.api import memcache

class getQuote(webapp.RequestHandler):
    def get(self):
       data = memcache.get(stkno_string)
       if data is None:
           data = self.query_for_data()
           #快取將資料保留20秒
           memcache.add(stkno_string, data, 20)
       self.response.out.write(data)

2009年8月2日 星期日

GAE--在 Python 使用 Cron 排程工作

在寫股價看盤軟體時需要上一個營業日的收盤價才能計算漲跌幅
所以必須在開盤前更新最新的收盤價才能算出正確的漲跌幅
這時Google App Engine提供的Cron Service就派上用場了,可以定期設定排程工作,
Cron會在每天的指定時間啟動網址,但還是與一般 HTTP request一樣有時間的限制。

一開始很高興的寫一大堆code撈DataStore的資料,
但是經過幾次request timeout後,只好乖乖的將一些繁雜的工作,
切成好幾分轉給其他的URL負責,讓cron啟動的URL只負責簡單的資料清空的動作
整個程式就可以正常運行了

下列是 cron.yaml 檔案的範例:

cron:
- description: daily update job
url: /tasks/updatedata
schedule: every 24 hours