我是Tensorflow的新手,对我不感兴趣。我试图查看在我的RNN模型中(从包含credit card fraud data的kaggle中读取数据集所创建的RNN模型中,损耗如何减少了10多个纪元)。我正在尝试将交易分类为欺诈(1)而不是欺诈(0)。当我尝试运行以下代码时,我不断收到以下错误:
2018-07-30 14:59:33.237749:W tensorflow /核心/内核/queue_base.cc:277] _1_shuffle_batch / random_shuffle_queue:跳过未排队的取消入队尝试跟踪未关闭(最近一次通话最后一次):
文件 “ /home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”, _do_call中的第1322行 返回fn(* args)文件“ /home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”, _run_fn中的第1307行 选项,feed_dict,fetch_list,target_list,run_metadata)文件“ /home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”, _call_tf_sessionrun中的第1409行 run_metadata)tensorflow.python.framework.errors_impl.InvalidArgumentError:登录 并且标签必须是可广播的:logits_size = [1,2] labels_size = [1,24] [[节点:softmax_cross_entropy_with_logits_sg = SoftmaxCrossEntropyWithLogits [T = DT_FLOAT, _device =“ / job:localhost / replica:0 / task:0 / device:CPU:0”](添加,softmax_cross_entropy_with_logits_sg / Reshape_1)]]在处理上述异常期间,发生了另一个异常:
回溯(最近通话最近):文件 “ /home/suleka/Documents/untitled1/RNN_CrediCard.py”,第96行,在 train_neural_network(x)文件“ /home/suleka/Documents/untitled1/RNN_CrediCard.py”,第79行,在 train_neural_network _,c = sess.run([优化器,成本],feed_dict = {x:feature_batch,y:label_batch})文件 “ /home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”, 900行,运行中 run_metadata_ptr)文件“ /home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”, _run中的第1135行 feed_dict_tensor,选项,run_metadata)文件“ /home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”, _do_run中的第1316行 run_metadata)文件“ /home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”, 第1335行,在_do_call中 提高类型(e)(node_def,op,message)tensorflow.python.framework.errors_impl.InvalidArgumentError:logits 并且标签必须是可广播的:logits_size = [1,2] labels_size = [1,24] [[节点:softmax_cross_entropy_with_logits_sg = SoftmaxCrossEntropyWithLogits [T = DT_FLOAT, _device =“ / job:localhost / replica:0 / task:0 / device:CPU:0”](添加,softmax_cross_entropy_with_logits_sg / Reshape_1)]]
由op'softmax_cross_entropy_with_logits_sg'引起,定义于:
文件“ /home/suleka/Documents/untitled1/RNN_CrediCard.py”,第96行,在 train_neural_network(x)文件“ /home/suleka/Documents/untitled1/RNN_CrediCard.py”,第63行,在 train_neural_network 成本= tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits =预测, 标签= y))文件 “ /home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py”, 第250行,在new_func中 返回func(* args,** kwargs)文件“ /home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py”, 1968年的行,在softmax_cross_entropy_with_logits中 标签=标签,logits =登录,dim =暗淡,名称=名称)文件“ /home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py”, 第1879行,在softmax_cross_entropy_with_logits_v2中 precision_logits,标签,名称=名称)文件“ /home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_nn_ops.py”, 行7205,在softmax_cross_entropy_with_logits中 name = name)文件“ /home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py”, _apply_op_helper中的第787行 op_def = op_def)文件“ /home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py”, 第3414行,位于create_op中 op_def = op_def)文件“ /home/suleka/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py”, 第1740行,在 init 中 self._traceback = self._graph._extract_stack()#pylint:disable =受保护的访问InvalidArgumentError(请参阅上面的回溯):登录和标签必须 可以广播:logits_size = [1,2] labels_size = [1,24] [[节点: softmax_cross_entropy_with_logits_sg = SoftmaxCrossEntropyWithLogits [T = DT_FLOAT, _device =“ / job:localhost / replica:0 / task:0 / device:CPU:0”](添加,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)
答案 0 :(得分:3)
确保最终分类层中的标签数量等于您在数据集中的类别数量。 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")
答案 1 :(得分:2)
当你说
<div id="latest-blog-posts" class="carousel slide" data-ride="carousel">
<?php
$args = array(
'post_type' => 'post',
);
$the_query = new WP_Query ( $args );
?>
<ol class="carousel-indicators">
<!-- Start Carousel Indicator Loop -->
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li data-target="#latest-blog-posts" data-slide-to="<?php echo $the_query->current_post; ?>" class="<?php if ( $the_query->current_post == 0 ) : ?>active<?php endif; ?>">"></li>
<?php endwhile; endif; ?>
</ol>
<?php rewind_posts(); ?>
<div class="carousel-inner">
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'full', true );
$thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attatchment_image_alt', true );
?>
<div class="carousel-item <?php if ( $the_query->current_post == 0 ) : ?>active<?php endif; ?>">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail('thumb'); ?>
</a>
<?php endif; ?>
<h1>
<?php the_title(); ?>
</h1>
<?php the_excerpt(); ?>
</div>
<?php endwhile; endif; ?>
</div>
<a class="carousel-control-prev" href="#latest-blog-posts" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#latest-blog-posts" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
预测和标签的形状不兼容。您需要更改预测的计算方式,以使微型批次中的每个示例获得一个预测。
答案 2 :(得分:0)
在使用CNN时遇到了类似的问题,出现此问题的原因是我在Generator函数中将标签数据类型更改为np.uint8,而在其余代码中未对标签类型进行任何操作。我通过在所有代码中将标签类型更改为uint8来解决了这个问题。
答案 3 :(得分:0)
聚会晚了一点,但CNN出现了相同的错误,我弄乱了不同类型的交叉熵,并使用sparce_softmax_cross_entropy_with_logits()解决了该错误。
cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction, labels=y))
答案 4 :(得分:0)
这发生在我使用小批量的 Tensorflow DataGenerator 时。尝试增加批量大小。
答案 5 :(得分:0)
发生此错误的原因是预测类和输入的计数不匹配。我复制了一段代码,遇到了这个错误
这是输出为 5 个类的原始代码
model.add(Dense(5, activation = "softmax"))
在我的例子中,我有 30 个班级,修正班级数量就解决了
model.add(Dense(30, activation = "softmax"))