Hello.
When I run tensorflow on grpc, it fails. Is it a thread problem?
Or is it a problem with the tensroflow code?
Can I run the tensorflow code with grpc?
I used a very simple example of mnist tensroflow.
However, the following error occurs.
Tensor("Const:0", shape=(1,), dtype=int32) must be from the same graph as
Tensor("Cast:0", shape=(?,), dtype=float32).
---------mnist code------
# MNIST 데이터를 다운로드 한다.
from tensorflow.examples.tutorials.mnist import input_data
# TensorFlow 라이브러리를 추가한다.
import tensorflow as tf
class tensormnist(object):
def __init__(self):
self.mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
def set(self):
# 변수들을 설정한다.
self.x = tf.placeholder(tf.float32, [None, 784])
self.W = tf.Variable(tf.zeros([784, 10]))
self.b = tf.Variable(tf.zeros([10]))
self.y = tf.nn.softmax(tf.matmul(self.x, self.W) + self.b)
# cross-entropy 모델을 설정한다.
self.y_ = tf.placeholder(tf.float32, [None, 10])
self.cross_entropy = tf.reduce_mean(-tf.reduce_sum(self.y_ *
tf.log(self.y), reduction_indices=[1]))
self.train_step =
tf.train.GradientDescentOptimizer(0.5).minimize(self.cross_entropy)
# 경사하강법으로 모델을 학습한다.
self.init = tf.global_variables_initializer()
self.sess = tf.Session()
def teinmnist(self):
self.sess.run(self.init)
for i in range(1000):
batch_xs, batch_ys = self.mnist.train.next_batch(100)
self.sess.run(self.train_step, feed_dict={self.x: batch_xs, self.y_:
batch_ys})
def mnistrun(self):
print('self.sess,',self.sess)
correct_prediction = tf.equal(tf.argmax(self.y,1), tf.argmax(self.y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(self.sess.run(accuracy, feed_dict={self.x: self.mnist.test.images,
self.y_: self.mnist.test.labels}))
-------mnist code end-------------
---------server code-------
from concurrent import futures
import time
import os
import grpc
import helloworld_pb2
import helloworld_pb2_grpc
from mnist_test import tensormnist
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
class Greeter(helloworld_pb2_grpc.GreeterServicer):
def __init__(self):
self.tclass = tensormnist()
self.tclass.set()
self.tclass.teinmnist()
print('mnist model load...')
def SayHello(self, request, context):
print('[{0}]....SayHello request.name: {1}'.format(os.getpid(),
request.name))
try:
self.tclass.mnistrun()
except Exception as e:
print(e)
return helloworld_pb2.HelloReply(message='Hello %s %s' % (str(os.getpid()),
request.name))
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=24))
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port('0.0.0.0:5000')
server.start()
print("==== SERVER RUNNING =====")
try:
#pass
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__':
serve()
------------server code end---------
-------------client code -----------
from __future__ import print_function
import os
import grpc
import helloworld_pb2
import helloworld_pb2_grpc
def run():
print('client run')
channel = grpc.insecure_channel('localhost:5000')
stub = helloworld_pb2_grpc.GreeterStub(channel)
response = stub.SayHello(helloworld_pb2.HelloRequest(name=str(os.getpid())))
print("Greeter client received: " + response.message)
return response.message
if __name__ == '__main__':
run()
from __future__ import print_function
import os
import grpc
import helloworld_pb2
import helloworld_pb2_grpc
def run():
print('client run')
channel = grpc.insecure_channel('localhost:5000')
stub = helloworld_pb2_grpc.GreeterStub(channel)
response = stub.SayHello(helloworld_pb2.HelloRequest(name=str(os.getpid())))
print("Greeter client received: " + response.message)
return response.message
if __name__ == '__main__':
run()
--------------client code end--------------
Thnaks!
--
You received this message because you are subscribed to the Google Groups
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit
https://groups.google.com/d/msgid/grpc-io/031b168f-2694-43ae-ae07-6f1311f9ce67%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.