首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将tensorflow占位符作为函数参数传递

将tensorflow占位符作为函数参数传递
EN

Stack Overflow用户
提问于 2017-04-08 11:48:12
回答 1查看 1.3K关注 0票数 0

我将两个参数传递给tensorflow中的my (loss)函数,它(我认为)应该是占位符的形式,因为它们在不同的步骤中会发生变化。我在训练中给他们喂食。我的程序提纲如下。我的问题是,他们是否有效地接受了我灌输给他们的价值观?如果你能看一下下面的片段,告诉我我做得对,我将不胜感激。顺便说一句,我没有得到任何错误或其他东西。

代码语言:javascript
复制
tetha1_placeholder, tetha2_placeholder = tf.placeholder(tf.float32, name='tetha1plh'), tf.placeholder(tf.float32, name='tetha2plh')
hyperparams = {'tetha1': tetha1_placeholder,'tetha2':tetha2_placeholder} 
[getting embeddings1,embeddings2, embeddings3 from my model]
loss = loss_function (embeddings1,embeddings1,embeddings3, hyperparams) 

with sess.as_default():

   while true:
        step = sess.run(global_step, feed_dict=None)
        t1, t2 = calculate_params(step)
        feed_dict = {tetha1_placeholder:t1, tetha2_placeholder:t2}            
        error=sess.run([loss], feed_dict=feed_dict)

def loss_function (embeddings1,embeddings2,embeddings3, hyperparams):
       pos_dist =hyperparams['tetha1'] * tf.reduce_sum(tf.square(tf.subtract(embeddings1, embeddings2)), 1)
       neg_dist = hyperparams['tetha2'] *tf.reduce_sum(tf.square(tf.subtract(embeddings1, embeddings3)), 1)
       loss = tf.reduce_mean(tf.add(pos_dist,neg_dist))
       return loss
EN

回答 1

Stack Overflow用户

发布于 2017-04-08 12:53:42

程序看起来是正确的。当您调用sess.run([loss], feed_dict=feed_dict)时,loss_function()中的张量hyperparams['tetha1']将具有值t1,而loss_function()中的张量hyperparams['tetha2']将具有值t2

另外,如果t1总是具有相同的形状,我建议您在为tetha1_placeholder构造tf.placeholder()时传递该形状(对于t2tetha2_placeholder也是如此)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43289967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档