将简单的线性回归转换为Tensorflow 2.0

时间:2020-01-17 19:17:49

标签: tensorflow regression

我正在尝试将此代码转换为2.0版本,但没有成功。

import os
import tensorflow as tf

x = tf.placeholder(tf.float32, name='x')
y = tf.placeholder(tf.float32, name='y')

w = tf.Variable(tf.random_uniform([1], -1.0, 1.0), name='w')
b = tf.Variable(tf.zeros([1]), name='b')
y_hat = w * x + b

loss = tf.reduce_mean(tf.square(y_hat - y))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss, name='train')

init = tf.variables_initializer(tf.global_variables(), name='init')

definition = tf.Session().graph_def
directory = 'examples/regression'
tf.train.write_graph(definition, directory, 'model.pb', as_text=False)

最重要的是尝试将其保存到model.pb,以便我可以使用rust包来使用模型。

0 个答案:

没有答案
相关问题