tensorflow犯错记录1(张量使用)

1 张量使用

  1. 举例 1 (少了,号分隔符)
    错误代码、结果如下:
#张量的形状
import tensorflow as tf
tens1 = tf.constant([[[1,2,2],[2,2,3]]
                     [[3,5,6],[5,4,3]]
                     [[7,0,1],[9,1,9]]
                     [[11,12,7],[1,3,14]]],name="tens1")

#语句中包含[]、{}或()括号,中间换行的就不需要使用多行链接符
print(tens1)

在这里插入图片描述
正确代码、结果如下:

#张量的形状
import tensorflow as tf
tens1 = tf.constant([[[1,2,2],[2,2,3]],   #在英文状态下输入,号才正确
                     [[3,5,6],[5,4,3]],
                     [[7,0,1],[9,1,9]],
                     [[11,12,7],[1,3,14]]],name="tens1")

#语句中包含[]、{}或()括号,中间换行的就不需要使用多行链接符
print(tens1)

在这里插入图片描述

  1. 举例 2 (少了中括号 [ ])
    错误代码、结果:
import tensorflow as tf

scalar = tf.constant(100)
vector = tf.constant([1,2,3,4,5])
matrix = tf.constant([1,2,3],[4,5,6])
cube_matrix = tf.constant([[[1],[2],[3]],[[4],[5],[6]],[7,][8],[9]])

print(scalar.get_shape())
print(vector.get_shape())
print(matrix.get_shape())
print(cube_matrix.get_shape())

在这里插入图片描述
在这里插入图片描述
正确代码、结果:

import tensorflow as tf

scalar = tf.constant(100)
vector = tf.constant([1,2,3,4,5])
matrix = tf.constant([[1,2,3],[4,5,6]])
cube_matrix = tf.constant([[[1],[2],[3]],[[4],[5],[6]],[[7,],[8],[9]]])

print(scalar.get_shape())
print(vector.get_shape())
print(matrix.get_shape())
print(cube_matrix.get_shape())

在这里插入图片描述

  1. 举例 3 (类型不匹配)
    TensorFlow会对参与运算的所有张量进行类型检查,发现类型不匹配时会报错。
    错误代码、结果如下:
import tensorflow as tf
a = tf.constant([1,2],name="a")
b = tf.constant([2.0,3.0],name="b")

result1 = a + b
result2 = tf.add(a,b)

print(result1)
print(result2)

在这里插入图片描述
在这里插入图片描述
正确代码、结果如下:

import tensorflow as tf
a = tf.constant([1,2],name="a")
b = tf.constant([2,3],name="b")

result1 = a + b
result2 = tf.add(a,b)

print(result1)
print(result2)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值