How to write the Expression of a polynomial in C++

32 views
Skip to first unread message

Yi Isaac Yang

unread,
Aug 28, 2018, 6:47:59 AM8/28/18
to DyNet Users
Dear all,

I'm a new user of DyNet. I want to use DyNet to realize a very simple function in C++: fitting the coefficients of a polynomial. The form of the polynomial and the loss function is know. However, I don't know how to write the Expression of polynomial.

For example, a simple Fourier polynomial is:

F(x) = \sum_i^N a_i*f_i(x)

where f_i(x) is the i-th basis function, and a_i is the coefficient of the i-th basis function need to be fitted. and the form of basis function f_i(x) is

f_0(x) = 1
f_1(x) = cos(x) + sin(x)
f_2(x) = cos(2x) + sin(2x)
...
f_n(x) = cos(n*x) + sin(n*x)

In order to fit the coefficients a, I think I can write like this:

    ParameterCollection pc;
   
SimpleSGDTrainer trainer(pc);

    ComputationGraph cg;
   
    // the coefficients {a_i}
   
Parameter p_a = pc.add_parameters({1, N+1});
    Expression a = parameter(cg, p_W);

    // the input value
    vector<dynet::real> x_values;
    Expression x = input(cg, {1}, &x_values);

    // the basis function of Fourier
    vector<
Expression> f = dynet::cos(x)+dynet::sin(x);
    for(unsigned i=0;i!=N;++i)
    {
        Expression fi = dynet::cos(i*x)+dynet::sin(i*x);
        f.push_back(fi);

    }

    // I don't know how to write the
output funciton y_pred
    // I just know it should be the form like y_pred = a * f
   
    for (unsigned iter=0;iter!=ITERATIONS;++iter)
    {
        x_values=features[id];
        //
the form of loss_expr is know
        loss+=cg.forward(loss_expr);
        cg.backward(loss_expr);
        trainer.update();
    }

I think the process of the program is like this, but I don't know how to write the Expression of the polynomial y_pred.

So could anyone tell me how to write this?

Thank you very much and best regards,

Isaac

Yi Isaac Yang

unread,
Aug 30, 2018, 4:25:43 AM8/30/18
to DyNet Users
Finally, I found the solution.

I need to use the function reshape to convert the vector<Expression> to Expression:

    vector<Expression> f = dynet::cos(x)+dynet::sin(x);
   
for(unsigned i=0;i!=N;++i)
   
{
        Expression fi = dynet::cos(i*x)+dynet::sin(i*x);
        f
.push_back(fi);

    }

   
Expression ff = reshape(concatenate_cols(f), Dim({N}));
   
Expression y_pred = W*ff;



在 2018年8月28日星期二 UTC+2下午12:47:59,Yi Isaac Yang写道:
Reply all
Reply to author
Forward
0 new messages