Created
May 29, 2019 07:42
-
-
Save ZJUGuoShuai/c6607e723cea9f38b8af60c09b7fccfa to your computer and use it in GitHub Desktop.
用 Tensorflow low API 实现最简单的线性回归模型训练
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| x = tf.constant([[1], [2], [3], [4]], dtype=tf.float32) | |
| y_true = tf.constant([[0], [-1], [-2], [-3]], dtype=tf.float32) | |
| linear_model = tf.layers.Dense(units=1) | |
| y_pred = linear_model(x) | |
| loss = tf.losses.mean_squared_error(labels=y_true, predictions=y_pred) | |
| optimizer = tf.train.GradientDescentOptimizer(0.01) | |
| train = optimizer.minimize(loss) | |
| init = tf.global_variables_initializer() | |
| sess = tf.Session() | |
| sess.run(init) | |
| for i in range(100): | |
| _, loss_value = sess.run((train, loss)) | |
| print(loss_value) | |
| print(sess.run(y_pred)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment