tensorflow 2.0-基本操作

33 篇文章 0 订阅
30 篇文章 0 订阅
这篇博客介绍了TensorFlow的基础用法,包括创建常量、进行数值运算、矩阵操作以及变量的声明。通过示例展示了如何进行加减乘除、矩阵求和、求积与平均值,还探讨了随机数生成和最大值计算。适合初学者入门TensorFlow。
摘要由CSDN通过智能技术生成

数据

# -*- coding: utf-8 -*-
import tensorflow as tf
c1=tf.constant(2)
print (c1)
c2=tf.constant(2.5)
print (c2)
c3=tf.constant("a")
print (c3)
c4=tf.constant((1,-1),tf.complex64)
print (c4)
	

常数运算

c11=tf.constant(2)
c12=tf.constant(4)
print ((c11+c12).numpy())
print ((c11-c12).numpy())
print ((c11*c12).numpy())
print ((c11/c12).numpy())
print (tf.add(c11,c12).numpy())
print (tf.subtract(c11,c12).numpy())
print (tf.multiply(c11,c12).numpy())
print (tf.divide(c11,c12).numpy())

矩阵

t0=tf.constant([[1,2,3,4,5]],dtype=tf.float32)
print (t0)
t1=tf.constant([[1,2,3,4,5]],dtype=tf.int32)
print (t1)
t2=tf.constant([['1','2','3','4','5']],dtype=tf.string)
print (t2)

矩阵操作

mix=tf.constant([
[1,2,3],
[2,3,4],
[5,6,7]
        ])

print (mix)
print (mix.numpy())#只显示矩阵
print (mix.shape)#矩阵形状3*3
print (mix[1][1])#某行某列
print (mix[1][1].numpy())#某行某列的值
print (tf.reduce_sum(mix))#矩阵求和
print (tf.reduce_prod(mix))#矩阵求积
print (tf.reduce_mean(mix))#平均值

A=tf.constant([
[1,2,3],
[2,3,4],
[3,4,5]
        ])
B=tf.constant([
[1,2,3],
[2,3,4],
[3,4,5]
        ])
print(A*B)
print(A+B)
print(A-B)
print(tf.matmul(A,B))
print(tf.add(A,B))
print (tf.subtract(A,B))


变数

t=tf.Variable([2,1],[1,2])
print(t)
t1=tf.Variable(['Hello'],tf.string)
print(t1)
t2=tf.Variable([2.1,1],tf.float32)
print(t2)
t3=tf.Variable([2,1,0,4],tf.int32)
print(t3)
t4=tf.Variable([(2,-5),(1,-4)],tf.complex64)
print(t4)

随机值

r=tf.random.uniform([3,4],-5,5)#shape,3行4列;[-5,5]之间随机
print (r)
print(tf.round(r))#取整
r1=tf.random.normal([3,4],0,1)#68%[-1,1],95%[-2,2]
print (r1)

最大值


import numpy as np
r1=tf.random.normal([3,4],0,1)
print(np.max(r1))#最大值
print(np.max(r1,0))#每一列最大值
print(np.argmax(r1))#最大值位置
print(np.argmax(r1,0))#每一列最大值位置

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

佐倉

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值