1、线性回归例子
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt #使用numpy生成200个随机点 x_data=np.linspace(-0.5,0.5,2000)[:,np.newaxis]#在-0.5到0.5之间生成200个随机数,[]里是增加一个维度,200行1列 noise=np.random.normal(0,0.2,x_data.shape)#干扰项,从正态分布中输出随机值,shape是查看矩阵或者数组的维数,生成与x_data数组一样的形状 y_data=np.square(x_data)+noise#得到一个U形图像 #定义两个placeholder占位符 x=tf.placeholder(tf.float32,[None,1])#行不确定,列是1 y=tf.placeholder(tf.float32,[None,1]) #定义神经网络中间层 Weights_L1=tf.Variable(tf.random_normal([1,10]))#权值,定义一个随机变量,随机赋值,形状是1行10列,权值是连接输入层和中间层,输入层是1个神经元,中间层是10个神经元 biases_L1=tf.Variable(tf.zeros([1,10]))#偏置值,初始化为0,注意zero函数的用途 Wx_plus_b_L1=tf.matmul(x,Weights_L1)+biases_L1#matmul是矩阵相乘,中间层信号的总和 L1=tf.nn.tanh(Wx_plus_b_L1)#L1相当于中间层的输出,双曲正切函数tanh作为激活函数.激活函数可以使输出一定非线性函数 #tf.nn :提供神经网络相关操作的支持,包括卷积操作(conv)、池化操作(pooling)