tensorflow 基础(一)-----张量tensor

实数:1

一维张量(列表):[1,2,3]  

二维张量(矩阵):[[1,2], [3, 4]]

三维张量:[[[1,2], [3,4], [5, 6]]]

进行张量运算时,需要注意tensor的形状

通过会话session()计算节点的值

import tensorflow as tf

# 定义a,b为常数
a = tf.constant(1)
b = tf.constant(2)
print(a + b)

print("*********************")

# 定义一维张量(列表)
A = tf.constant([1,2,3])
B = tf.constant([10, 12, 13])
print(A,"\n", B)

# 定义二维张量(矩阵)
V1 = tf.constant([[1,2], [3, 4]])
V2 = tf.constant([[1], [2]])

# 定义会话
with tf.Session() as sess:
    # 初始化所有变量
    init_op = tf.global_variables_initializer()
    # 为变量赋初始值
    sess.run(init_op)

    print(sess.run(tf.matmul(V1, V2))) # 只用两个矩阵(二维张量)才能用 matmul() 函数
    print(sess.run(A * B))             # 列表中对应元素相乘
Tensor("add:0", shape=(), dtype=int32)
*********************
Tensor("Const_2:0", shape=(3,), dtype=int32) 
Tensor("Const_3:0", shape=(3,), dtype=int32)


[[ 5]
 [11]]
[10 24 39]

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值