- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 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/
我想将模型及其各自训练的权重从 tensorflow.js 转换为标准 tensorflow,但无法弄清楚如何做到这一点,tensorflow.js 的文档对此没有任何说明 我有一个 manifest
我有一个运行良好的 TF 模型,它是用 Python 和 TFlearn 构建的。有没有办法在另一个系统上运行这个模型而不安装 Tensorflow?它已经经过预训练,所以我只需要通过它运行数据。 我
当执行 tensorflow_model_server 二进制文件时,它需要一个模型名称命令行参数,model_name。 如何在训练期间指定模型名称,以便在运行 tensorflow_model_s
我一直在 R 中使用标准包进行生存分析。我知道如何在 TensorFlow 中处理分类问题,例如逻辑回归,但我很难将其映射到生存分析问题。在某种程度上,您有两个输出向量而不是一个输出向量(time_t
Torch7 has a library for generating Gaussian Kernels在一个固定的支持。 Tensorflow 中有什么可比的吗?我看到 these distribu
在Keras中我们可以简单的添加回调,如下所示: self.model.fit(X_train,y_train,callbacks=[Custom_callback]) 回调在doc中定义,但我找不到
我正在寻找一种在 tensorflow 中有条件打印节点的方法,使用下面的示例代码行,其中每 10 个循环计数,它应该在控制台中打印一些东西。但这对我不起作用。谁能建议? 谢谢,哈米德雷萨, epsi
我想使用 tensorflow object detection API 创建我自己的 .tfrecord 文件,并将它们用于训练。该记录将是原始数据集的子集,因此模型将仅检测特定类别。我不明白也无法
我在 TensorFlow 中训练了一个聊天机器人,想保存模型以便使用 TensorFlow.js 将其部署到 Web。我有以下内容 checkpoint = "./chatbot_weights.c
我最近开始学习 Tensorflow,特别是我想使用卷积神经网络进行图像分类。我一直在看官方仓库中的android demo,特别是这个例子:https://github.com/tensorflow
我目前正在研究单图像超分辨率,并且我设法卡住了现有的检查点文件并将其转换为 tensorflow lite。但是,使用 .tflite 文件执行推理时,对一张图像进行上采样所需的时间至少是使用 .ck
我注意到 tensorflow 的 api 中已经有批量标准化函数。我不明白的一件事是如何更改训练和测试之间的程序? 批量归一化在测试和训练期间的作用不同。具体来说,在训练期间使用固定的均值和方差。
我创建了一个模型,该模型将 Mobilenet V2 应用于 Google colab 中的卷积基础层。然后我使用这个命令转换它: path_to_h5 = working_dir + '/Tenso
代码取自:- http://adventuresinmachinelearning.com/python-tensorflow-tutorial/ import tensorflow as tf fr
好了,所以我准备在Tensorflow中运行 tf.nn.softmax_cross_entropy_with_logits() 函数。 据我了解,“logit”应该是概率的张量,每个对应于某个像素的
tensorflow 服务构建依赖于大型 tensorflow ;但我已经成功构建了 tensorflow。所以我想用它。我做这些事情:我更改了 tensorflow 服务 WORKSPACE(org
Tensoflow 嵌入层 ( https://www.tensorflow.org/api_docs/python/tf/keras/layers/Embedding ) 易于使用, 并且有大量的文
我正在尝试使用非常大的数据集(比我的内存大得多)训练 Tensorflow 模型。 为了充分利用所有可用的训练数据,我正在考虑将它们分成几个小的“分片”,并一次在一个分片上进行训练。 经过一番研究,我
根据 Sutton 的书 - Reinforcement Learning: An Introduction,网络权重的更新方程为: 其中 et 是资格轨迹。 这类似于带有额外 et 的梯度下降更新。
如何根据条件选择执行图表的一部分? 我的网络有一部分只有在 feed_dict 中提供占位符值时才会执行.如果未提供该值,则采用备用路径。我该如何使用 tensorflow 来实现它? 以下是我的代码
我是一名优秀的程序员,十分优秀!