当前位置:网站首页>Tensorflow2.0 self defined training method to solve function coefficients
Tensorflow2.0 self defined training method to solve function coefficients
2022-07-06 19:29:00 【Hekai】
When doing curve fitting and approximation , If you know the approximate equation of fitting , How to find the coefficients of the equation ?
Actually in numpy There are already functions in to find polynomial coefficients , as follows
# x and y Corresponding to the coordinate axis x and y.
z = np.polyfit(x, y, 3) # use 3 Quadratic polynomial fitting , The output coefficient ranges from high to 0
# p Is the equation
p = np.poly1d(z) # Use degree to synthesize polynomials
But sometimes curves are more than polynomials , It's an exponential function 、 power function , This requires us to write by ourselves , In fact, it's the same as finding the coefficients of a linear function before , It's just another equation
# Here we define independent variables and dependent variables x,y.
x = tf.constant([1, 2, 3, 4, 5, 6],dtype=tf.float64)
y = tf.constant([11, 23, 45, 46, 48, 59],dtype=tf.float64)
# Here we define the parameters that need to be updated during reverse transmission , That is, the parameters we require
w_a = tf.Variable(-0.0, dtype=tf.float64,name='a')
w_b = tf.Variable(0.1, dtype=tf.float64,name='b')
w_c = tf.Variable(-0.0, dtype=tf.float64,name='c')
w_d = tf.Variable(2.1, dtype=tf.float64,name='d')
# Put our coefficients in an array , Convenient back transmission
variables=[w_a, w_b, w_c, w_d]
# Define the number of cycles
epoch = 1000
# Define optimizer
optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3)
# loop
for e in range(epoch):
with tf.GradientTape() as tape:
# Prediction function
y_pred=w_a*tf.exp(w_b*x)+w_c*x+w_d
# Loss function
loss=tf.reduce_sum(tf.square(y_pred-y))
# Take the derivative of the variable
grads=tape.gradient(loss, variables)
# Adjust the parameters
optimizer.apply_gradients(grads_and_vars=zip(grads, variables))
# Print the results
if e%100==0:
print(f"step: {
e}, loss: {
loss}, a: {
w_a.numpy()}, b: {
w_b.numpy()}, c: {
w_c.numpy()}, d: {
w_d.numpy()}")
# Output parameters
print(variables)
sauce
边栏推荐
- Modulenotfounderror: no module named 'PIL' solution
- Is not a drawable (color or path): the vector graph downloaded externally cannot be called when it is put into mipmap, and the calling error program crashes
- Problems encountered in using RT thread component fish
- How to do smoke test
- Mysql Information Schema 学习(一)--通用表
- Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
- JDBC details
- Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference
- Looting iii[post sequence traversal and backtracking + dynamic planning]
- The list of people who passed the fifth phase of personal ability certification assessment was published
猜你喜欢
学习探索-无缝轮播图
Characteristic colleges and universities, jointly build Netease Industrial College
思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!
接雨水问题解析
Simple understanding of MySQL database
在解决了 2961 个用户反馈后,我做出了这样的改变...
零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
Dark horse -- redis
随机推荐
Simple understanding of MySQL database
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
【翻译】供应链安全项目in-toto移至CNCF孵化器
Druid database connection pool details
Simple application of VBA script in Excel
Problems encountered in using RT thread component fish
Elastic search indexes are often deleted [closed] - elastic search indexes gets deleted frequently [closed]
The second day of rhcsa study
黑馬--Redis篇
An error occurs when installing MySQL: could not create or access the registry key needed for the
Spark foundation -scala
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
In depth analysis, Android interview real problem analysis is popular all over the network
Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
Mysql Information Schema 學習(一)--通用錶
PMP每日一练 | 考试不迷路-7.6
中缀表达式转后缀表达式详细思路及代码实现
PMP practice once a day | don't get lost in the exam -7.6