简体   繁体   English

如何在训练/评估期间访问Tensorflow中标签的值?

[英]How do I access the value of a label in Tensorflow during training/eval?

I'm working on a neural network in Tensorflow with a custom loss function, which based on the label of the input image I sample a vector (from another data set corresponding to the label) and take the dot product of the input image's embedding (the softmax preactivation) and the sampled vector. 我正在使用Tensorflow中的神经网络,使用自定义丢失函数,该函数基于输入图像的标签,我采样矢量(来自与标签相对应的另一个数据集)并获取输入图像嵌入的点积( softmax预激活)和采样矢量。 I also negatively sample a mismatch vector that conflicts with the input label, along with another random training input image embedding whose label differs from the current input label. 我还负面地采样与输入标签冲突的不匹配矢量,以及其标签与当前输入标签不同的另一个随机训练输入图像嵌入。 Obviously these are all constructed as same-dimensional tensors, and the custom loss is: 显然这些都被构造为同维张量,并且自定义损失是:

Loss = max(0, input_embedding * mismatching_sample - input_embedding * matching_sample + 1) \ 
+ max(0, random_embedding * matching_sample - input_embedding * matching_sample + 1)

That was mainly for background, but the issue I'm having is how to access the values of labels corresponding to the input images? 这主要是为了背景,但我遇到的问题是如何访问与输入图像对应的标签值? I need to be able to access these labels' values in order to sample the correct vectors and compute my loss. 我需要能够访问这些标签的值,以便采样正确的向量并计算我的损失。

I read in documentation that you can use .eval() to get the value of a tensor by running in a session, but when I tried this my terminal just hung... Technically I'm already running a session when I'm training my neural network, so not sure if there is an issue with running a second session inside another session and trying to evaluate values that are technically part of the other running session. 我在文档中读到你可以使用.eval()来通过在会话中运行来获取张量的值,但是当我尝试这个时我的终端只是挂了...技术上我在训练时已经在运行一个会话我的神经网络,所以不确定是否存在在另一个会话中运行第二个会话并尝试评估技术上属于另一个正在运行的会话的值的问题。 Anyway, I'm completely out of ideas as to how I can make this work. 无论如何,我完全没有关于如何使这项工作的想法。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Here's my original attempt that proved problematic: 这是我原来的尝试证明有问题:

# compute custom loss function using tensors and tensor operations
def compute_loss(e_list, labels, i):
    embedding = e_list[i] #getting the current embedding tensor
    label = labels[i] #getting the matching label tensor, this is value I need.
    y_index = np.nonzero(label)[0][0] #this always returns 0, doesn't work :(
    target = get_mnist_embedding(y_index)
    wrong_mnist = get_mismatch_mnist_embedding(y_index)
    wrong_spec = get_random_spec_embedding(y_index, e_list, labels)
    # compute the loss:
    zero = tf.constant(0,dtype="float32")
    one = tf.constant(1,dtype="float32")
    mul1 = tf.mul(wrong_mnist,embedding)
    dot1 = tf.reduce_sum(mul1)
    mul2 = tf.mul(target,embedding)
    dot2 = tf.reduce_sum(mul2)
    mul3 = tf.mul(target,wrong_spec)
    dot3 = tf.reduce_sum(mul3)
    max1 = tf.maximum(zero, tf.add_n([dot1, tf.negative(dot2), one]))
    max2 = tf.maximum(zero, tf.add_n([dot3, tf.negative(dot2), one]))
    loss = tf.add(max1,max2)
    return loss

The issue was I was not using feed_dict to pass in the sampled values. 问题是我没有使用feed_dict传递采样值。 I was trying to identify the value of the tensor for the input's label at the time of loss computation, in order to then sample the other values needed for my loss function. 我试图在损失计算时识别输入标签的张量值,以便然后采样我的损失函数所需的其他值。

I have worked around this issue by precomputing and organizing the mismatching_sample and matching_sample values for example, using a tf.placeholder for these values initially, and passing in the values using feed_dict dictionary object when I execute sess.run([train_op, loss...], feed_dict=feed_dict) to access the values for my loss calculation. 我通过预先计算和组织mismatching_samplematching_sample值来解决这个问题,例如,最初使用tf.placeholder来表示这些值,并在执行sess.run([train_op, loss...], feed_dict=feed_dict)时使用feed_dict字典对象传入值sess.run([train_op, loss...], feed_dict=feed_dict)访问我的损失计算值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果字典值包含在 eval 中,如何访问带有键变量的字典值? - How do I access a dictionary value, with a variable for the key, if it is wrapped in eval? 如何在 Tensorflow 1.x 中的训练期间打印值 - How to print value during Training in Tensorflow 1.x 我如何训练我的DNNClassifier模型(在tensorflow中),以从新的训练案例中学习? 我无权访问初始CSV文件 - How do I train my DNNClassifier model (in tensorflow), to learn from new training cases? I do not have access to the initial CSV file 如何在 Tensorflow 训练期间打印梯度? - How to print the gradients during training in Tensorflow? 在训练过程中如何在Tensorflow Python中打印训练损失 - How to print training loss in tensorflow python during training process 如何在 Tensorflow 2 LSTM 训练中屏蔽多输出? - How do I mask multi-output in Tensorflow 2 LSTM training? 如何在TensorFlow中使用分布式DNN培训? - How do I use distributed DNN training in TensorFlow? 如何生成自定义 tensorflow 训练/验证数据, - How do I generate custom tensorflow training / validation data, 在 Keras 训练期间如何在损失函数内打印? - How do I print inside the loss function during training in Keras? 如何保存我的 model 在训练期间创建的嵌入? - How do I save the embeddings my model creates during training?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM