完整神经网络栗子:
数据加入噪音,更具真实性
1 import tensorflow as tf 2 import numpy as np 3 4 def add_layer(input,in_size,out_size,activation_function=None): 5 Weights = tf.Variable(tf.random_normal([in_size,out_size])) 6 biases = tf.Variable(tf.zeros([1,out_size])+0.1) 7 Wx_plus_b = tf.matmul(input,Weights) + biases 8 if activation_function is None: 9 outputs = Wx_plus_b 10 else: 11 outputs = activation_function(Wx_plus_b) 12 return outputs 13 14 x_data = np.linspace(-1,1,300)[:,np.newaxis] 15 noise = np.random.normal(0,0.05,x_data.shape)#噪音 16 y_data = np.square(x_data)-0.5+noise 17 18 xs = tf.placeholder(tf.float32,[None,1]) 19 ys = tf.placeholder(tf.float32,[None,1]) 20 21 l1 = add_layer(xs,1,10,activation_function=tf.nn.relu) 22 predition = add_layer(l1,10,1,activation_function=None) 23 24 loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-predition),reduction_indices=[1])) 25 train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) 26 27 init = tf.initialize_all_variables() 28 sess = tf.Session() 29 sess.run(init) 30 for i in range(1001): 31 sess.run(train_step,feed_dict={xs:x_data,ys:y_data}) 32 if i % 50 == 0: 33 print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
结果:
损失函数越小,说明拟合越好(有可能过拟合)