使用Elasticsearch DSL的Python金字塔(檐口)

时间:2018-04-20 13:54:37

标签: python elasticsearch pyramid cornice

使用python pyramid和ElastiSearch。我查看pythonelasticsearch-dsl提供了一个很好的ORM,但我不确定如何将它与金字塔集成。

到目前为止,我已经建立了一个全球连接"根据pythonelasticsearch-dsl,通过属性将连接公开为金字塔的请求。

您认为此代码有什么问题吗?!

import tensorflow as tf

xx=(
        [178.72,218.38,171.1],
        [211.57,215.63,173.13],
        [196.25,196.69,116.91],
        [121.88,132.07,85.02],
        [117.04,135.44,112.54],
        [118.13,124.04,97.98],
        [116.73,125.88,99.04],
        [118.75,125.01,110.16],
        [109.69,111.72,69.07],
        [76.57,96.88,67.38],
        [91.69,128.43,87.57],
        [117.57,146.43,117.57]
      )

yy=(
        [212.09],
        [195.58],
        [127.6],
        [116.5],
        [117.95],
        [117.55],
        [117.55],
        [110.39],
        [74.33],
        [91.08],
        [121.75],
        [127.3]
       )


x=tf.placeholder(tf.float32,[None,3])
y=tf.placeholder(tf.float32,[None,1])
n1=5
n2=5
classes=12

def neuralnetwork(data):

    hl1={'weights':tf.Variable(tf.random_normal([3,n1])),'biases':tf.Variable(tf.random_normal([n1]))}   

    hl2={'weights':tf.Variable(tf.random_normal([n1,n2])),'biases':tf.Variable(tf.random_normal([n2]))}

    op={'weights':tf.Variable(tf.random_normal([n2,classes])),'biases':tf.Variable(tf.random_normal([classes]))}

    l1=tf.add(tf.matmul(data,hl1['weights']),hl1['biases'])
    l1=tf.nn.relu(l1)
    l2=tf.add(tf.matmul(l1,hl2['weights']),hl2['biases'])
    l2=tf.nn.relu(l2)
    output=tf.matmul(l2,op['weights'])+op['biases']
    return output

def train(x):
        pred=neuralnetwork(x)
       # cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=y))
        sq = tf.square(pred-y)
        loss=tf.reduce_mean(sq)

        optimizer = tf.train.GradientDescentOptimizer(0.5)
        train = optimizer.minimize(loss)

        #optimizer=tf.train.RMSPropOptimizer(0.01).minimize(cost)
        epochs=10



        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            for epoch in range(epochs):
               for i in range (int(1)):
                   batch_x=xx
                   batch_y=yy
                  # a=tf.shape(xx)
                   #print(sess.run(a))
                   i,c=sess.run(loss,feed_dict={x:batch_x, y: batch_y})
                   epoch_loss+=c
                   print("Epoch ",epoch," completed out of ",epochs, 'loss:', epoch_loss)

train(x)

我使用连接

from elasticsearch_dsl import connections   

def _create_es_connection(config):


    registry = config.registry
    settings = registry.settings

    es_servers = settings.get('elasticsearch.' + 'servers', ['localhost:9200'])
    es_timeout = settings.get('elasticsearch.' + 'timeout', 20)

    registry.es_connection = connections.create_connection(
        hosts=es_servers,
        timeout=es_timeout)

def get_es_connection(request):
    return getattr(request.registry, 'es_connection', 
                   connections.get_connection())

# main
def main(global_config, **settings):
     ...
     config = Configurator(settings=settings)

     config.add_request_method(
                               get_es_connection,
                               'es',
                               reify=True)

如果有任何其他方式我会感激任何指示 - 谢谢。

1 个答案:

答案 0 :(得分:0)

有些事情看起来很奇怪,但我猜它来自你项目的复制/粘贴(在设置中缺少类型转换,未定义连接等)。

您尝试做的与您使用SQLAlchemy的操作非常相似: https://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/database/sqlalchemy.html

但是根据pythonelasticsearch-dsl的文档,您甚至不必费心去做所有这些,因为lib允许您定义全局默认连接: https://elasticsearch-dsl.readthedocs.io/en/latest/configuration.html#default-connection