1 基本计算单元和操作---TensorFlow个人学习笔记

本文是TensorFlow学习笔记,介绍了TensorFlow的基本知识,包括Tensor和Flow的概念,TensorFlow的工作机制,以及创建Tensor、Operations和Session的步骤。接着详细讲解了基本计算单元,如tf.zeros()、tf.ones()、tf.constant()等常用操作,以及它们在TensorFlow中的应用。
摘要由CSDN通过智能技术生成

注:不同环境下,输出结果可能有些不同。TensorFlow 2.0.0 、Python 3.7.2

作者:Irain
QQ:2573396010
微信:18802080892
视频资源:https://pan.baidu.com/s/1kg5DwTJfNTtX0toszHmYBQ 、提取码:6qrx

1 TensorFlow基本知识

Tensorflow是一个基于图的计算系统。主要应用于机器学习。

1.1 定义拆分:Tensor + Flow

Tensor:“张量”,其本质就是任意维度的数组。1维Tensor:向量(线),2维Tensor:矩阵(面),3维Tensor:球面(立体)。
Flow:图计算中的数据流。

1.2 Tensorflow的机制

把定义(静态图) 与运行(动态图)分开,先定义,后运行。所有定义作为图Graph的节点,绘画成静态图,然后,Session会话运行该图Graph中的操作。Session处理真实的运算,比较消耗资源。在使用结束后,关闭Session,释放资源。手动关闭:Session.close();自动关闭:with tf.Session() as sess:。

1.3 Tensorflow的三个操作步骤:

  1. 创建Tensor(常量、变量等);
  2. 添加Operations(Operations输入Tensor,然后输出另一个Tensor);
  3. 创建Session对象(会话,不知道是否准确),执行计算(也就是运行一个可计算的图)。(可以先创建Session对象,在Session对象内,进行上面两个操作,最后执行计算)

1.4 相关链接:

Tensorflow学习笔记1:Get Started
Tensorflow学习笔记2:About Session, Graph, Operation and Tensor
tensorflow中的session(会话)的理解
TensorFlow中Session的使用

2 基本计算单元

实例化Tensor(常量、变量)、矩阵相乘、初始化变量操作、
在这里插入图片描述

import tensorflow as tf
import os
# https://tensorflow.google.cn/api_docs/python/tf/compat/v1/disable_eager_execution?hl=en
tf.compat.v1.disable_eager_execution() # Disables eager execution.
print("Python version:",os.sys.version)
print("TensorFlow版本:",tf.__version__)
print("-"*30,"基本计算单元","-"*30)
#https://tensorflow.google.cn/guide/variable?hl=zh-cn
weights = tf.Variable([[0.2,1]],name='weights') # to represent shared, persistent state your program manipulates. 
biases = tf.constant([[2.0],[3]],name='biases') # Creates a constant tensor from a tensor-like object.
print("weights类型:",type(weights))
print("weights值:",weights)
#https://tensorflow.google.cn/api_docs/python/tf/Tensor?hl=en#__matmul__
y = tf.matmul(weights,biases) # Multiplies matrix a by matrix b, producing a * b.
#https://tensorflow.google.cn/api_docs/python/tf/compat/v1/global_variables_initializer?hl=zh-cn
init_op = tf.compat.v1.global_variables_initializer(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值