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

2024-09-19 20:48:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我将两个参数传递给tensorflow中的my(loss)函数,它(我认为)应该是占位符的形式,因为它们随着不同的步骤而变化。我在训练时喂它们吃。 我的节目大纲如下。 我的问题是,他们是否能有效地接受我提供给他们的价值观? 如果你能看看下面的片段,告诉我我做得对吗? 顺便说一下,我没有发现任何错误或什么。在

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

Tags: reducedisttffeedstepdictplaceholdersess
1条回答
网友
1楼 · 发布于 2024-09-19 20:48:00

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

另外,如果t1始终具有相同的形状,我建议在为tetha1_placeholder构造tf.placeholder()时传递该形状(类似于t2tetha2_placeholder)。在

相关问题 更多 >