TensorFlow样例一
Posted ratels
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TensorFlow样例一相关的知识,希望对你有一定的参考价值。
假设原函数为 f(x) = 5x^2 + 3,为了估计出这个函数,定义参数未知的函数g(x, w) = w0 x^2 + w1 x + w2,现要找出适合的w使g(x, w) ≈ f(x)。将这个问题转化为求解参数w使得损失函数L(w) = ∑ (f(x) - g(x, w))^2最小,求解过程使用了随机梯度下降(Stochastic Gradient Descent)。求解问题的代码如下:
1 import numpy as np 2 import tensorflow as tf 3 4 # Placeholders are used to feed values from python to TensorFlow ops. We define 5 # two placeholders, one for input feature x, and one for output y. 6 x = tf.placeholder(tf.float32) 7 y = tf.placeholder(tf.float32) 8 9 # Assuming we know that the desired function is a polynomial of 2nd degree, we 10 # allocate a vector of size 3 to hold the coefficients. The variable will be 11 # automatically initialized with random noise. 12 w = tf.get_variable("w", shape=[3, 1]) 13 14 # We define yhat to be our estimate of y. 15 f = tf.stack([tf.square(x), x, tf.ones_like(x)], 1) 16 yhat = tf.squeeze(tf.matmul(f, w), 1) 17 18 # The loss is defined to be the l2 distance between our estimate of y and its 19 # true value. We also added a shrinkage term, to ensure the resulting weights 20 # would be small. 21 loss = tf.nn.l2_loss(yhat - y) + 0.1 * tf.nn.l2_loss(w) 22 23 # We use the Adam optimizer with learning rate set to 0.1 to minimize the loss. 24 train_op = tf.train.AdamOptimizer(0.1).minimize(loss) 25 26 def generate_data(): 27 x_val = np.random.uniform(-10.0, 10.0, size=100) 28 y_val = 5 * np.square(x_val) + 3 29 return x_val, y_val 30 31 sess = tf.Session() 32 # Since we are using variables we first need to initialize them. 33 sess.run(tf.global_variables_initializer()) 34 for _ in range(1000): 35 x_val, y_val = generate_data() 36 _, loss_val = sess.run([train_op, loss], {x: x_val, y: y_val}) 37 print(loss_val) 38 print(sess.run([w]))
求解过程如下:
4380421.0 3147655.5 4625718.5 3493661.0 3061016.0 3057624.5 3104206.2 …… 103.7392 98.461266 113.29772 104.56809 89.75495 …… 17.354445 17.66056 17.716873 18.782757 16.015532 [array([[4.9863739e+00], [6.9120852e-04], [3.8031762e+00]], dtype=float32)]
以上是关于TensorFlow样例一的主要内容,如果未能解决你的问题,请参考以下文章