%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