Logit和标签必须是Tensorflow RNN中的可传播错误

2sbarzqh  于 2023-02-24  发布在  Git
关注(0)|答案(8)|浏览(194)

我是Tensorflow和深度学习的新手。我创建了RNN模型,用于从kaggle读取包含credit card fraud data的数据集,我正在尝试查看损失在10个时期内是如何减少的。我正在尝试将交易分类为欺诈(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)
k97glaaz

k97glaaz1#

确保最终分类图层中的标注数等于数据集中的类数。**InvalidArgumentError(请参见上文的追溯):日志和标签必须是可广播的:logits_size=[1,2] labels_size=[1,24]**如您的问题所示,这可能意味着您的最终分类图层中只有两个类,而实际上需要24个类。
在我的示例中,我的数据集中有7个类,但在最终分类图层中错误地使用了4个标注。

  • tf.keras.层。致密(4,激活=“softmax”)*

  • tf.角质层。致密(7,激活=“softmax”)*
0g0grzrc

0g0grzrc2#

当你说

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

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

jmo0nnb3

jmo0nnb33#

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

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

在我的例子中,我有30节课,更正了班级数就解决了这个问题

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

dwthyt8l4#

聚会有点晚了,但我在CNN上也犯了同样的错误,我摆弄了不同类型的交叉熵,这个错误是通过使用sparce_softmax_cross_entropy_with_logits()解决的。

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

q9yhzks05#

我在使用CNN时遇到了类似的问题,问题的发生是因为我在Generator函数中将labels数据类型更改为np.uint8,而在其余代码中对labels类型没有做任何操作。我通过在所有代码中将labels类型更改为uint8来解决问题。

r7s23pms

r7s23pms6#

我在使用批量较小的Tensorflow DataGenerator时遇到过这种情况。请尝试增加批量大小。

qyuhtwio

qyuhtwio7#

使用data.Shape检查第一个卷积层中的输入形状和馈入网络的输入形状。很有可能您的CNN馈入了不同的形状。

mkh04yzy

mkh04yzy8#

在我的情况下,我的训练数据集标签和我的验证数据集标签计数是不相等的。当我尝试使用标签的相同计数时,它工作。
Training data details should be like this
validation data details should be like this

相关问题