在 Tensorflow RNN 中,logits 和标签必须是可广播的错误

Posted

技术标签:

【中文标题】在 Tensorflow RNN 中,logits 和标签必须是可广播的错误【英文标题】:logits and labels must be broadcastable error in Tensorflow RNN 【发布时间】:2019-01-06 13:28:33 【问题描述】:

我是 Tensorflow 和深度学习的新手。我试图在我创建的 RNN 模型中查看损失如何在 10 个时期内减少,该模型是为了从 kaggle 中读取包含 credit card fraud data 的数据集。我试图将交易归类为欺诈(1)而不是欺诈(0)。当我尝试运行以下代码时,我不断收到以下错误:

> 2018-07-30 14:59:33.237749: W
> tensorflow/core/kernels/queue_base.cc:277]
> _1_shuffle_batch/random_shuffle_queue: Skipping cancelled enqueue attempt with queue not closed Traceback (most recent call last):  
> File
> "/home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py",
> line 1322, in _do_call
>     return fn(*args)   File "/home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py",
> line 1307, in _run_fn
>     options, feed_dict, fetch_list, target_list, run_metadata)   File "/home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py",
> line 1409, in _call_tf_sessionrun
>     run_metadata) tensorflow.python.framework.errors_impl.InvalidArgumentError: logits
> and labels must be broadcastable: logits_size=[1,2] labels_size=[1,24]
>    [[Node: softmax_cross_entropy_with_logits_sg =
> SoftmaxCrossEntropyWithLogits[T=DT_FLOAT,
> _device="/job:localhost/replica:0/task:0/device:CPU:0"](add, softmax_cross_entropy_with_logits_sg/Reshape_1)]]
> 
> During handling of the above exception, another exception occurred:
> 
> Traceback (most recent call last):   File
> "/home/suleka/Documents/untitled1/RNN_CrediCard.py", line 96, in
> <module>
>     train_neural_network(x)   File "/home/suleka/Documents/untitled1/RNN_CrediCard.py", line 79, in
> train_neural_network
>     _, c = sess.run([optimizer, cost], feed_dict=x: feature_batch, y: label_batch)   File
> "/home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py",
> line 900, in run
>     run_metadata_ptr)   File "/home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py",
> line 1135, in _run
>     feed_dict_tensor, options, run_metadata)   File "/home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py",
> line 1316, in _do_run
>     run_metadata)   File "/home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py",
> line 1335, in _do_call
>     raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.InvalidArgumentError: logits
> and labels must be broadcastable: logits_size=[1,2] labels_size=[1,24]
>    [[Node: softmax_cross_entropy_with_logits_sg =
> SoftmaxCrossEntropyWithLogits[T=DT_FLOAT,
> _device="/job:localhost/replica:0/task:0/device:CPU:0"](add, softmax_cross_entropy_with_logits_sg/Reshape_1)]]
> 
> Caused by op 'softmax_cross_entropy_with_logits_sg', defined at:  
> File "/home/suleka/Documents/untitled1/RNN_CrediCard.py", line 96, in
> <module>
>     train_neural_network(x)   File "/home/suleka/Documents/untitled1/RNN_CrediCard.py", line 63, in
> train_neural_network
>     cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,
> labels=y))   File
> "/home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py",
> line 250, in new_func
>     return func(*args, **kwargs)   File "/home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py",
> line 1968, in softmax_cross_entropy_with_logits
>     labels=labels, logits=logits, dim=dim, name=name)   File "/home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py",
> line 1879, in softmax_cross_entropy_with_logits_v2
>     precise_logits, labels, name=name)   File "/home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_nn_ops.py",
> line 7205, in softmax_cross_entropy_with_logits
>     name=name)   File "/home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py",
> line 787, in _apply_op_helper
>     op_def=op_def)   File "/home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py",
> line 3414, in create_op
>     op_def=op_def)   File "/home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py",
> line 1740, in __init__
>     self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access
> 
> InvalidArgumentError (see above for traceback): logits and labels must
> be broadcastable: logits_size=[1,2] labels_size=[1,24]     [[Node:
> softmax_cross_entropy_with_logits_sg =
> SoftmaxCrossEntropyWithLogits[T=DT_FLOAT,
> _device="/job:localhost/replica:0/task:0/device:CPU:0"](add, softmax_cross_entropy_with_logits_sg/Reshape_1)]]

谁能指出我在我的代码中做错了什么,如果可能的话,还有我的代码中的任何问题。提前谢谢你。

下面是我的代码:

import tensorflow as tf
from tensorflow.contrib import rnn



# cycles of feed forward and backprop
hm_epochs = 10
n_classes = 2
rnn_size = 128
col_size = 30
batch_size = 24
try_epochs = 1
fileName = "creditcard.csv"

def create_file_reader_ops(filename_queue):
    reader = tf.TextLineReader(skip_header_lines=1)
    _, csv_row = reader.read(filename_queue)
    record_defaults = [[1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1]]
    col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col15, col16, col17, col18, col19, col20, col21, col22, col23, col24, col25, col26, col27, col28, col29, col30, col31 = tf.decode_csv(csv_row, record_defaults=record_defaults)
    features = tf.stack([col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col15, col16, col17, col18, col19, col20, col21, col22, col23, col24, col25, col26, col27, col28, col29, col30])
    return features, col31


def input_pipeline(fName, batch_size, num_epochs=None):
    # this refers to multiple files, not line items within files
    filename_queue = tf.train.string_input_producer([fName], shuffle=True, num_epochs=num_epochs)
    features, label = create_file_reader_ops(filename_queue)
    min_after_dequeue = 10000 # min of where to start loading into memory
    capacity = min_after_dequeue + 3 * batch_size # max of how much to load into memory
    # this packs the above lines into a batch of size you specify:
    feature_batch, label_batch = tf.train.shuffle_batch(
        [features, label],
        batch_size=batch_size,
        capacity=capacity,
        min_after_dequeue=min_after_dequeue)
    return feature_batch, label_batch


creditCard_data, creditCard_label = input_pipeline(fileName, batch_size, try_epochs)


x = tf.placeholder('float',[None,col_size])
y = tf.placeholder('float')


def recurrent_neural_network_model(x):
    #giving the weights and biases random values
    layer = 'weights': tf.Variable(tf.random_normal([rnn_size, n_classes])),
            'bias': tf.Variable(tf.random_normal([n_classes]))

    x = tf.split(x, 24, 0)
    print(x)

    lstm_cell = rnn.BasicLSTMCell(rnn_size)
    outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32 )
    output = tf.matmul(outputs[-1], layer['weights']) + layer['bias']

    return output

def train_neural_network(x):
    prediction = recurrent_neural_network_model(x)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
    optimizer = tf.train.AdamOptimizer().minimize(cost)


    with tf.Session() as sess:

        gInit = tf.global_variables_initializer().run()
        lInit = tf.local_variables_initializer().run()
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        for epoch in range(hm_epochs):
            epoch_loss = 0

            for counter in range(101):
                    feature_batch, label_batch = sess.run([creditCard_data, creditCard_label])
                    print(label_batch.shape)
                    _, c = sess.run([optimizer, cost], feed_dict=x: feature_batch, y: label_batch)
                    epoch_loss += c
            print('Epoch', epoch, 'compleated out of', hm_epochs, 'loss:', epoch_loss)



train_neural_network(x)

【问题讨论】:

【参考方案1】:

当你说

    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))

预测和标签的形状不兼容。您需要更改预测的计算方式,以便在您的 minibatch 中为每个示例获取一个。

【讨论】:

是的,这也是我的代码中的问题之一,而且我还犯了几个错误。我一直在尝试使用 softmax_cross_entropy_with_logits,因为它是一个二进制分类,我应该使用 sigmoid_cross_entropy_with_logits 并将 n_classes 变量设置为 1。 嗨,请告诉我您是如何设法计算给定小批量的每个示例的预测的。我面临与您相同的错误。 logits 和标签必须是可广播的:logits_size=[32,31] labels_size=[32,199]。【参考方案2】:

确保最终分类层中的标签数量等于数据集中的类别数量。 InvalidArgumentError(有关回溯,请参见上文):logits 和标签必须是可广播的:logits_size=[1,2] labels_size=[1,24] 如您的问题所示,可能表明您只有两个类在您的最终分类层中,而您实际上需要 24 个。

就我而言,我的数据集中有 7 个类,但我在最终分类层中错误地使用了 4 个标签。因此,我不得不从

tf.keras.layers.Dense(4, activation="softmax")

tf.keras.layers.Dense(7, activation="softmax")

【讨论】:

【参考方案3】:

聚会有点晚了,但我在使用 CNN 时遇到了同样的错误,我弄乱了不同类型的交叉熵,并通过使用 sparce_softmax_cross_entropy_with_logits() 解决了错误。

cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction, labels=y))

【讨论】:

【参考方案4】:

我在使用 CNN 时遇到了类似的问题,出现问题是因为我在 Generator 函数中将标签数据类型更改为 np.uint8,而在其余代码中没有对标签类型执行任何操作。我通过将所有代码中的标签类型更改为 uint8 解决了这个问题。

【讨论】:

【参考方案5】:

我在使用小批量的 Tensorflow DataGenerator 时发生了这种情况。尝试增加批量大小。

【讨论】:

【参考方案6】:

发生此错误是因为预测类的计数与输入不匹配。我复制了一段代码,遇到了这个错误

这是输出为 5 个类的原始代码

model.add(Dense(5, activation = "softmax"))

在我的例子中,我有 30 个班级,并且修正班级数修复了它

model.add(Dense(30, activation = "softmax"))

【讨论】:

以上是关于在 Tensorflow RNN 中,logits 和标签必须是可广播的错误的主要内容,如果未能解决你的问题,请参考以下文章

如何将内置 tensorflow 的 logits 自定义损失函数转换为 keras?

为啥 TensorFlow 的文档将 softmax 的输入称为“logits”?

TensorFlow 的 sparse_softmax_cross_entropy 中的 Logits 表示

tensorflow中四种不同交叉熵函数tf.nn.softmax_cross_entropy_with_logits()

Tensorflow 卷积网络错误:无效参数:logits 和标签必须相同大小:logits_size=[512,4] labels_size=[128,4]

Tensorflow:logits 和标签必须具有相同的第一维