Tensorflow2.0笔记 - 基础数学运算

        本笔记主要记录基于元素操作的+,-,*,/,//,%,**,log,exp等运算,矩阵乘法运算,多维tensor乘法相关运算

import tensorflow as tf
import numpy as np

tf.__version__

#element-wise运算,对应元素的+,-,*,/,**,//,%
tensor1 = tf.fill([3,3], 4)
tensor2 = tf.ones([3,3], dtype=tf.int32)
print(tensor1)
print(tensor2)

print("========tensor1 + tensor2=========\n", tensor1 + tensor2)
print("========tensor1 - tensor2=========\n", tensor1 - tensor2)
print("========tensor1 * tensor2=========\n", tensor1 * tensor2)
print("========tensor1 / tensor2=========\n", tensor1 / tensor2)
print("========tensor1 // tensor2=========\n", tensor1 // tensor2)
print("========tensor1 % tensor2=========\n", tensor1 % tensor2)
#计算tensor的元素的2次方
print("========tensor1 ** 2=========\n", tensor1 ** 2)
print("========tf.pow(tensor1, 2)===\n", tf.pow(tensor1, 2))
#开根号,tf.sqrt()
tensor1 = tf.cast(tensor1, dtype=tf.float32)
print("========tf.sqrt(tensor1)=====\n", tf.sqrt(tensor1))

#log操作,tf.math.log,注意这个函数以e为底
tensor = tf.ones([3,3], dtype=tf.float32)
print("========log(tensor)============\n", tf.math.log(tensor))
#如果要实现以任意数为底数,需要使用换底公式,下面的例子计算了以2为底,对tensor1做log操作
print("========log2(tensor1)==========\n", tf.math.log(tensor1) / tf.math.log(2.))

#指数操作,tf.exp,计算e的n次方
print("========exp(tensor1)===========\n", tf.exp(tensor1))

#矩阵乘法
#两个2x2矩阵相乘
matrix1 = tf.fill([2,2], 1)
matrix2 = tf.fill([2,2], 2)
print(matrix1, "@", matrix2)
print("==========matrix1@matrix2=========\n", matrix1 @ matrix2)
#也可以用tf.matmul()
print("==========tf.matmul(matrix1, matrix2)=\n", tf.matmul(matrix1, matrix2))

#多维tensor乘法
tensor1 = tf.ones([4, 2, 5])
tensor2 = tf.ones([4, 5, 1])
#相乘结果是一个[4,2,1]形状的tensor,具体操作是对应2*5和5*1的matrix相乘
print("==========tensor1@tensor2==========\n", tensor1@tensor2)
#相乘结果是一个[4,2,3,2]形状的tensor,具体操作是对应3*6和6*2的matrix相乘
tensor1 = tf.ones([4,2,3,6])
tensor2 = tf.ones([4,2,6,2])
print("==========tensor1@tensor2==========\n", tensor1@tensor2)
#使用broadcasting
tensor1 = tf.ones([4,2,3])
tensor2 = tf.ones([3,2])
#可以调用broadcast_to扩展,也可以直接用'@'运算符
#tensor2 = tf.broadcast_to(tensor2, [4,3,2])
print("==========tensor1@tensor2==========\n", tensor1@tensor2)

        运行结果:

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在conda中配置Tensorflow 2.0,可以按照以下步骤进行: 1. 首先,确保已经安装了conda和相应的Python版本。如果没有安装,可以从Anaconda官网下载和安装。 2. 打开Anaconda Prompt或者终端,并创建一个新的conda环境,可以使用命令:conda create -n tf2.0 python=3.7 3. 激活新创建的环境,可以使用命令:conda activate tf2.0 4. 使用conda安装Tensorflow 2.0,可以使用命令:conda install tensorflow-gpu=2.0.0 5. 等待安装完成后,就成功配置了conda中的Tensorflow 2.0环境。 另外,如果你想使用pip进行安装,也可以按照以下步骤进行: 1. 激活你的conda环境,可以使用命令:conda activate tf2.0 2. 使用pip安装Tensorflow 2.0,可以使用命令:pip install tensorflow==2.0.0 -i https://pypi.tuna.tsinghua.edu.cn/simple 这样你就成功配置了conda中的Tensorflow 2.0环境。希望对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [深度学习(基于Tensorflow2.0)学习笔记——Day2](https://download.csdn.net/download/weixin_38747211/14884742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Conda安装Tensorflow2.0](https://blog.csdn.net/u010442263/article/details/125567460)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Anaconda安装+Tensorflow2.0安装配置+Pycharm安装+GCN调试(Window 10)](https://blog.csdn.net/adreammaker/article/details/125506038)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亦枫Leonlew

希望这篇文章能帮到你

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值