当前位置:网站首页>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
边栏推荐
- Yyds dry goods inventory leetcode question set 751 - 760
- Druid database connection pool details
- 第五期个人能力认证考核通过名单公布
- zabbix 代理服务器 与 zabbix-snmp 监控
- Mysql Information Schema 学习(一)--通用表
- 受益匪浅,安卓面试问题
- Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
- Unbalance balance (dynamic programming, DP)
- 凤凰架构3——事务处理
- 包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
猜你喜欢
In depth analysis, Android interview real problem analysis is popular all over the network

五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”

Cereals Mall - Distributed Advanced p129~p339 (end)

Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting

Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars

【基础架构】Flink/Flink-CDC的部署和配置(MySQL / ES)

CCNP Part 11 BGP (III) (essence)

冒烟测试怎么做

How word displays modification traces

ZABBIX proxy server and ZABBIX SNMP monitoring
随机推荐
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the grouped dot strip plot, and set the add parameter to add box plots for different levels of dot strip
swagger2报错Illegal DefaultValue null for parameter type integer
Druid 数据库连接池 详解
JDBC details
Dark horse -- redis
史上超级详细,想找工作的你还不看这份资料就晚了
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
A popular explanation will help you get started
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
tensorflow和torch代码验证cuda是否安装成功
English topic assignment (25)
中缀表达式转后缀表达式详细思路及代码实现
ROS custom message publishing subscription example
Benefit a lot, Android interview questions
How to do smoke test
Don't miss this underestimated movie because of controversy!
Mysql Information Schema 学习(一)--通用表
Problems encountered in using RT thread component fish
第五期个人能力认证考核通过名单公布
打家劫舍III[后序遍历与回溯+动态规划]