Skip to content

Instantly share code, notes, and snippets.

@ZJUGuoShuai
Created May 29, 2019 07:42
Show Gist options
  • Select an option

  • Save ZJUGuoShuai/c6607e723cea9f38b8af60c09b7fccfa to your computer and use it in GitHub Desktop.

Select an option

Save ZJUGuoShuai/c6607e723cea9f38b8af60c09b7fccfa to your computer and use it in GitHub Desktop.
用 Tensorflow low API 实现最简单的线性回归模型训练
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