供个人学习记录,来源于:
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按行求和