import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
x_data = np.linspace(-0.5, 0.5, 200)[:, np.newaxis]
noise = np.random.normal(0, 0.02, x_data.shape)
y_data = np.square(x_data) + noise
x = tf.placeholder(tf.float32, [None, 1])
y = tf.placeholder(tf.float32, [None, 1])
w1 = tf.Variable(tf.random_normal([1, 10]))
b1 = tf.Variable(tf.zeros([1, 10]))
input1 = tf.matmul(x, w1) + b1
L1 = tf.nn.tanh(input1)
w2 = tf.Variable(tf.random_normal([10, 1]))
b2 = tf.Variable(tf.zeros([1, 1]))
input2 = tf.matmul(L1, w2) + b2
prediction = tf.nn.tanh(input2)
loss = tf.reduce_mean(tf.square(y - prediction))
optimizer = tf.train.GradientDescentOptimizer(0.2)
result = optimizer.minimize(loss)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for _ in range(2000):
sess.run(result, feed_dict={x: x_data, y: y_data})
prediction_value = sess.run(prediction, feed_dict={x :x_data})
plt.figure()
plt.scatter(x_data, y_data)
plt.plot(x_data, prediction_value, 'r-', lw = 5)
plt.show()
3.1 tensorflow学习与应用——非线性回归
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- Sequential模型 Sequential模型字面上的翻译是顺序模型,给人的感觉是线性模型,但实际上Seque...
- 在上篇深度学习框架Keras的教程中,我们详细的介绍了Keras中的Sequential和Functional A...
- 在上篇文章中, 我们演示了一个简单的线性回归Demo.在了解了回归算法中的正向传播和反向传播之后, 我们可以用梯度...