人工智能从入门到精通(17)
来源:互联网 发布:云平台网络架构 编辑:程序博客网 时间:2024/06/06 00:24
tensorflow多线程基础:
1.队列的使用
import tensorflow as tfq=tf.FIFOQueue(2,"int32")init=q.enqueue_many(([0,10],))x=q.dequeue()#第一个元素出队列y=x+1q_inc=q.enqueue([y])with tf.Session() as sess: init.run() for _ in range(5): v,_=sess.run([x,q_inc]) print(v)
tf.Coordinator协调多个线程一起停止
import tensorflow as tfimport numpy as npimport threadingimport timedef MyLoop(coord,worker_id): while not coord.should_stop(): if np.random.rand()<0.1: print("stoping from id:%d\n"% worker_id) coord.request_stop() else: print("working on id:%d\n"% worker_id) time.sleep(1)coord=tf.train.Coordinator()threads=[threading.Thread(target=MyLoop,args=(coord,i,))for i in range(5)]for t in threads:t.start()coord.join(threads)
tf.QueueRunner启动多个线程操作同一个队列
import tensorflow as tfqueue=tf.FIFOQueue(100,"float")enqueue_op=queue.enqueue([tf.random_normal([1])])qr=tf.train.QueueRunner(queue,[enqueue_op]*5)tf.train.add_queue_runner(qr)out_tensor=queue.dequeue()with tf.Session() as sess: coord=tf.train.Coordinator() threads=tf.train.start_queue_runners(sess=sess,coord=coord) for _ in range(3):print(sess.run(out_tensor)[0]) coord.request_stop() coord.join(threads)
阅读全文
0 0
- 人工智能从入门到精通(17)
- 人工智能从入门到精通(1)
- 人工智能从入门到精通(2)
- 人工智能从入门到精通(3)
- 人工智能从入门到精通(4)
- 人工智能从入门到精通(5)
- 人工智能从入门到精通(6)
- 人工智能从入门到精通(9)
- 人工智能从入门到精通(10)
- 人工智能从入门到精通(12)
- 人工智能从入门到精通(13)
- 人工智能从入门到精通(14)
- 人工智能从入门到精通(16)
- 人工智能从入门到精通(18)
- 人工智能从入门到精通(7)-mnist准备
- 人工智能从入门到精通(8)-mnist实现
- 人工智能从入门到精通(11)-基本概念
- 人工智能从入门到精通(15)-卷积网络在数字识别的应用
- HashMap和HashTable的区别,HashTable和ConcurrentHashMap的区别
- matlab工具箱下载
- Windows下VS2015编译64位Boost1.64
- 对抗样本机器学习_Note1
- C语言实现排序算法---桶(箱)排序
- 人工智能从入门到精通(17)
- XX区别
- jdbc事务封装之DBCPUtil和ThreadLocal
- ACdream 1020
- Kaggle_Titanic生存预测 -- 详细流程吐血梳理
- 单例模式常用三种写法
- java作业
- java、数组
- 前端项目架构小结