Example #1
0
def main3():
    rng = numpy.random.RandomState(23455)

    docList = ttList.TypedListType(
        ttList.TypedListType(TensorType(theano.config.floatX,
                                        (False, False))))("docList")
    docLabel = T.ivector('docLabel')
    layer0 = DocEmbeddingNN(docList, rng, 4)

    layer1 = HiddenLayer(rng,
                         input=layer0.output,
                         n_in=layer0.outputDimension,
                         n_out=10,
                         activation=T.tanh)

    layer2 = LogisticRegression(input=layer1.output, n_in=10, n_out=10)
    cost = layer2.negative_log_likelihood(docLabel)
    params = layer2.params + layer1.params + layer0.params
    grads = T.grad(cost, params)

    f = theano.function([docList], layer2.y_pred)

    a = [[[[2, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]],
          [[1, 2, 4, 4], [1, 2, 3, 4]]],
         [[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]],
          [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]]]
    print f(a)
    print "All finished!"
Example #2
0
def main2():
    a = [[[[1, 2, 3]], [[4, 5]]]]
    tl = ttList.TypedListType(ttList.TypedListType(theano.tensor.fmatrix))()
    o = ttList.length(tl)
    f = theano.function([tl], o)
    testRes = f(a)
    print testRes
    print "All finished!"
Example #3
0
def main():
    rng = numpy.random.RandomState(23455)
    a = [[[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]],
          [[1, 2, 3, 4], [1, 2, 3, 4]]]]

    docList = ttList.TypedListType(ttList.TypedListType(T.fmatrix))("docList")
    docSize = ttList.length(docList)

    modelResults, _ = theano.scan(fn=lambda i, tl: dealWithOneDoc(rng, tl[i]),
                                  non_sequences=[docList],
                                  sequences=[T.arange(docSize, dtype='int64')])

    testFunc = theano.function([docList], modelResults)
    rrrr = testFunc(a)
    print rrrr[0]
    print "All finished!"
Example #4
0
idx.append(array([0, 1, 3], dtype=int64))
idx.append(idx[2])
idx.append(array([0], dtype=int64))
E = eye(K - 1).astype(float32)
idx2 = [sp.csc_matrix(E[it, :]) for it in idx]
labels = []
labels.append(array([-1, -1, -1], dtype=float32))
labels.append(array([-1, -1, 1], dtype=float32))
labels.append(array([-1, 1, -1], dtype=float32))
labels.append(array([-1, 1, 1], dtype=float32))
labels.append(array([1], dtype=float32))

#hierarchical parameters
Wh = random.randn(K - 1, D).astype(float32)
tWh = T.matrix()
tlIdx = tl.TypedListType(T.lvector)()
tlIdx2 = tl.TypedListType(sparse.csc_fmatrix)()
tlLab = tl.TypedListType(T.fvector)()

test = tlIdx[0]
print test.eval({tlIdx: idx})

tr = T.lvector()

#we can generate outputs as well as shared var updates by returning dictionary
n = 0
tsh = theano.shared(n)
gWh = zeros(Wh.shape).astype(float32)
tgWh = theano.shared(gWh)

Example #5
0
emb_dim = EMBEDDING_DIM


emb_matrix_path = 'embedding_matrix_gensim_300D.npy'

Vocabulary_size = word_dim
x_train, word_to_index, index_to_word = load_data(INPUT_DATA_FILE, Vocabulary_size)



x_test = x_train[400000:500000]
x_train = x_train[0:400000]

#iterator counter
t = theano.shared(name = 't', value = np.array(0).astype('int32'))
x = tlist.TypedListType(T.ivector)()

#wl = T.ivector('wl')
l = tlist.length(x)

def get_shapes(index, x):

	shape_ex = T.shape(x[index])
	return shape_ex[0]

x_shapes, last_output = theano.scan(fn=get_shapes, 
							outputs_info=None, 
							non_sequences = [x],
							sequences = [T.arange(batch_size, dtype = 'int64')]
						    )
    def inner_fn(x_t, s_tm1):

        phi_1_t = phi_1.fprop([x_t, s_tm1], params)
        phi_mu_t = phi_mu.fprop([phi_1_t], params)
        phi_sig_t = phi_sig.fprop([phi_1_t], params)

        prior_1_t = prior_1.fprop([s_tm1], params)
        prior_mu_t = prior_mu.fprop([prior_1_t], params)
        prior_sig_t = prior_sig.fprop([prior_1_t], params)

        z_t = Gaussian_sample(
            phi_mu_t, phi_sig_t
        )  #in the original code it is gaussian. GMM is for the generation
        z_1_t = z_1.fprop([z_t], params)

        theta_1_t = theta_1.fprop([z_1_t, s_tm1], params)

        #theta_mu_t= TL.TypedListType(T.dtensor3)()
        theta_mu_t = TL.TypedListType(T.TensorType('float64', (False, ) * 2))()
        #heta_mu_t = []#T.ftensor3('theta_mu_t')
        #theta_mu_t = [theta_mu_y.fprop([theta_1_t], params) for theta_mu_y in theta_mu]
        for theta_mu_y in theta_mu:
            theta_mu_t.append(theta_mu_y.fprop([theta_1_t], params))

        theta_sig_t = TL.TypedListType(T.TensorType('float64',
                                                    (False, ) * 2))()
        for theta_sig_y in theta_sig:
            theta_sig_t.append(theta_sig_y.fprop([theta_1_t], params))

        coeff_t = TL.TypedListType(T.TensorType('float64', (False, ) * 2))()
        for theta_coef_y in coeff:
            coeff_t.append(theta_coef_y.fprop([theta_1_t], params))
        '''
        theta_sig_t = [theta_sig_y.fprop([theta_1_t], params) for theta_sig_y in theta_sig]
        coeff_t = [theta_coef_y.fprop([theta_1_t], params) for theta_coef_y in coeff]
        '''
        '''
        theta_mu1_t = theta_mu1.fprop([theta_1_t], params)
        theta_sig1_t = theta_sig1.fprop([theta_1_t], params)
        coeff1_t = coeff1.fprop([theta_1_t], params)

        theta_mu2_t = theta_mu2.fprop([theta_1_t], params)
        theta_sig2_t = theta_sig2.fprop([theta_1_t], params)
        coeff2_t = coeff2.fprop([theta_1_t], params)

        theta_mu3_t = theta_mu3.fprop([theta_1_t], params)
        theta_sig3_t = theta_sig3.fprop([theta_1_t], params)
        coeff3_t = coeff3.fprop([theta_1_t], params)
        '''
        #corr_t = corr.fprop([theta_1_t], params)
        #binary_t = binary.fprop([theta_1_t], params)

        # I was missing this reshape that is done before BiGMM in the original code
        '''
        theta_mu_in = theta_mu_t.reshape((x_t[0]*x_t[1], -1))
        theta_sig_in = theta_sig_t.reshape((x_t[0]*x_t[1], -1))
        coeff_in = coeff_t.reshape((x_t[0]*x_t[1], -1))
        
        y_pred1 = GMM_sampleY(theta_mu1_t, theta_sig1_t, coeff1_t) #Gaussian_sample(theta_mu_t, theta_sig_t)
        y_pred2 = GMM_sampleY(theta_mu2_t, theta_sig2_t, coeff2_t)
        y_pred3 = GMM_sampleY(theta_mu3_t, theta_sig3_t, coeff3_t)
        '''

        #y_pred = [GMM_sampleY(theta_mu_t[i], theta_sig_t[i], coeff_t[i]) for i in range(y_dim)]#T.stack([y_pred1,y_pred2],axis = 0 )
        s_t = rnn.fprop([[x_t, z_1_t], [s_tm1]], params)
        #y_pred = dissag_pred.fprop([s_t], params)

        #return s_t, phi_mu_t, phi_sig_t, prior_mu_t, prior_sig_t, z_t,  z_1_t, theta_1_t, theta_mu_t[0], theta_sig_t[0], coeff_t[0], theta_mu_t[1], theta_sig_t[1], coeff_t[1], theta_mu_t[2], theta_sig_t[2], coeff_t[2],y_pred1, y_pred2, y_pred3
        return s_t, phi_mu_t, phi_sig_t, prior_mu_t, prior_sig_t, z_t, z_1_t, theta_mu_t, theta_sig_t, coeff_t  #,y_pred
Example #7
0
idx.append(array([0, 1, 2], dtype=int64))
idx.append(idx[0])
idx.append(array([0, 1, 3], dtype=int64))
idx.append(idx[2])
idx.append(array([0], dtype=int64))
labels = []
labels.append(array([-1, -1, -1], dtype=float32))
labels.append(array([-1, -1, 1], dtype=float32))
labels.append(array([-1, 1, -1], dtype=float32))
labels.append(array([-1, 1, 1], dtype=float32))
labels.append(array([1], dtype=float32))

#hierarchical parameters
Wh = random.randn(D, K - 1).astype(float32)
tWh = T.matrix()
tlIdx = tl.TypedListType(T.lvector)()
test = tlIdx[0]
print test.eval({tlIdx: idx})
tr = T.lvector()


def act(idx, W):
    M = W.shape[0]
    N = W.shape[1]
    K = idx.shape[0]
    data = T.ones(K)
    I = sparse.csc_matrix()
    return sparse.true_dot(I, W[idx, :])


s, u = theano.scan(lambda i, L, D: D[L[i], :],