标签:global ros 算法 variable ini 代码 vector tor 1.0
标签(空格分隔): tensorflow
import numpy as np
import tensorflow as tf
path = ‘D:\tensorflow_quant\ailib\log_tmp‘
# 生成x数据
points = 100
vectors = []
for i in range(points): # y=0.1*x + 0.2
x = np.random.uniform(0, 0.66)
y = x * 0.1 + 0.2 + np.random.uniform(0, 0.04)
vectors.append([x, y])
x_data = [v[0] for v in vectors]
y_data = [v[1] for v in vectors]
#形成计算图
w = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = w * x_data + b
#定义损失函数
loss = tf.reduce_mean(tf.square(y-y_data))
#定义优化器
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
#对计算图开始计算
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
for step in range(1000):
sess.run(train)
if step%5==0:
print(step,sess.run(loss),sess.run(w),sess.run(b))
#生成计算日志
writer = tf.Summary.FileWriter(path,sess.graph)
标签:global ros 算法 variable ini 代码 vector tor 1.0
原文地址:https://www.cnblogs.com/guanzhicheng/p/9211464.html