grpc mnist with tensroflow "Tensor("Const:0", shape=(1,), dtype=int32) must be from the same graph"

219 views
Skip to first unread message

he...@snaps.com

unread,
Jan 11, 2018, 4:23:10 AM1/11/18
to grpc.io
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!

Nathaniel Manista

unread,
Jan 20, 2018, 12:05:28 PM1/20/18
to he...@snaps.com, grpc.io
On Thu, Jan 11, 2018 at 1:23 AM, <he...@snaps.com> wrote:
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).

From the error this looks like it has everything to do with TensorFlow and nothing to do with gRPC. I'd redirect your message to their user support mailing list, but it looks like they route all their support ("help forum") traffic to Stack Overflow. So try asking on Stack Overflow?
-Nathaniel
Reply all
Reply to author
Forward
0 new messages