h = T.dot(h.dimshuffle('x', 0), W)
    h1 = h[:, 0:10] # 1x10
    h2 = h[:, 10:20]
    h3 = h[:, 20:30]
    h4 = h[:, 30:40]

    test = (T.dot(x1, T.transpose(h1)) +
            T.dot(x2, T.transpose(h2)) +
            T.dot(x3, T.transpose(h3)) +
            T.dot(x4, T.transpose(h4)))
    return test + mem

mem, updates = T.scan(step1,
                sequences=[inputs, hidden],
                outputs_info=[T.zeros(shape=(seq_len, 1))]
            )
f1 = T.function(inputs=[], outputs=mem)
print(f1().shape)

# ====== second approach ====== #


def step2(x1, x2, x3, x4, h, mem):
    h = h.dimshuffle('x', 0) # 1x20
    h1 = T.dot(h, W1) # 1x10
    h2 = T.dot(h, W2)
    h3 = T.dot(h, W3)
    h4 = T.dot(h, W4)
    test = (T.dot(x1, T.transpose(h1)) +
            T.dot(x2, T.transpose(h2)) +
示例#2
0
import time
import theano
def step(s1, s2, s3, o1, o2, n1, n2):
    return o1, o2

seq1 = T.variable(np.arange(10))
seq2 = T.variable(np.arange(20))
seq3 = T.variable(np.arange(5))

nonseq1 = T.variable(1.)
nonseq2 = T.variable(2.)

([o1, o2], updates) = theano.scan(
    fn=step,
    sequences=[seq1, seq2, seq3],
    outputs_info=[T.zeros((2, 2)), T.ones((2, 2))],
    non_sequences=[nonseq1, nonseq2],
    n_steps=None,
    truncate_gradient=-1,
    go_backwards=False)

f1 = T.function(
    inputs=[],
    outputs=[o1, o2],
    updates=updates)
a, b = f1()
print(a.shape)

o1, o2 = T.loop(step,
    sequences=[seq1, seq2, seq3],
    outputs_info=[T.zeros((2, 2)), T.ones((2, 2))],