Tensorflow基本用法

简介: Tensorflow基本用法

因需求需要用到些Tensorflow的知识,所以简单入门一下

首先我们去安装tensorflow,tensorflow有CPU版本和GPU版本有关tensorflow的安装可以看这个视频,讲解的很好tensorflow2.0的安装

安装好后我们进行代码操作

import tensorflow as tf
a = tf.constant(2.5)   # 定义一个常量,给a赋值为2.5
print(a)
b = tf.Variable(10, name="var")    #  定义一个变量,初始值10,名字为var
print(b)

分别定义了一个常亮和一个变量,但是打印出来确实类型的解释,tensorflow的数据都要放到session会话中执行

import tensorflow.compat.v1 as tf   #  如果是tensorflow2.0这么写     而 tensorflow 1.x写作 import tensorflow as tf
tf.disable_v2_behavior()     #  表示继续使用1.x版本的语法
a = tf.constant(2.5)   # 定义一个常量,给a赋值为2.5
print(a)
b = tf.Variable(10, name="var")    #  定义一个变量,初始值10,名字为var
print(b)
sess = tf.Session()    #创建session会话   
print(sess.run(a))
sess.close()   # 关闭session

可以看到打印了2.5

我们也可以给他指定数据类型如下

a = tf.constant(2, dtype=tf.int32)

但是我们打印这个变量b却是报错的,那是因为变量在tensorflow中都要进行初始化

init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(b))

我们也可以用with as来创建session

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
a = tf.constant(3.4)
b = tf.Variable(12)
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print(sess.run(a))
    print(sess.run(b))

tensorflow四则运算

常量的计算

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
a = tf.constant(8)
b = tf.constant(2)
x1 = tf.add(a, b)
x2 = tf.multiply(a, b)
x3 = tf.subtract(a, b)
x4 = tf.divide(a, b)
with tf.Session() as sess:
    print(sess.run(x1))
    print(sess.run(x2))
    print(sess.run(x3))
    print(sess.run(x4))

变量的运算

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
a = tf.constant(8)
b = tf.Variable(2)
init = tf.global_variables_initializer()
x1 = tf.add(a, b)
x2 = tf.multiply(a, b)
x3 = tf.subtract(a, b)
x4 = tf.divide(a, b)
with tf.Session() as sess:
    sess.run(init)
    print(sess.run(x1))
    print(sess.run(x2))
    print(sess.run(x3))
    print(sess.run(x4))

相关文章
|
1月前
|
TensorFlow 算法框架/工具
第2章 TensorFlow 基础
第2章 TensorFlow 基础
27 0
|
7天前
|
机器学习/深度学习 算法框架/工具 Python
如何使用Python的Keras库构建神经网络模型?
使用Python的Keras库构建神经网络模型,示例包括一个Sequential模型,添加了三层:输入层(64个节点,ReLU激活),一个隐藏层(32个节点,ReLU激活)和输出层(10个节点,softmax激活)。
10 1
|
7天前
|
机器学习/深度学习 PyTorch TensorFlow
TensorFlow与PyTorch在Python面试中的对比与应用
【4月更文挑战第16天】这篇博客探讨了Python面试中TensorFlow和PyTorch的常见问题,包括框架基础操作、自动求梯度与反向传播、数据加载与预处理。易错点包括混淆框架API、动态图与静态图的理解、GPU加速的利用、模型保存恢复以及版本兼容性。通过掌握这些问题和解决策略,面试者能展示其深度学习框架技能。
30 9
|
1月前
|
TensorFlow 算法框架/工具
TensorFlow基础
TensorFlow基础
27 0
|
PyTorch 算法框架/工具
pytorch中ImageFolder()使用方法
pytorch中ImageFolder()使用方法
199 0
pytorch中ImageFolder()使用方法
|
机器学习/深度学习 算法 大数据
Python-Tensorflow-优化器
Python-Tensorflow-优化器
198 0
|
TensorFlow 算法框架/工具
TensorFlow教程(2)-基本函数使用
本文主要介绍tf.argmax,tf.reduce_mean(),tf.reduce_sum(),tf.equal()的使用
124 0
TensorFlow教程(2)-基本函数使用
|
人工智能 TensorFlow 算法框架/工具
Tensorflow 打印常量hello tensorflow
Data Science Workshop Dev - Tensorflow - 2.X - 打印常量hello tensorflow
288 0
|
算法 TensorFlow 算法框架/工具
Tensorflow源码解析6 -- TensorFlow本地运行时
# 1 概述 TensorFlow后端分为四层,运行时层、计算层、通信层、设备层。运行时作为第一层,实现了session管理、graph管理等很多重要的逻辑,是十分关键的一层。根据任务分布的不同,运行时又分为本地运行时和分布式运行时。本地运行时,所有任务运行于本地同一进程内。而分布式运行时,则允许任务运行在不同机器上。 Tensorflow的运行,通过session搭建了前后端沟通的桥
3039 0
|
算法 TensorFlow 算法框架/工具
Tensorflow源码解析7 -- TensorFlow分布式运行时
# 1 概述 TensorFlow架构设计精巧,在后端运行时这一层,除了提供本地运行时外,还提供了分布式运行时。通过分布式训练,在多台机器上并行执行,大大提高了训练速度。前端用户通过session.run()启动系统执行时,target默认为空字符串"",对应的是本地运行模式。若target以"grpc://"开头,则对应的是分布式运行模式,target指定了要连接的TensorFlow执行
2064 0

相关实验场景

更多