首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >theano上的简单XOR示例

theano上的简单XOR示例
EN

Stack Overflow用户
提问于 2016-03-07 13:48:18
回答 1查看 683关注 0票数 0

我是西亚诺的新手,我没能用提亚诺做简单的异或例子。我试了很多东西让它发挥作用,但似乎我只是在做萨满教。看看代码,它很简单,但是我得到了随机的结果。

代码语言:javascript
复制
import numpy as np
import collections

import theano
import theano.tensor as T

INPUT_SIZE = 2
HIDDEN_SIZE = 2
OUTPUT_SIZE = 1

def train_2(data, valid_set_x):
    lr = 0.2

    x, y = data

    # symbol declarations
    ep = T.scalar()
    sx = T.matrix()
    sy = T.matrix()

    w1 = theano.shared(np.random.normal(loc=0, scale=1, size=(INPUT_SIZE, HIDDEN_SIZE)))
    b1 = theano.shared(np.random.normal(loc=0, scale=1, size=(HIDDEN_SIZE)))
    w2 = theano.shared(np.random.normal(loc=0, scale=1, size=(HIDDEN_SIZE, OUTPUT_SIZE)))
    b2 = theano.shared(np.random.normal(loc=0, scale=1, size=(OUTPUT_SIZE)))

    # symbolic expression-building
    hid = T.tanh(T.dot(sx, w1) + b1)
    out = T.tanh(T.dot(hid, w2) + b2)

    err = 0.5 * T.sum(out - sy) ** 2

    gw = T.grad(err, w1)
    gb = T.grad(err, b1)
    gv = T.grad(err, w2)
    gc = T.grad(err, b2)

    list = ((w1, w1 - (lr / ep) * gw),
            (b1, b1 - (lr / ep) * gb),
            (w2, w2 - (lr / ep) * gv),
            (b2, b2 - (lr / ep) * gc))

    dict = collections.OrderedDict(list)

    # compile a fast training function
    train = theano.function([sx, sy, ep], err, updates=dict)
    sample = theano.function([sx], out)

    train_set_size = x.shape[0]

    # now do the computations
    batchsize = 1
    for epoch in xrange(10):
        err = 0
        for i in xrange(train_set_size):
            x_i = x[i * batchsize: (i + 1) * batchsize]
            y_i = y[i * batchsize: (i + 1) * batchsize]
            err += train(x_i, y_i, epoch + 1)
        print "Error: " + str(err)

    print "Weights:"
    print w1.get_value()
    print b1.get_value()
    print w2.get_value()
    print b2.get_value()

    return sample(valid_set_x)

def test__(files=None):
    x_set = np.array([[-5, -5],
                      [-5, 5],
                      [5, -5],
                      [5, 5]]).astype("float32")
    y_set = np.array([[-0.9], [-0.9], [-0.9], [0.9]]).astype("float32")

    print "Processing..."
    result_set_x = train_2((x_set, y_set), x_set)

    print x_set
    print result_set_x
    print y_set

if __name__ == '__main__':
    test__()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-02 09:09:19

问题在您的更新部分。我将变量重命名为“更新”为“list”和“dict”是保留的单词,这不是一个好的选择。另外,我不知道为什么你要降低你的学习速度这么快,我删除了。更新应该是如下所示的数组

代码语言:javascript
复制
updates = [(w1, w1 - lr * gw),
           (b1, b1 - lr * gb),
           (w2, w2 - lr * gv),
           (b2, b2 - lr * gc)]

# compile a fast training function
train = theano.function([sx, sy], err, updates=updates)

我运行了修改后的示例,得到了下面的结果。它需要更多的迭代来减少损失,但除此之外,它是可以的。

代码语言:javascript
复制
Processing...
Error: 1.4456279556
...
Error: 0.0767515052046
Weights:
[[ 0.52955082 -1.26936557]
 [-1.05887804  0.04998216]]
[ 0.29209577 -0.22703456]
[[-0.89983822]
 [-0.88619565]]
[-0.86047891]
[[-5. -5.]
 [-5.  5.]
 [ 5. -5.]
 [ 5.  5.]]
[[-0.98989634]
 [-0.68941057]
 [-0.7034631 ]
 [ 0.72087948]]
[[-0.89999998]
 [-0.89999998]
 [-0.89999998]
 [ 0.89999998]]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35845448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档