首页 > 代码库 > tensorflow2
tensorflow2
# step1 加载包
import tensorflow as tfimport numpy as np
# step2 输入:随机产生数据# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3x_data = http://www.mamicode.com/np.random.rand(100).astype(np.float32)y_data = http://www.mamicode.com/x_data * 0.1 + 0.3
#step 3: 参数:定义参数并初始化# Try to find values for W and b that compute y_data = http://www.mamicode.com/W * x_data + b"stx-comment"># (We know that W should be 0.1 and b 0.3, but TensorFlow will# figure that out for us.)W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))b = tf.Variable(tf.zeros([1]))y = W * x_data + b
#steo 4:预测的值y,损失函数,求解器# Minimize the mean squared errors.loss = tf.reduce_mean(tf.square(y - y_data))optimizer = tf.train.GradientDescentOptimizer(0.5)train = optimizer.minimize(loss)
# step 5:初始化# Before starting, initialize the variables. We will ‘run‘ this first.init = tf.initialize_all_variables()
# step 6: 创建会话并运行初始化# Launch the graph.sess = tf.Session()sess.run(init)
# step 7: 迭代求解# Fit the line.for step in range(201): sess.run(train) if step % 20 == 0: print(step, sess.run(W), sess.run(b))# Learns best fit is W: [0.1], b: [0.3]
tensorflow2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。