-->
当前位置:首页 > 题库

PROGRAMMING:Calculation chart

Luz5年前 (2021-05-10)题库498
Computational graph is the basic execution engine of modern deep learning system. It provides a method to express any mathematical expression, such as neural network represented by directed acyclic graph. The nodes in the graph represent basic operations or input variables, and the edges represent the dependence of intermediate values between nodes. For example, the following figure is a function
$$f(x_ 1,x_ 2)=\ln x_ 1 + x_ 1 x_ 2 - \sin x_ 2$$
The calculation chart of the model is given.
![ figure.png](~/f1986194-8842-45d2-8ae8-5a4eb42752da.png)
Now, given a calculation diagram, you can calculate the value of the function and its partial derivative (i.e. gradient) according to all the input variables. For example, given the input $$X_ 1 = 2, x_ 2 = 5 $$, the function value obtained from the above calculation figure is $$f (2,5) = \ ln (2) + 2 times 5 - \ sin (5) = 11.652 $$; And according to the differential chain rule, the gradient obtained in the figure above is $$\ nabla f = [[partial f / [partial x]_ 1 , \partial f / \partial x_ 2 ] = [ 1/x_ 1 + x_ 2 , x_ 1 - \cos x_ 2] = [5.500,1.716]$$。
I know you have forgotten calculus, so I only ask you to deal with a few simple operators: addition, subtraction, multiplication, exponent ($$e ^ x $$, that is, exp (x) function in programming language), logarithm ($$$ln x $$, that is, log (x) function in programming language) and sine function ($$$SiN x $$, that is, sin (x) function in programming language).
Friendly reminder:
-The derivative of the constant is 0$$ The derivative of x $$is 1$$ The derivative of e ^ x $$is still $$e ^ x $$$$\ The derivative of LN x $$is $$1 / x $$$$\ The derivative of SiN x $$is $$\ cos x $$.
-Review what is * * partial derivative * *: in mathematics, the partial derivative of a multivariable function is its derivative with respect to one of the variables while keeping the other variables constant. In the example above, when we look at $$X_ 1 $$finding partial derivative_ 1 $, the $$X_ 2 $$is taken as a constant, so we get $$- ln X_ The derivative of 1 $$is $$1 / X_ 1$$,$$x_ 1 x_ The derivative of 2 $$is $$X_ 2$$,$$\sin x_ The derivative of 2 $$is 0.
-Let's review the * * chain rule * *: the derivative of a composite function is the product of the derivatives of the finite composite functions at corresponding points, that is, $$u = f (y) $, $$y = g (x) $$, then $$Du / DX = Du / dy / cdot dy / DX $$. For example, if we take the derivative of $$\ sin (\ ln x) $, we get $$\ cos ([ln x)] cdot (1 / x) $.
If you pay attention to the observation, you can find that in the calculation diagram, the calculation of function value is a calculation from left to right, while the calculation of partial derivative is just the opposite.
###Input format:
Enter the positive integer $$n $$($$Le 5 / times 10 ^ 4 $$) in the first line to calculate the number of vertices in the graph.
The following $$n $$line, line $$I $$gives the information of the $$I $$vertex, where $$I = 0,1, cdots, n-1 $$. The first value is the type number of the vertex
-0 represents the input variable
-1 for addition, corresponding to $$X_ 1 + x_ 2$$
-2 stands for subtraction, corresponding to $$X_ 1 - x_ 2$$
-3 stands for multiplication, corresponding to $$X_ 1 \times x_ 2$$
-4 is the index, corresponding to $$e ^ X$$
-5 represents logarithm, corresponding to $$\ ln X$$
-6 represents sine function, corresponding to $$\ sin X$$
For an input variable, it is followed by its double precision floating-point value; For the monocular operator, it will be followed by the vertex number of the corresponding single variable (the number starts from 0); For a binocular operator, it is followed by the vertex numbers of the corresponding two variables.
The topic guarantees that there is only one output vertex (i.e. the vertex without edge, such as the '-') on the rightmost side of the figure above), and the calculation process will not exceed the accuracy range of double precision floating-point numbers.
###Output format:
First, output the function value of the given graph in the first line. In the second line, output the partial derivative value of the function for each variable in order, separated by a space, and there must be no extra space at the beginning and end of the line. The output order of partial derivative is the same as that of input variable. Output 3 decimal places.
###Input example:
```in
seven
0 2.0
0 5.0
5 0
3 0 1
6 1
1 2 3
2 5 4
```
###Output example:
```out
eleven point six five two
5.500 1.716
```







answer:If there is no answer, please comment