Linear Models

这篇博客主要记录了使用TensorFlow进行线性模型的学习过程,适合个人学习参考。内容源于TensorFlow官方课程。

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

供个人学习记录,来源于:
https://github.com/machinelearningmindset/TensorFlow-Course#why-use-tensorflow

import os 
import tensorflow as tf
import numpy as np
import xlrd
from sklearn.utils import check_random_state
import matplotlib.pyplot as plt

n = 50
XX = np.arange(n)  #np.arange定义一个np数组
rs = check_random_state(0)  #check_random_state检查设置种子点
YY = rs.randint(-10,10,size=(n,))+2.0*XX  #randint在(low,high)中随机初始化int类型
data = np.stack([XX,YY],axis=1)  #np.stack拼接两个数组,0按行1按列

num_epochs = 50

W = tf.Variable(0.0,name="weights")
b = tf.Variable(0.0,name="bias")

def inputs():
  X = tf.placeholder(tf.float32,name="X")  #tf.placeholder提前准备好接收器
  Y = tf.placeholder(tf.float32,name="Y")
  return X,Y
  
def inference(X):
  return X*W+b
 
def loss(X,Y):
  Y_predicted = inference(X)
  return tf.reduce_sum(tf.squared_difference(Y,Y_predicted))/(2*data.shape[0])  #reduce_sum求和,null求总和,0按列求和,1按行求和
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值