Tensorflow线性回归(Case 1)

%matplotlib inline
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize']=(8,6)

n_observations=100
xs=np.linspace(-3,3,n_observations)
ys=np.sin(xs)+np.random.uniform(-0.5,0.5,n_observations)
plt.scatter(xs,ys)
plt.show()

在这里插入图片描述

X=tf.placeholder(tf.float32,name='X')
Y=tf.placeholder(tf.float32,name='Y')
W=tf.Variable(tf.random_normal([1]),name='weight')   #[]一个数 [i]一维向量长为i [i,j]二维矩阵
b=tf.Variable(tf.random_normal([1]),name='bias')
Y_pred=tf.add(tf.multiply(W,X),b)
loss=tf.square(Y-Y_pred,name='loss')
learning_rate=0.01
optimizer=tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
n_samples=xs.shape[0]
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    writer=tf.summary.FileWriter('./graphs/linear_reg',sess.graph)
    for i in range(50):   #训练50轮
        total_loss=0
        for x,y in zip(xs,ys):   #每轮 数据一个个feed进去
            _,ls=sess.run([optimizer,loss],feed_dict={X:x,Y:y})
            total_loss += ls
        if i%5==0:
            print('Epoch {0}:{1}'.format(i,total_loss/n_samples))
    writer.close()
    W,b=sess.run([W,b])

# Epoch 0:[0.35652953]
# Epoch 5:[0.20358896]
# Epoch 10:[0.20359129]
# Epoch 15:[0.20359129]
# Epoch 20:[0.20359129]
# Epoch 25:[0.20359129]
# Epoch 30:[0.20359129]
# Epoch 35:[0.20359129]
# Epoch 40:[0.20359129]
# Epoch 45:[0.20359129]
plt.plot(xs,ys,'bo',label='Real_data')
plt.plot(xs,xs*W+b,'r',label='Pred_data')
plt.legend()
plt.show()

在这里插入图片描述

print(type(W))
tf.convert_to_tensor(W,tf.float32,name='W')

# numpy.ndarray
# <tf.Tensor 'W:0' shape=(1,) dtype=float32>

cmd=> tensorboard --logdir=“C:\Users\86151\TensorFlow\graphs”
浏览器输入=> localhost:6006
在这里插入图片描述

本机连服务器,获取tensorboard

mac本地机器,ssh连接服务器
(1)查询服务器主机名和ip
在这里插入图片描述

(2)服务器 后台启动tensorboard服务
在这里插入图片描述(3)本机 连接服务器 tensorboard相应服务端口,并重定向

yy@localhost ~ % ssh -L 16006:127.0.0.1:6006 mist@gpu133.mistgpu.xyz -p 10020

mist@gpu133.mistgpu.xyz's password: 
Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-107-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

This system has been minimized by removing packages and content that are
not required on a system that users do not log into.

To restore this content, you can run the 'unminimize' command.
Last login: Sun Jul 12 20:25:44 2020 from 123.123.133.186

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值