ShannonAI / service-streamer

Boosting your Web Services of Deep Learning Applications.
Apache License 2.0
1.23k stars 187 forks source link

使用的keras_bert,出现错误 #20

Closed FirstDiscoverer closed 5 years ago

FirstDiscoverer commented 5 years ago

ValueError: Tensor Tensor("dense_1/Softmax:0", shape=(?, 4), dtype=float32) is not an element of this graph.

参考这个链接 https://www.cnblogs.com/qhlblog/p/9233621.html 应该是tensorflow在做多线程的时候需要制定graph

_BaseStreamWorker.model_predict的使用应该制定 with graph.as_default():

Meteorix commented 5 years ago

那你可以使用ManagedModelpredict函数里面使用with graph.as_default():

FirstDiscoverer commented 5 years ago

是这么操作了 还有会timeOut,原来的50ms太小了

那你可以使用ManagedModelpredict函数里面使用with graph.as_default():

是这么操作了 还有会timeOut,原来的50ms太小了

Meteorix commented 5 years ago

timeout可能是worker初始化比较慢,超过了streamer的超时时间,可以用这个api试试https://github.com/ShannonAI/service-streamer/blob/19a2bad2cc54b20fc96f7e3093d2e785b19345fb/example/future_example.py#L18

FirstDiscoverer commented 5 years ago

streamer = ThreadedStreamer(model.predict, batch_size=max_batch, max_latency=0.1)

streamer = Streamer(ManagedBertModel, batch_size=batch_size, max_latency=0.1, worker_num=4, cuda_devices=(0, 1, 2, 3))

ThreadedStreamer、 Streamer有啥区别?

建议graph可以通过接口传递进去,最后如果graph不为空就加上with graph.as_default():,否则为原来的的predict

Meteorix commented 5 years ago

ThreadedStreamer是用子线程作为worker;Streamer是会开新的进程,可以支持多worker。

streamer不希望知道框架(tf/pytorch),所以不太好加graph参数

FirstDiscoverer commented 5 years ago

好的 感谢老哥

fushengwuyu commented 4 years ago

好的 感谢老哥 这个问题,你解决了吗,我也遇到同样的问题,请问一下你的graph怎么加的。

FirstDiscoverer commented 4 years ago

贴一下解决方法,web我使用的tornado框架

注意在main方法中初始化streamer

if name == "main": streamer = Streamer(TextCnnModel, batch_size=global_config.batch_size, max_latency=global_config.max_latency, cuda_devices=global_config.cuda_device, worker_num=1)

然后再controller的方法中 data = streamer.predict([news])[0]调用就行了,注意不要在非main外边初始化就好了