4tf矩阵基础1

tf矩阵基础1
#placehold
import tensorflow as tf
data1 = tf.placeholder(tf.float32)#浮点类型
data2 = tf.placeholder(tf.float32)
dataAdd = tf.add(data1,data2)
with tf.Session() as sess:
    print(sess.run(dataAdd,feed_dict={data1:6,data2:2}))
    # 参数1 dataAdd 参数2 data (feed_dict = {参数1:6,参数2:2})
print('end!')
  • 问题

    C:\Users\16603\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_qint8 = np.dtype([("qint8", np.int8, 1)])
    C:\Users\16603\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
    C:\Users\16603\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_qint16 = np.dtype([("qint16", np.int16, 1)])
    
  • 解决(对应日志提示一个一个进行修改)

    找到错误日志里面对应目录下面的文件进行修改(518行)C:\Users\16603\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or ‘1type’

    将_np_qint16 = np.dtype([(“qint16”, np.int16, 1)])

    改为_np_qint16 = np.dtype([(“qint16”, np.int16, (1,))])

  • 解决以后输出正确结果

    8.0
    end!
    
  1. 矩阵定义以及输出
  • #矩阵类比 数组 M行N列 外部一定要有[]   内部[]  [里面 列数据]   里面[] 中括号的个数 行数
    #[[6,6]] [[6,6]]
    import tensorflow as tf
    data1 = tf.constant([[6,6]])#一行:内部一个中括号,两列:两个数据
    data2 = tf.constant([[2],
                         [2]])#两行一列
    data3 = tf.constant([[3,3]])#一行两列
    data4 = tf.constant([[1,2],
                         [3,4],
                         [5,6]])#三行两列
    print('打印data4的维度:',data4.shape)# 维度
    #print(data4.shape)# 维度(行数,列数)
    with tf.Session() as sess:
        print('打印data4整体:\n',sess.run(data4)) #打印整体
        print('打印data4第一行:',sess.run(data4[0]))# 打印某一行
        print('打印data4第一列:',sess.run(data4[:,0]))#MN 列
        print('打印data4第一行第二列:',sess.run(data4[0,1]))# 1 1  MN = 0 32 = M012 N01
    

    运行结果:

    打印data4的维度: (3, 2)
    打印data4整体:
     [[1 2]
     [3 4]
     [5 6]]
    打印data4第一行: [1 2]
    打印data4第一列: [1 3 5]
    打印data4第一行第二列: 2
    
    3.矩阵运算理论

    import tensorflow as tf
    data1 = tf.constant([[6,6]])
    data2 = tf.constant([[2],
                         [2]])
    data3 = tf.constant([[3,3]])
    data4 = tf.constant([[1,2],
                         [3,4],
                         [5,6]])
    matMul = tf.matmul(data1,data2)#矩阵乘法: 1X2  2X1得1X2
    matMul2 = tf.multiply(data1,data2)#
    matAdd = tf.add(data1,data3)#矩阵加法要相同1X2
    with tf.Session() as sess:
        print('1.[[6,6]]乘[[2],[2]]:matmul普通乘法',sess.run(matMul))#1 维 M=1 N2. 1X2(MK) 2X1(KN) = 1
        print('2.[[6,6]]加[[3,3]]:',sess.run(matAdd))#1行2列
        print('3.[[6,6]]乘[[2],[2]]:multiply\n',sess.run(matMul2))# 1x2 2x1 = 2x2 multiply类似于加法(乘),但不需要相同
        print('4.三种结果一次性打印:',sess.run([matMul,matAdd,matMul2]))#一次打印多种结果
    
    • 运行结果:

      1.[[6,6]]乘[[2],[2]]:matmul普通乘法 [[24]]
      2.[[6,6]]加[[3,3]]: [[9 9]]
      3.[[6,6]]乘[[2],[2]]:multiply
       [[12 12]
       [12 12]]
      4.三种结果一次性打印: [array([[24]]), array([[9, 9]]), array([[12, 12],
             [12, 12]])]
      
    矩阵初始化
    1. 矩阵初始化
    import tensorflow as tf
    #初始化并指定行列
    mat0 = tf.constant([[0,0,0],[0,0,0]])#两行三列初始化
    mat1 = tf.zeros([2,3])#zeros初始化
    mat2 = tf.ones([3,2])
    mat3 = tf.fill([2,3],15)
    with tf.Session() as sess:
        print('1.直接赋值方式初始化:\n',sess.run(mat0))
        print('2.使用 zeros 初始化0:\n',sess.run(mat1))
        print('3.使用 ones 初始化1:\n',sess.run(mat2))
        print('4.使用 fill 初始化15:\n',sess.run(mat3))
    
    • 结果
    1.直接赋值方式初始化:
     [[0 0 0]
     [0 0 0]]
    2.使用 zeros 初始化0:
     [[0. 0. 0.]
     [0. 0. 0.]]
    3.使用 ones 初始化1:
     [[1. 1.]
     [1. 1.]
     [1. 1.]]
    4.使用 fill 初始化15:
     [[15 15 15]
     [15 15 15]]
    
    1. 一些特殊矩阵
    import tensorflow as tf
    mat1 = tf.constant([[2],[3],[4]])
    mat2 = tf.zeros_like(mat1)#与mat1维度相同并且初始化为0
    mat3 = tf.linspace(0.0,2.0,11)#0.0-2.0分成十等分 中间11个数据
    mat4 = tf.random_uniform([2,3],-1,2)#随机矩阵序列,并指定维度-1——2
    with tf.Session() as sess:
        print('打印constant([[2],[3],[4]]):\n',sess.run(mat1))
        print('打印zeros_like(mat1):\n',sess.run(mat2))
        print('打印linspace(0.0,2.0,11):\n',sess.run(mat3))
        print('打印random_uniform([2,3],-1,2):\n',sess.run(mat4))
    
    • 结果:
    打印constant([[2],[3],[4]]):
     [[2]
     [3]
     [4]]
    打印zeros_like(mat1):
     [[0]
     [0]
     [0]]
    打印linspace(0.0,2.0,11):
     [0.        0.2       0.4       0.6       0.8       1.        1.2
     1.4       1.6       1.8000001 2.       ]
    打印random_uniform([2,3],-1,2):
     [[ 1.1290278  -0.16853952  0.76491785]
     [ 0.3711592   1.7550797   1.3344793 ]]
    
     1.4       1.6       1.8000001 2.       ]
    打印random_uniform([2,3],-1,2):
     [[ 1.1290278  -0.16853952  0.76491785]
     [ 0.3711592   1.7550797   1.3344793 ]]
    
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Clark-dj

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

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

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

打赏作者

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

抵扣说明:

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

余额充值