Tensorflow 搭建神经网络(单层)
2017-09-26 16:38
826 查看
#coding=utf-8 import tensorflow as tf import numpy as np #add layer def add_layer(inputs,in_size,out_size,activation_function=None): # add one more layer and return the output of this layer w = tf.Variable(tf.random_normal([in_size,out_size])) b = tf.Variable(tf.zeros([1,out_size])+0.1) y = tf.matmul(inputs,w)+b if activation_function is None: outputs = y else: outputs = activation_function(y) return outputs #make train data x_data = np.linspace(-1,1,300)[:,np.newaxis] noise = np.random.normal(0,0.05,x_data.shape) y_data = np.square(x_data) - 0.5 + noise #print (x_data) #print (y_data) #define placeholder xs = tf.placeholder(tf.float32,[None,1]) ys = tf.placeholder(tf.float32,[None,1]) #add hidden layer l1 = add_layer(xs,1,10,activation_function = tf.nn.relu) #add output layer prediction = add_layer(l1,10,1,activation_function = None) #define loss function loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices = [1])) #定义用什么方法减少loss optimizer = tf.train.GradientDescentOptimizer(0.1).minimize(loss) with tf.Session() as sess: sess.run(tf.initialize_all_variables()) for i in range(1001): sess.run(optimizer,feed_dict = {xs:x_data,ys:y_data}) if i%100 == 0: print (sess.run(loss,feed_dict = {xs:x_data,ys:y_data})) #pre = sess.run(prediction,feed_dict = {xs:x_data,ys:y_data}) #aa = np.abs(pre - y_data) #print (aa)
相关文章推荐
- Tensorflow搭建神经网络及使用Tensorboard进行可视化
- 一文学会用 Tensorflow 搭建神经网络
- DeepMind发布Sonnet 帮你用TensorFlow快速搭建神经网络
- 用tensorflow搭建一个简单的神经网络
- TensorFlow学习笔记5:神经网络搭建
- Tensorflow神经网络的搭建
- Linux下用TensorFlow搭建神经网络
- Tensorflow学习:单层神经网络的建立
- tensorflow之搭建神经网络
- tensorflow-搭建两层神经网络
- Tensorflow搭建第一个神经网络
- 一文学会用 Tensorflow 搭建神经网络
- TensorFlow识别复杂验证码以及搭建生产环境(5)—— 设计神经网络
- TensorFlow学习笔记6:神经网络搭建(layer,estimator等)
- 【tensorflow】1.添加层及搭建神经网络
- tensorflow之安装及简单神经网络搭建
- 【懒懒的Tensorflow学习笔记三之搭建简单的神经网络模型】
- 一文学会用 Tensorflow 搭建神经网络
- 对比学习用 Keras 搭建 CNN RNN 等常用神经网络和其后端切换(Theano和TensorFlow)
- TensorFlow搭建神经网络最佳实践