zoukankan      html  css  js  c++  java
  • 深度学习(机器学习)tensorflow学习第一课——tensor数据载体中的基本数据类型

    Tensorflow中的tensor数据载体中的基本数据类型

    # 在记录基本数据类型的时候,我们先说一下Tensor,Tensor是
    # tensor 是tensorflow的一个数据载体,里面有大量的数据存储
    # tensor 中存储的数据有哪些
    # 1.scalar:1.1
    # 2.vector:[1.1],[1.1,1.2]
    # 3.matrix:[[1.1,2.2]] 相当于一个矩阵
    # 4.tensor:rank>2 只要是维度大于2 都可以叫做tensor,tensor
    # 基本代表的机器学习的所有数据类型

    1.创建基本数据类型

    import tensorflow as tf
    import numpy as np
    
    
    # 基本数据类型 (1) 为 int
    x = tf.constant(1)
    # tf.Tensor(1, shape=(), dtype=int32)
    a = tf.constant(2)
    # tf.Tensor(2, shape=(), dtype=int32)
    b = tf.constant(3)
    # tf.Tensor(3, shape=(), dtype=int32)
    c = tf.constant(4)
    # tf.Tensor(4, shape=(), dtype=int32)
    print(x, a, b, c)
    
    
    # 基本数据类型 (1.) 为 float
    x = tf.constant(1.)
    # tf.Tensor(1.0, shape=(), dtype=float32)
    a = tf.constant(2.)
    # tf.Tensor(2.0, shape=(), dtype=float32)
    b = tf.constant(3.)
    # tf.Tensor(3.0, shape=(), dtype=float32)
    c = tf.constant(4.)
    # tf.Tensor(4.0, shape=(), dtype=float32)
    
    print(x, a, b, c)
    
    
    # 基本数据类型 bool
    bol = tf.constant([True, False])
    print(bol)
    # tf.Tensor([ True False], shape=(2,), dtype=bool)
    
    
    
    # 基本数据类型支持 string (简单了解一下)
    st = tf.constant('hellow tensorflow')
    print(st)
    # tf.Tensor(b'hellow tensorflow', shape=(), dtype=string)

    2.获取基本数据类型

    with tf.device('gpu'):
        r = tf.range(4)  # 默认range(4) 就相当于 一个python中的列表 [0,1,2,3]
        r1 = tf.range(1, 5)  # 从1开始,不超过5
    
    result_r = tf.rank(r)
    print(r)  # tf.Tensor([0 1 2 3], shape=(4,), dtype=int32)
    print(r1)  # tf.Tensor([1 2 3 4], shape=(4,), dtype=int32)
    print(result_r)  # tf.Tensor(1, shape=(), dtype=int32)
    
    print(tf.is_tensor(r))  # True 判断是不是为 tensor数据
    
    
    # 获取类型
    a1 = tf.constant(1.)
    b1 = tf.constant(1)
    c1 = tf.constant([1.])
    d1 = tf.constant([True, False])
    f1 = tf.constant('hellow tensorflow')
    print(a1.dtype, b1.dtype, c1.dtype, d1.dtype, f1.dtype)  # 判断类型
    # <dtype: 'float32'> <dtype: 'int32'> <dtype: 'float32'> <dtype: 'bool'> <dtype: 'string'>
    
    print(a1.dtype == tf.float32)  # True  可以直接进行比较

    3.基本数据类型之间的相互转换

    # 将 numpy数据转换成 Tensor数据 要用到 tf.convert_to_tensor()
    a2 = np.arange(5)  # 新建一个 numpy 数组
    print(a2.dtype, a2)  # int32 [0 1 2 3 4]
    
    # 将numpy 中的a2 数据转换成tensor数据,但是默认转换的过程中,int32是不变的
    convert_a2 = tf.convert_to_tensor(a2)
    print(convert_a2)  # tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int32)
    
    # 如果需要将位也转变的话,就要加上第二个参数
    convert_a2 = tf.convert_to_tensor(a2, dtype=tf.int64)
    print(convert_a2)  # tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int64)
    
    # 将 int32 数据转换成 float32 数据 要用到 tf.cast
    a3 = tf.constant(1)
    print(a3)  # tf.Tensor(1, shape=(), dtype=int32)
    convert_a3 = tf.cast(a3, dtype=tf.float32)  # 讲int32 类型的a3 转换成 float32
    print(convert_a3)  # tf.Tensor(1.0, shape=(), dtype=float32)
    
    a4 = np.arange(5)
    print(a4.dtype, a4)  # int32 [0 1 2 3 4]
    #
    convert_a4 = tf.convert_to_tensor(a4, dtype=tf.float32)
    print(convert_a4)  # tf.Tensor([0. 1. 2. 3. 4.], shape=(5,), dtype=float32)
    
    again_convert_a4 = tf.cast(convert_a4, dtype=tf.int64)
    print(again_convert_a4)  # tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int64)
    
    # 将int 转换为 bool
    in1 = tf.constant([0, 1])
    print(in1)  # tf.Tensor([0 1], shape=(2,), dtype=int32)
    convert_in1 = tf.cast(in1, dtype=tf.bool)
    print(convert_in1)  # tf.Tensor([False  True], shape=(2,), dtype=bool)
    
    # 将 bool转换为 int
    bol1 = tf.constant([True, False])
    print(bol1)  # tf.Tensor([ True False], shape=(2,), dtype=bool)
    
    convert_bol = tf.cast(bol1, dtype=tf.int32)
    print(convert_bol)  # tf.Tensor([1 0], shape=(2,), dtype=int32)

    4.其他属性

    # tensor 数据包装一个求导属性 要用到tf.Variable(),包装之后就会有可求导的特性
    # 例子
    a = tf.range(5)
    vb_a = tf.Variable(a, name='input_data')
    # 当tensor 数据 a 被tf.Variable() 之后 会有两个默认属性
    # 1.name属性(基本不用)
    # 2.vb_a.trainable (就是具有求导属性,后期详细讲解)
    print(vb_a)  # <tf.Variable 'input_data:0' shape=(5,) dtype=int32, numpy=array([0, 1, 2, 3, 4])>
    b1 = vb_a.trainable
    print(b1)  # True
    
    print(tf.is_tensor(vb_a))  # True 转换之后还是tensor数据
    
    # tensor 数据  转变为 numpy
    
    a=tf.constant(1.)
    num_a=a.numpy() # 直接调用即可,numpy 主要运行在cpu,tensor主要运行在GPU
    print(a.dtype,num_a)
  • 相关阅读:
    Mysql 修改默认端口
    通过.pro文件生成C++工程
    内联函数知识点
    DICOM文件添加私有Tag(DCMTK Private Tag)
    poj 1185 炮兵阵地 状压dp
    cf #216 C
    cdoj1365 木杆上的蚂蚁
    cf #214 Dima and Salad
    cf #213 Matrix
    hdu 2222 Keywords Search(AC自动机入门题)
  • 原文地址:https://www.cnblogs.com/pyhan/p/13372297.html
Copyright © 2011-2022 走看看