Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132932 views
License: OTHER
1
import theano
2
import theano.tensor as T
3
import numpy as np
4
5
coefficients = T.vector("coefficients")
6
x = T.scalar("x")
7
max_coefficients_supported = 10000
8
9
10
def step(coeff, power, prior_value, free_var):
11
return prior_value + (coeff * (free_var ** power))
12
13
# Generate the components of the polynomial
14
full_range = T.arange(max_coefficients_supported)
15
outputs_info = np.zeros((), dtype=theano.config.floatX)
16
17
components, updates = theano.scan(fn=step,
18
sequences=[coefficients, full_range],
19
outputs_info=outputs_info,
20
non_sequences=x)
21
22
polynomial = components[-1]
23
calculate_polynomial = theano.function(inputs=[coefficients, x],
24
outputs=polynomial,
25
updates=updates)
26
27
test_coeff = np.asarray([1, 0, 2], dtype=theano.config.floatX)
28
print(calculate_polynomial(test_coeff, 3))
29
30