gpt4 book ai didi

python - logits 和 labels must be broadcastable error in Tensorflow RNN

转载 作者:太空宇宙 更新时间:2023-11-03 15:42:53 24 4
gpt4 key购买 nike

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

最佳答案

确保最终分类层中的标签数量等于数据集中的类别数量。 InvalidArgumentError(回溯见上):logits 和 labels must be broadcastable: 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")

关于python - logits 和 labels must be broadcastable error in Tensorflow RNN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51591550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com