当前位置:网站首页>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
边栏推荐
- Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
- A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
- How to access localhost:8000 by mobile phone
- 受益匪浅,安卓面试问题
- 打家劫舍III[后序遍历与回溯+动态规划]
- 利用 clip-path 绘制不规则的图形
- Problems encountered in using RT thread component fish
- 【翻译】供应链安全项目in-toto移至CNCF孵化器
- 中缀表达式转后缀表达式详细思路及代码实现
- C # use Marshall to manually create unmanaged memory in the heap and use
猜你喜欢

A popular explanation will help you get started

Druid database connection pool details

The list of people who passed the fifth phase of personal ability certification assessment was published

Synchronous development of business and application: strategic suggestions for application modernization

Don't miss this underestimated movie because of controversy!

Zero foundation entry polardb-x: build a highly available system and link the big data screen

Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease

安装Mysql报错:Could not create or access the registry key needed for the...

Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!

MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
随机推荐
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
CCNP Part 11 BGP (III) (essence)
R language uses the order function to sort the dataframe data, and descending sorting based on a single field (variable)
第五期个人能力认证考核通过名单公布
ModuleNotFoundError: No module named ‘PIL‘解决方法
php+redis实现超时取消订单功能
Looting iii[post sequence traversal and backtracking + dynamic planning]
通俗的讲解,带你入门协程
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
Php+redis realizes the function of canceling orders over time
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
Application of clock wheel in RPC
史上超级详细,想找工作的你还不看这份资料就晚了
Pytorch common loss function
Mysql Information Schema 學習(一)--通用錶
JDBC详解
【pytorch】yolov5 训练自己的数据集
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform