深度学习框架TensorFlow实例——拟合最简单的线性函数y=ax+b

本文介绍了如何使用深度学习框架TensorFlow实现拟合最简单的线性函数y=ax+b。通过实例展示训练过程并输出中间结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        在安装完TensorFlow框架后,我们来做一个简单的例子,使用TensorFlow拟合简单的线性函数,并输出中间训练的结果。具体的解释就放在代码的注释里。

# encoding=utf-8
'''
Created on 2018年4月19日

@author: yanghang
'''
import numpy as np
import tensorflow as tf

# create data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3

# create tensorflow structure start
Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
biases = tf.Variable(tf.zeros([1]))

y = Weights * x_data + biases

loss = tf.reduce_mean(tf.square(y-y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

init = tf.initialize_all_variables()
# create tensorflow structure end

ses = tf.Session()
ses.run(init)


for step in range(201):
    if step % 20 == 0:
        print(step,ses.run(Weights),ses.run(biases))
    ses.run(train)

输出的结果如下:

0 [ 0.27708817] [ 0.]
20 [ 0.15496725] [ 0.26999283
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值