TensorFlow (人工智能)

基本用法

  • 构建图

构建图的第一步是创建原算子。源算子不需要任何输入,例如常量。源算子的输出传递给其他算子做运算
Python库中,算子构造器的返回值代表被构造出的算子的输出,这些返回值可以传递给其他算子作为输入

  1. 简单实用

     import tensorflow as tf
     #构造器返回值代表常量算子的返回值
     matrix1 = tf.constant([[3., 3.]])
     #创建另外一个常量算子
     matrix2 = tf.constant([[2.],[2.]])
     #进行矩阵运算
     product = tf.matmul(matrix1,matrix2)
     #启动默认图
     sess = tf.Session()
     #触发图中三个算子的执行
     res = sess.run(product)
     print(res)
     #任务完成关闭会话
     sess.close()
    
  2. 会话关闭方式

     with tf.Session() as sess:
     	res = sess.run([product])
    
  3. 实用多个cpu或GPU等

     with tf.Session() as sess:
     	with tf.device("/gpu:1"):
     		....
    

    设备用字符串进行标识。目前支持的设备包括
    a. “/cpu:0”:机器的CPU
    b. “/gpu:0”:机器的第一个GPU
    c. “/gpu:1”:机器的第二个GPU

猜你喜欢

转载自blog.csdn.net/weixin_42150936/article/details/88524864
今日推荐