python3下tensorflow练习(一)

1.了解tensorflow基本架构

2.用梯度下降的方法训练处模型

3.可视化样本数据,可视化训练出的模型以及可视化损失函数

 
 

"""
Created on Wed May  2 09:40:08 2018
@author: jiangcheng
"""


import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# create data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3
#打印出准备训练的样本
fig = plt.figure()  
plt.xlabel('X')  
#设置Y轴标签  
plt.ylabel('Y')    
plt.scatter(x_data,y_data,c = 'r',marker = 'x')  #'o'---圆点,‘s’---方块
#设置图标 ,左上角的图标 
plt.legend('x')  
#显示所画的图  
plt.show()  
### tensorflow structure start ###
Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
biases = tf.Variable(tf.zeros([1]))
y = Weights*x_data + biases
loss = tf.reduce_mean(tf.square(y-y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)#梯度下降,学习速率为0.5
train = optimizer.minimize(loss)
init = tf.initialize_all_variables()
###  tensorflow structure end ###


sess = tf.Session()
sess.run(init)          # Very important


cost=[]
for step in range(201):
    sess.run(train)
    if step % 20 == 0:
#        print(step, sess.run(Weights), sess.run(biases))
        print("Cost after iteration {}: {}".format(step, np.squeeze(sess.run(loss))))
        cost.append(sess.run(loss))
####训练出的模型
fig = plt.figure()    
#设置X轴标签  
plt.xlabel('Xmodel')  
#设置Y轴标签  
plt.ylabel('Ymodel')  
#画散点图  
plt.scatter(x_data,sess.run(y),c = 'y',marker = 'o')  #'o'---圆点,‘s’---方块
#设置图标 ,左上角的图标 
plt.legend('xmodel')   
plt.show()  
###打印代价
plt.plot(np.squeeze(cost))  #压缩
plt.ylabel('cost')
plt.xlabel('iterations (per 20)')
plt.title("Learning rate =" +str( 0.5))

plt.show()

 

1.训练的数据可视化

 

2.模型可视化

3.损失函数可视化

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值