• Tensorflow学习:(一)tensorflow框架基本概念


    一、Tensorflow基本概念

      1、使用图(graphs)来表示计算任务,用于搭建神经网络的计算过程,但其只搭建网络,不计算

      2、在被称之为会话(Session)的上下文(context)中执行图

      3、使用张量(tensor)表示数据,用“阶”表示张量的维度。关于这一点需要展开一下

                 0阶张量称为标量,表示单独的一个数

                1阶张量称为向量, 表示一个一维数组

                2阶张量称为矩阵,表示一个二维数组

                ……

                张量是几阶的可以通过张量右边的方括号数来判断。例如 t = [ [ [    ] ] ],显然这个为3阶。

      4、通过变量(Variable)维护状态

      5、使用feed和fetch可以为任意的操作赋值或者从其中获取数据

      Tensorflow是一个编程系统,使用图(graphs)来表示计算任务,图(graphs)中的节点称之为op(operation),一个op获得0个或者多个Tensor,执行计算,产生0个或多个Tensor,Tensor看作是一个n维的数组或列表。图必须在会话(Session)里被启动。

    二、tensorflow基本框架知识

    # -*- coding: utf-8 -*-
    
    import tensorflow as tf
    
    print("
    ----会话----")
    m1 = tf.constant([[3, 3]])#一行两列的矩阵,这里是矩阵乘法,所以是二维数组,注意书写格式以及矩阵乘法规则
    m2 = tf.constant([[2], [3]])#两行一列的矩阵
    
    product = tf.matmul(m1, m2)# 创建一个矩阵乘法(matmul)的op
    print(product)
    
    # sess = tf.Session()
    # result = sess.run(product)
    # print(result)
    # sess.close()
    
    with tf.Session() as sess:
        result = sess.run(product)
        print(result)
    
    print("
    ----变量----")
    x = tf.Variable([1, 2])# 定义一个变量,这里是张量的加减法,所以一维数组即可
    a = tf.constant([3, 3])# 定义一个常量
    sub = tf.subtract(x, a)# 增加一个减法op
    add = tf.add(x, sub)# 增加一个加法op
    init = tf.global_variables_initializer()
    # 在tensorflow中使用变量要初始化,此条语句也可以初始化多个变量,这句代码提示没有(),多加练习
    with tf.Session() as sess:
        sess.run(init)
        print(sess.run(sub))
        print(sess.run(add))
    
    a = tf.Variable(0, name='counter')# 创建一个变量初始化为0,并命名为counter。(此段代码中命名无作用)
    add = tf.add(a, 1)
    update = tf.assign(a, add) # 赋值
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        for i in range(5):
            print(sess.run(update))
    
    print("
    ----Fetch----")
    #sess.run([fetch1,fetch2]) 进行多个op,注意格式
    input1 = tf.constant(3.0)
    input2 = tf.constant(2.0)
    input3 = tf.constant(5.0)
    
    add = tf.add(input2,input3)
    mul = tf.multiply(input1, add)
    with tf.Session() as sess:
        result = sess.run([mul, add])
        print(result)
    
    print("
    ----placeholder占位----")
    a1 = tf.placeholder(tf.float32)
    a2 = tf.placeholder(tf.float32)
    output = tf.multiply(a1, a2)
    with tf.Session() as sess:
        print(sess.run(output,feed_dict={a1:8.0, a2:2.0}))
        

  • 相关阅读:
    浏览器嗅探
    (转)javascript中为何在匿名function函数后面还外加一个括号
    js十进制转换二进制
    css_毛玻璃
    css桌布样式
    ocr api 识别表格 图片
    Linux查看日志常用命令
    linux find 命令查找文件和文件夹
    docker上安装airflow
    文件权限中 chmod、u+x、u、r、w、x分别代表什么
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/11114449.html
Copyright © 2020-2023  润新知