StudyAI
新闻中心
课程中心
博客专栏
开发社区
招聘专区
阅读 36次
评论 0条
徐ShoweR
TensorFlow 初级课程笔记
机器学习
8天前
1403字
36阅读
# TensorFlow 初级课程笔记 自己通过线性回归课程,然后边看边写的神经网络小例子 ```python import tensorflow as tf import numpy as np import matplotlib.pyplot as plt # 使用numpy生成200个随机值 x_data = np.linspace(-0.5,0.5,200)[:,np.newaxis] # 生成一些形状和x_data一样的干扰 noise = np.random.normal(0,0.02,x_data.shape) y_data = np.square(x_data) + noise # 定义两个placeholder x = tf.placeholder(tf.float32,[None,1]) y = tf.placeholder(tf.float32,[None,1]) # 定义神经网络中间层 # 输入层1 中间层自己定义10个神经元 Weights_L1 = tf.Variable(tf.random_normal([1,10])) biases_L1 = tf.Variable(tf.random_normal([1,10])) Wx_plus_b_L1 = tf.matmul(x,Weights_L1) + biases_L1 L1 = tf.nn.tanh(Wx_plus_b_L1) Weights_L2 = tf.Variable(tf.random_normal([10,1])) biases_L2 = tf.Variable(tf.random_normal([1,1])) Wx_plus_b_L2 = tf.matmul(L1,Weights_L2) + biases_L2 L2 = tf.nn.tanh(Wx_plus_b_L2) # 代价函数 loss = tf.reduce_mean(tf.square(y - L2)) # 梯度下降法 optimizer = tf.train.GradientDescentOptimizer(0.2) train = optimizer.minimize(loss) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for _ in range(2000): sess.run(train,feed_dict={x:x_data,y:y_data}) prediction_value = sess.run(L2,feed_dict={x:x_data}) # 画图 plt.figure() plt.scatter(x_data,y_data) plt.plot(x_data,prediction_value,'r-',lw = 5) plt.show() ``` 但是有个小问题我不太清楚 就是把tanh函数全换成sigmoid以后就失效了 没想明白原因是什么
收藏 (0)
打赏
点赞 (0)
热门评论
到顶端
发布^_^
©2017
studyai.com
版权所有
关于我们