tensorflow学习笔记一——justgetstarted

author author     2022-07-31     441

关键词:

  我现在什么都不知道,打算开始学习tensorflow作为机器学习入门的开始。

  昨天完成了对tensorflow官方入门介绍的学习,了解了tensorflow的基本原理和编程方法。我们在进行tensorflow编程时,程序的逻辑是:建立数据流图-->初始化变量-->运行程序。下面就每一步进行介绍。

  1. 建立数据流图
      完成了这一部分的学习,我才了解了tensorflow的意思。在tensorflow中,程序的逻辑可以表示成数据流图,图的节点是一组对tensor(向量或者矩阵)的操作,节点的输出仍是一组向量或者矩阵。在建立数据流图时,可以先给出数据,或者用变量表示输入数据,在第三步运行时再对变量进行赋值。
    import tensorflow as tf
    
    # Create a Constant op that produces a 1x2 matrix.  The op is
    # added as a node to the default graph.
    #
    # The value returned by the constructor represents the output
    # of the Constant op.
    matrix1 = tf.constant([[3., 3.]])
    
    # Create another Constant that produces a 2x1 matrix.
    matrix2 = tf.constant([[2.],[2.]])
    
    # Create a Matmul op that takes ‘matrix1‘ and ‘matrix2‘ as inputs.
    # The returned value, ‘product‘, represents the result of the matrix
    # multiplication.
    product = tf.matmul(matrix1, matrix2)

    上面一段代码就创建了简单的图界面,节点的输入是两个常量矩阵,即在创建节点时已经给出了输入值,节点的输出是两个矩阵的乘积。当然,我们还可以用变量表示节点的输入,如步骤2的 代码所示

  2. 初始化变量
    如果我们在建立数据流图时定义了变量,程序在运行之前首先需要对变量进行初始化。用于变量初始化的命令很简单:tensorflow.initialize_all_variables(),在下面的程序中定义了变量并进行了变量的初始化:
    import tensorflow as tf
    import numpy as np
    
    # Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
    x_data = np.random.rand(100).astype(np.float32)
    y_data = x_data * 0.1 + 0.3
    
    # Try to find values for W and b that compute y_data = W * x_data + b
    # (We know that W should be 0.1 and b 0.3, but Tensorflow will
    # figure that out for us.)
    W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
    b = tf.Variable(tf.zeros([1]))
    y = W * x_data + b
    
    # Minimize the mean squared errors.
    loss = tf.reduce_mean(tf.square(y - y_data))
    optimizer = tf.train.GradientDescentOptimizer(0.5)
    train = optimizer.minimize(loss)
    
    # Before starting, initialize the variables.  We will ‘run‘ this first.
    init = tf.initialize_all_variables()
    
    # Launch the graph.
    sess = tf.Session()
    sess.run(init)

     

  3. 运行程序,得到结果
    运行程序之前需要通过tensorflow.Session()建立session,然后运行程序,如果我们已经在建立流图时给出了程序的输入,那么直接将需要计算的结果作为run的参数:
    技术分享
     1 import tensorflow as tf
     2 import numpy as np
     3 #输入已经给出
     4 x_data = np.random.rand(100).astype(np.float32)
     5 y_data = x_data * 0.1 + 0.3
     6 
     7 W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
     8 b = tf.Variable(tf.zeros([1]))
     9 y = W * x_data + b
    10 
    11 # Minimize the mean squared errors.
    12 loss = tf.reduce_mean(tf.square(y - y_data))
    13 optimizer = tf.train.GradientDescentOptimizer(0.5)
    14 train = optimizer.minimize(loss)
    15 
    16 # Before starting, initialize the variables.  We will ‘run‘ this first.
    17 init = tf.initialize_all_variables()
    18 
    19 # Launch the graph.
    20 sess = tf.Session()
    21 sess.run(init)
    22 
    23 # Fit the line.
    24 for step in range(201):
    25     sess.run(train)
    26     if step % 20 == 0:
    27         #我们需要计算W和b的值,将他们作为run的参数
    28         print(step, sess.run(W), sess.run(b))
    29 
    30 # Learns best fit is W: [0.1], b: [0.3]
    View Code

    对于使用变量代表输入的程序,需要将变量赋值并作为run的参数:

    技术分享
    #定义了变量,未经性赋值
    input1 = tf.placeholder(tf.float32)
    input2 = tf.placeholder(tf.float32)
    output = tf.mul(input1, input2)
    
    with tf.Session() as sess:
        #运行时,以字典形式传入变量的值
      print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))
    View Code

     

tensorflow学习笔记一(代码片段)

1、tensorflow中的数据类型importtensorflowastfimportnumpyasnp#张量可以是数字、列表、ndarray#使用列表创建张量print(tf.constant([2,3]))print(tf.constant([[2,3],[1,4]]))#tensorflow2默认使用Eager动态图机制print(type(tf.constant([[2,3],[1,4] 查看详情

tensorflow学习笔记

一.背景  本人学习Tensorflow是为了完成毕业设计的工作,之前并没有用过其他的深度学习平台,Tensorflow作为当前很流行的一个平台,无论是教学文档,还是使用其开发的工程,这些资源都是很丰富的,所以很适合新手来进行入... 查看详情

tensorflow学习笔记(代码片段)

TensorFlow学习笔记一、常量表示importtensorflowastfm1=tf.constant([[3,3]])m2=tf.constant([[2],[3]])product=tf.matmul(m1,m2)print(product)sess=tf.Session()result=sess.run(product)print(result)sess.close()withtf.S 查看详情

tensorflow学习笔记1

什么是TensorFlow? TensorFlow是Google开发的一款神经网络的Python外部的结构包,也是一个采用数据流图来进行数值计算的开源软件库.TensorFlow让我们可以先绘制计算结构图,也可以称是一系列可人机交互的计算操作,然后把编辑好的Pyth... 查看详情

tensorflow学习笔记入门(代码片段)

一、TensorFlow是什么?是谷歌开源的机器学习实现框架,本文从Python语言来理解学习Tensorflow以及机器学习的知识。TensorFlow的API主要分两个层次,核心层和基于核心层的高级API。核心层面向机器学习的研究人员,以... 查看详情

tensorflow学习笔记(转)

转自:http://blog.csdn.net/qq_32166627/article/details/52734387侵删。 tensorflow中有一类在tensor的某一维度上求值的函数。如: 求最大值tf.reduce_max(input_tensor,reduction_indices=None,keep_dims=False,name=None)求平均值tf.red 查看详情

tensorflow学习笔记:mnist机器学习入门

...,首先从深度学习的入门MNIST入手。通过这个例子,了解Tensorflow的工作流程和机器学习的基本概念。一 MNIST数据集    MNIST是入门级的计算机视觉数据集,包含了各种手写数字的图片。在这个例子中就是通过机... 查看详情

tensorflow学习笔记(代码片段)

...13378306/article/details/56281549张量的阶、形状、数据类型 TensorFlow用张量这种数据结构来表示所有的数据.你可以把一个张量想象成一个n维的数组或列表.一个张量有一个静态类型和动态类型的维数.张量可以在 查看详情

tensorflow学习笔记读取数据

Overview  之前几次推送的全部例程,使用的都是tensorflow预处理过的数据集,直接载入即可。例如:   然而实际中我们使用的通常不会是这种超级经典的数据集,如果我们有一组图像存储在磁盘上面,如何以mini-b... 查看详情

tensorflow学习笔记补充1——interactivesession

InteractiveSession大家有时候在阅读代码时会看见InteractiveSession而不是熟悉的Session,这是什么东东呢?其实,它们只有一点不同。。。。。InteractiveSession是默认的session,这就意味着你可以在不声明session的条件下直接使用run(),eval()... 查看详情

tensorflow学习笔记一(代码片段)

1、tensorflow中的数据类型importtensorflowastfimportnumpyasnp#张量可以是数字、列表、ndarray#使用列表创建张量print(tf.constant([2,3]))print(tf.constant([[2,3],[1,4]]))#tensorflow2默认使用Eager动态图机制print(type(tf.constant([[2,3],[1,4]])))#使用numpy数组创建... 查看详情

tensorflow笔记之mnist手写识别系列一

tensorflow笔记(四)之MNIST手写识别系列一版权声明:本文为博主原创文章,转载请指明转载地址http://www.cnblogs.com/fydeblog/p/7436310.html前言这篇博客将利用神经网络去训练MNIST数据集,通过学习到的模型去分类手写数字。我会将本篇... 查看详情

tensorflow学习笔记:神经网络优化(代码片段)

本文是个人的学习笔记,是跟随北大曹健老师的视频课学习的附:bilibili课程链接和MOOC课程链接以及源码下载链接(提取码:mocm)文章目录一、预备知识1.条件判断和选择(where)2.[0,1)随机数(rando... 查看详情

tensorflow学习笔记mnist手写数字识别(代码片段)

...模型,以达到稳定的高性能,而是学会如何使用Tensorflow解 查看详情

tensorflow学习笔记

简单神经网络构造importtensorflowastfimportmatplotlib.pyplotaspltimportnumpyasnptf.set_random_seed(1)np.random.seed(1)fakedatax=np.linspace(-1,1,100)[:,np.newaxis]#shape(100,1)noise=np.random.normal(0,0.1,size 查看详情

tensorflow2.0笔记(代码片段)

Tensorflow2.0笔记本博客为Tensorflow2.0学习笔记,感谢北京大学微电子学院曹建老师目录Tensorflow2.0笔记6优化器6.1SGD6.1.1vanillaSGD6.1.2SGDwithMomentum6.1.3withNesteroyAcceleration6.2AdaGrad6.3RMSProp6优化器​ 优化算法可以分成一阶优化和二阶优化算法... 查看详情

tensorflow学习笔记

importtensorflowastfimportnumpyasnpimportmathimporttensorflow.examples.tutorials.mnistasmnsess=tf.InteractiveSession()mnist=mn.input_data.read_data_sets("E:\Python35\Lib\site-packages\tensorflow\ 查看详情

《tensorflow实战google深度学习框架(第二版)》学习笔记及书评(代码片段)

《TensorFlow实战Google深度学习框架(第二版)》学习笔记文章目录《TensorFlow实战Google深度学习框架(第二版)》学习笔记写在前面1.TensorFlow图像处理函数学习总结2.TensorFlow图像预处理完整样例3.TensorFlow多线程输入... 查看详情