matlab错误使用tfe,tfe 简单 案例 自动优化 线性拟合

编程入门 行业动态 更新时间:2024-10-27 20:32:17

matlab错误使用tfe,tfe 简单 案例 自动优化 <a href=https://www.elefans.com/category/jswz/34/1768154.html style=线性拟合"/>

matlab错误使用tfe,tfe 简单 案例 自动优化 线性拟合

自动计算梯度

import tensorflow as tf

import tensorflow.contrib.eager as tfe

tf.enable_eager_execution()

def leaky_relu(x):

if x < 0:

return x * 0.1

else:

return x

grad = tfe.gradients_function(leaky_relu)

print(grad(4.0))

print(grad(-3.0))

[]

[]

自动根据梯度优化数值

import tensorflow as tf

tf.enable_eager_execution()

w = tf.get_variable("w", initializer=3.0)

def loss(x):

return x * w

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)

for i in range(5):

optimizer.minimize(lambda: loss(5))

print(w.numpy())

# 2.5

# 2.0

# 1.5

# 1.0

# 0.5

学习率为0.01

2.95

2.9

2.8500001

2.8000002

2.7500002

线性拟合

import tensorflow as tf

import numpy as np

tf.enable_eager_execution()

def get_data():

x = np.random.random()

y = 2 * x - 1

return x, y

lr = .01

train_step = 10000

show_step = 100

w = tf.Variable(0.)

b = tf.Variable(0.)

def loss():

x, y = get_data()

out = x * w + b

return (out - y) ** 2

def main():

train = tf.train.AdamOptimizer(lr)

for i in range(1, 1 + train_step):

train.minimize(loss)

if not i % show_step:

print(i, w.numpy(), b.numpy())

if __name__ == '__main__':

main()

9500 2.0 -1.0

9600 2.0 -1.0

9700 2.0000002 -1.0000008

9800 1.9999999 -1.0000001

9900 1.9999962 -0.9999975

10000 1.9999998 -0.99999946

更多推荐

matlab错误使用tfe,tfe 简单 案例 自动优化 线性拟合

本文发布于:2024-03-08 01:20:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1719470.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线性   错误   案例   简单   matlab

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!