【TensorFlow基础操作笔记】图+对话+张量+变量+线性回归实例


这是博主的一篇笔记性质的博客,随着学习会不断更新。

基本结构(图 + 对话)

TensorFlow的基本结构是 图 + 对话。

import tensorflow as tf
def tensorflow_demo():
    '''
    Tensorflow的基本结构
    '''
    a_t = tf.constant(2)
    b_t = tf.constant(3)
    c_t = tf.add(a_t,b_t)
    
    with tf.Session() as sess:
        c_t_value = sess.run(c_t)
        print("c_t_value = ",c_t_value)

图的演示

包括默认图和在新建图,这两者互不打扰,

def graph_demo():
    '''
    图的演示
    '''
    a_t = tf.constant(2)
    b_t = tf.constant(3)
    c_t = tf.add(a_t,b_t)
    #默认图,如果不定义,则数据都在默认图中
    default_g = tf.get_default_graph()
    print("default_g = \n",default_g)
    print("a_t的默认图属性 \n",a_t.graph)       
    #新建图   
    new_g = tf.Graph()
    with new_g.as_default():
        a_new = tf.constant(20,name = "a_new")
        print(a_new)
        b_new = tf.constant(30,name = "b_new")
        c_new = tf.add(a_new,b_new,name = "c_new")
        print("a_new的自定义图属性 \n",a_new.graph)
        
    with tf.Session() as sess1:
        #c_t_value = sess1.run(c_t)
        print("c_t_value = ",c_t.eval())
        print("默认图属性 \n",sess1.graph)
    with tf.Session(graph = new_g) as sess2:
        c_new_value = sess2.run(c_new)
        print("c_new_value = ",c_new_value)
        print("自定义图属性 \n",sess2.graph)
        path = "./tmp/summary"
        shutil.rmtree(path)
        tf.summary.FileWriter(path,graph = sess2.graph)

对话的演示

对话主要掌握 run 和 feed_dict

def session_demo():
    #session\run\feed_dict
    a_t = tf.constant(2)
    b_t = tf.constant(3)
    c_t = tf.add(a_t,b_t)
    
    a = tf.placeholder(tf.float32,name = "a")
    print(a)
    b = tf.placeholder(tf.float32,name = "b")
    print(b)
    c = tf.multiply(a,b)
    #默认图,如果不定义,则数据都在默认图中
    #default_g = tf.get_default_graph()
    #print("default_g = \n",default_g)
    #print("a_t的默认图属性 \n",a_t.graph)

    with tf.Session(config = tf.ConfigProto(allow_soft_placement=True,
                                            log_device_placement=True)) as sess1:
        a_t_value,b_t_value,c_t_value = sess1.run([a_t,b_t,c_t])
        print("a_t_value = ",a_t_value,",b_t_value = ",b_t_value,",c_t_value = ",c_t_value)
        c_value = sess1.run(c, feed_dict = {a:2.5,b:3.3})
        print("c_value = ",c_value)

张量的演示

tensor这里要掌握初始化,数据类型的转换(tf.cast)、张量形状的改变。对于静态形状已经确定了的,则不能再改变,对于未确定的,可以利用something.set_shape()来改变。对于动态形状,可以利用tf.reshape()来改变,但是要注意,要保证前后元素的个数一致。

def tensor_demo():
    #张量的初始化
    x = tf.constant(2)
    y = tf.constant([1,2,3,4])
    linear_squares = tf.constant([[4],[9],[16],[25]],dtype = tf.int32)
    z = tf.zeros(shape = (3,4),dtype=tf.float32)
    d= tf.random_normal(shape = (2,3),mean=0,stddev=1,dtype=tf.float32)
    
    print("x  ===",x)
    print("y  ===",y)
    print("linear_squares  ===",linear_squares)
    print("z  ===",z)
    
    with tf.Session() as sess:
        print("z: ",sess.run(z),type(sess.run(z)))
        print("d: ",sess.run(d),type(sess.run(d)))
        
    #张量类型的修改
    x_cast = tf.cast(x,dtype=tf.float32)
    print("x_cast  ===",x_cast)
    
    #张量形状的修改
    a_p = tf.placeholder(shape=[None,None],dtype=tf.float32)
    b_p = tf.placeholder(shape=[None,10],dtype=tf.float32)
    c_p = tf.placeholder(shape=[4,2],dtype=tf.float32)#只能修改动态形状,而且要保证元素个数不变
    print("a_p  ===",a_p)
    print("b_p  ===",b_p)
    print("c_p  ===",c_p)
    #动态形状的修改:对于未确定的静态形状,才可以修改静态形状
    a_p.set_shape([2,3])
    b_p.set_shape([2,10])
    print("a_p  ===",a_p)
    print("b_p  ===",b_p)
    #动态形状的修改
    a_p_reshape = tf.reshape(a_p,[2,3,1])
    print("a_p_reshape  ===",a_p_reshape)
    c_p_reshape = tf.reshape(c_p,[2,4])#修改动态形状
    print("c_p_reshape  ===",c_p_reshape)
    c_p_reshape = tf.reshape(c_p,[2,2,2])
    print("c_p_reshape  ===",c_p_reshape)

变量

变量是用来保存模型参数的。

import tensorflow as tf

#创建变量
#变量通过tf.Variable()创建,变量的特点是:
#存储持久化
#可修改值
#可指定被训练
def variable_demo():
    #修改命名空间,使结构更加清晰
    with tf.variable_scope('my_scope'):
        a = tf.Variable(initial_value = 50)
        b = tf.Variable(initial_value = 40)
    with tf.variable_scope('your_scope'):
        c = tf.add(a,b)
    print(a)
    print(b)
    print(c)
    #一定一定要全局变量初始化!!!
    init = tf.global_variables_initializer()
    
    with tf.Session() as sess:
        sess.run(init)
        a_value,b_value,c_value = sess.run([a,b,c])
        print(a_value)
        print(b_value)
        print(c_value)
        #tensorboard可视化,生成统计日志
        path = "./tmp/summary"
        tf.summary.FileWriter(path,graph = sess.graph)
    
if __name__ == "__main__":
    variable_demo()

一个线性回归的案例

这是一个关于LR的实例,包括Tensorboard可视化,添加命名空间,事件文件数据的收集以及写入,代码都有注释。

要注意一下tf.summary.Filewriter()的返回值,也要注意tf.reduce_mean的用法,因为一个loss是一个100维的列向量,要注意一下。

# -*- coding: utf-8 -*-
"""
Created on Wed Nov 11 19:03:16 2020

@author: Administrator
"""

'''
原理:
①构建模型:假设函数(y = w1*x1+w2*x2+……)
②构架损失函数:loss = 均方误差
③优化方法:梯度下降

真实数据(100个):y_true = 0.8*x + 1
特征值  x  (100,1) 标签值 y_true  (100,1)
y_predict = x * weights(1,1) + bias(1,1)
loss = (y_predict - y_true)^2
'''

import tensorflow as tf
import numpy as np

DATA_SIZE = 100
ECOPE = 1000

def linear_regression():
    #数据准备
    with tf.variable_scope("Data_preparation"):
        x = tf.random_normal(shape=[DATA_SIZE,1],dtype=tf.float32)
        y_true = tf.matmul(x,[[2.0]]) + 0.7
    #构建模型
    with tf.variable_scope("model"):
        weights = tf.Variable(initial_value=tf.random_normal(shape=[1,1]))
        bias = tf.Variable(initial_value=tf.random_normal(shape=[1,1]))
        y_predict = tf.matmul(x,weights) + bias
    #损失函数,注意reduce_mean
    with tf.variable_scope("loss"):
        loss = tf.reduce_mean(tf.square(y_predict - y_true))
    #优化损失
    with tf.variable_scope("train"):
        optimization = tf.train.GradientDescentOptimizer(
                learning_rate=0.01).minimize(loss)
    #全局变量初始化
    init = tf.global_variables_initializer()
    #收集事件文件中的变量
    tf.summary.scalar('loss',loss)##收集标量用scalar
    tf.summary.histogram('weights',weights)#收集高维用histogram
    tf.summary.histogram('bias',bias)#收集高维用histogram
    #合并所收集到的变量
    merged = tf.summary.merge_all()
    with tf.Session() as sess:
        sess.run(init)
        #查看训练前的模型参数
        print("weights_before = ",weights.eval(),"bias_before = ",bias.eval())
        #创建事件文件
        path = "./tmp/summary"
        file_writer = tf.summary.FileWriter(path,graph = sess.graph)
        #开始训练
        for i in range(ECOPE):
            sess.run(optimization)
            print("average_loss =",loss.eval())
            #将每次收集的数据写入事件文件
            summary = sess.run(merged)
            file_writer.add_summary(summary,i)
        print("weights_after = ",weights.eval(),"bias_after = ",bias.eval())
        
if __name__ == '__main__':
    linear_regression()

可视化结果:
loss:
在这里插入图片描述
模型结构:
在这里插入图片描述
bias:
在这里插入图片描述
weights:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值