# Tensorflow学习1-简单搭建神经网络

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
if __name__=='__main__':
x_data=np.linspace(-1,1,200)[:,np.newaxis] #构建等差数列，:表示构建的等差数列，newaxis，创建新列，及把一维变2维
noise=np.random.normal(0,0.2,x_data.shape)
y_data=np.square(x_data)+noise
f1=plt.figure(1)
plt.subplot(211)
plt.scatter(x_data,y_data,marker='o',color='r',label='1',s=5)
plt.show()
x=tf.placeholder(tf.float32,[None,1])
y = tf.placeholder (tf.float32, [None, 1])
Weight_L1=tf.Variable(tf.random_normal([1,10]))
biases_L1=tf.Variable(tf.zeros([1,10]))
Wx_plus_L1=tf.matmul(x,Weight_L1)+biases_L1
L1=tf.nn.tanh(Wx_plus_L1)

Weight_L2=tf.Variable(tf.random_normal([10,1]))
biases_L2=tf.Variable(tf.zeros([1,1]))
Wx_plus_L2=tf.matmul(L1,Weight_L2)+biases_L2
predication=tf.nn.tanh(Wx_plus_L2)
#二次代价函数
loss=tf.reduce_mean(tf.square(y-predication))
#梯度下降法

with tf.Session()as sess:
sess.run(tf.global_variables_initializer())
for _ in range(2000):
sess.run(train_step,feed_dict={x:x_data,y:y_data})
#获取测试值
pre_val=sess.run(predication,feed_dict={x:x_data})
#画图
plt.figure(1)
plt.scatter(x_data,y_data)
plt.plot(x_data,pre_val,'b-',lw=2)
plt.show()

;