当前位置:网站首页>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
边栏推荐
- How can my Haskell program or library find its version number- How can my Haskell program or library find its version number?
- R language uses rchisq function to generate random numbers that conform to Chi square distribution, and uses plot function to visualize random numbers that conform to Chi square distribution
- Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
- 【翻译】供应链安全项目in-toto移至CNCF孵化器
- Modulenotfounderror: no module named 'PIL' solution
- Mysql Information Schema 学习(一)--通用表
- Word如何显示修改痕迹
- Druid database connection pool details
- Simple understanding of MySQL database
- How to type multiple spaces when editing CSDN articles
猜你喜欢
冒烟测试怎么做
CPU负载很低,loadavg很高处理方法
潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
JDBC details
【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
Black Horse - - Redis Chapter
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
史上超级详细,想找工作的你还不看这份资料就晚了
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
随机推荐
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
1805. 字符串中不同整数的数目
Interview assault 63: how to remove duplication in MySQL?
思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理
Synchronous development of business and application: strategic suggestions for application modernization
Lick the dog until the last one has nothing (simple DP)
Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference
面试突击63:MySQL 中如何去重?
利用 clip-path 绘制不规则的图形
Reflection and illegalaccessexception exception during application
Unbalance balance (dynamic programming, DP)
The second day of rhcsa study
通俗的讲解,带你入门协程
Camel case with Hungarian notation
【pytorch】yolov5 训练自己的数据集
CCNP Part 11 BGP (III) (essence)
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
深入分析,Android面试真题解析火爆全网
【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。
[玩转Linux] [Docker] MySQL安装和配置