Skip to content

Commit

Permalink
experiment graphql support
Browse files Browse the repository at this point in the history
  • Loading branch information
fliu2 committed Aug 22, 2017
1 parent 68c6be3 commit decd484
Show file tree
Hide file tree
Showing 25 changed files with 162 additions and 897 deletions.
6 changes: 2 additions & 4 deletions examples/mnistDemo.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import sys
sys.path.append('../')
import tensorlab
import yaml
from MnistDB import MnistDB
from tensorlab import Model
from tensorlab.TensorLayerApp import Model
from tensorlab import DBLogger


import seed

mndb=seed.mn
Expand Down
3 changes: 1 addition & 2 deletions examples/seed.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import sys
sys.path.append('../')
import tensorflow as tf
import tensorlayer as tl
import numpy as np

from tensorlab import *
from tensorlab.TensorDB import *

from MnistDB import MnistDB

Expand Down
31 changes: 31 additions & 0 deletions graphQLengine/DataLoader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import sys
sys.path.append('../')


from tensorlab.TensorDB import TensorDB
from tensorlab.TensorDB import mongo_impl
import yaml

config=yaml.load(open('loader.yml','r').read())
print config
dbc=config['TensorDB']['backend']
print dbc
del dbc['type']

m = mongo_impl.MongoFsDBImpl(**dbc)



db=TensorDB(studyID="DemoTensorLab",dbimpl=m,fsimpl=m)

result=db.TrainLogGraph(''' query {
trainHistory (epoch: 50,studyID: "run5")
{
acc,
stepTime
}
}'''
)

print result.data

63 changes: 39 additions & 24 deletions graphQLengine/graphQLtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

#schema

from DataLoader import db



class Log(graphene.Interface):
studyId=graphene.String()
accuracy=graphene.Float()
studyID=graphene.String()
epoch=graphene.Int()


Expand All @@ -18,27 +18,40 @@ class TrainLog(graphene.ObjectType):
class Meta:
interfaces = (Log,)

batchID=graphene.Int()
stepTime=graphene.Float()

def resolve_batchID(self,args,context,info):
print "batchID"
return 3
stepTime=graphene.List(graphene.Float)

acc=graphene.List(graphene.Float)


def mf(args):

print args['studyID']
t = TrainLog(studyID=args['studyID'],epoch=args['epoch'])
t.acc=args['acc']
t.stepTime=args['step_time']


return t



class LogQuery(graphene.ObjectType):
trainHistory = graphene.List(lambda:TrainLog, epoch=graphene.Int())
trainHistory = graphene.List(lambda:TrainLog, studyID=graphene.String(), epoch=graphene.Int(),)


def resolve_trainHistory(self, args, context, info):
print "resolve"

print "the element"
p = TrainLog(studyId="what", batchID=1, stepTime=1.0, accuracy=0.2)
p2 = TrainLog(studyId="what", batchID=1, stepTime=1.0, accuracy=0.2)
print args
print context

tdb=context['tdb']

return [p,p2]
print tdb
p=tdb.queryTrainLog(args)

return map(mf, p)



Expand All @@ -47,20 +60,22 @@ def resolve_trainHistory(self, args, context, info):
scheme=graphene.Schema(query=LogQuery)

print scheme
print db

results=scheme.execute('''
query {
trainHistory (epoch:9)
{
accuracy
}
}
'''
)


print results.data['trainHistory']
query {
trainHistory (epoch: 50,studyID: "run5")
{
acc,
stepTime
}
}
''', context_value={'tdb': db})


print results
print results.data['trainHistory'][0]['stepTime']




Expand Down
5 changes: 5 additions & 0 deletions graphQLengine/loader.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
TensorDB:
backend:
type: mongo
port: 27018
db_name: mnistTensorDB
94 changes: 0 additions & 94 deletions mytest.py
Original file line number Diff line number Diff line change
@@ -1,97 +1,3 @@
from tensorlab import *

code='''
import tensorflow as tf
import tensorlayer as tl
def build_network(name,reuse=False):
x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
y_ = tf.placeholder(tf.int32, shape=[None, ], name='y_')
with tf.variable_scope(name, reuse=reuse):
tl.layers.set_name_reuse(reuse)
network = tl.layers.InputLayer(x, name='input_layer')
network = tl.layers.DropoutLayer(network, keep=0.8, name='drop1')
network = tl.layers.DenseLayer(network, n_units=800,
act=tf.nn.relu, name='relu1')
network = tl.layers.DropoutLayer(network, keep=0.5, name='drop2')
network = tl.layers.DenseLayer(network, n_units=800,
act=tf.nn.relu, name='relu2')
network = tl.layers.DropoutLayer(network, keep=0.5, name='drop3')
network = tl.layers.DenseLayer(network, n_units=10,
act=tf.identity,
name='output_layer')
y = network.outputs
y_op = tf.argmax(tf.nn.softmax(y), 1)
# cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(y, y_))
cost = tl.cost.cross_entropy(y, y_, name='cost')
learning_rate = 0.0001
train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08,
use_locking=False).minimize(cost)
return x, y_op, y_, cost, train_op, network
network_fn=build_network
'''

if __name__ =="__main__":
print "hello"

mb = MnistDB(port=27018, db_name="mnistTest")

d,l=mb.find_data({'type':'train'})



g = gc_impl.gcpfsImpl()
gm=googlDBimpl()



m = mongo_impl.MongoFsDBImpl(ip='localhost',port=27018,db_name='mytestDB')

# p=TensorDB(studyID="hello",dbimpl=gm,fsimpl=g)
p2=TensorDB(studyID="hello2",dbimpl=gm,fsimpl=g)
p2.save_model_architecture(code,{'name':'mlp'})
c,f,fn=p2.load_model_architecture( {'name': 'mlp'})

print c
print f
print fn



n1=Model(fn,"hello",False)



#w,fid=p.find_one_params()
#print w
#print fid

#n1.Params=w

n1.fit(5,d,l,128,[DBLogger(p2,n1)],d[0:128],l[0:128])


#p.del_params({})
#p.del_train_log({})
#p.del_valid_log({})




Expand Down
3 changes: 3 additions & 0 deletions tensorlab/TensorDB/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from tensor_db import TensorDB
import mongo_impl

75 changes: 75 additions & 0 deletions tensorlab/TensorDB/graphSchema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import graphene










class Log(graphene.Interface):
studyID=graphene.String()
epoch=graphene.Int()


class Params(graphene.ObjectType):
class Meta:
interfaces = (Log,)

modelParams=graphene.String()

def resolve_modelParams(self,args,context,info):
pass









class TrainLog(graphene.ObjectType):
class Meta:
interfaces = (Log,)


stepTime=graphene.List(graphene.Float)

acc=graphene.List(graphene.Float)


def mf(args):

print args['studyID']
t = TrainLog(studyID=args['studyID'],epoch=args['epoch'])
t.acc=args['acc']
t.stepTime=args['step_time']


return t



class LogQuery(graphene.ObjectType):
trainHistory = graphene.List(lambda:TrainLog, studyID=graphene.String(), epoch=graphene.Int(),)


def resolve_trainHistory(self, args, context, info):
print "resolve"

print args
print context

tdb=context['tdb']

print tdb
p=tdb.queryTrainLog(args)

return map(mf, p)


gqlSchema = graphene.Schema(query=LogQuery)

File renamed without changes.
2 changes: 2 additions & 0 deletions tensorlab/TensorLayerApp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from Model import Model

9 changes: 4 additions & 5 deletions tensorlab/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from tensor_lab import *
import mongo_impl
from tensor_db import TensorDB

from Model import Model
from tensorlab.TensorDB import mongo_impl
from Logger import DBLogger

##from Model import Model
##from Logger import DBLogger


2 changes: 0 additions & 2 deletions tensorlab/gc_impl/__init__.py

This file was deleted.

Loading

0 comments on commit decd484

Please sign in to comment.