Tensorflow 汇总合并错误:形状 [-1,784] 具有负尺寸

2023-11-26

我试图总结下面神经网络的训练过程。

import tensorflow as tf 
import numpy as np 

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets(".\MNIST",one_hot=True)

# Create the model
def train_and_test(hidden1,hidden2, learning_rate, epochs, batch_size):

    with tf.name_scope("first_layer"):
        input_data = tf.placeholder(tf.float32, [batch_size, 784], name = "input")
        weights1  = tf.Variable(
        tf.random_normal(shape =[784, hidden1],stddev=0.1),name = "weights")
        bias = tf.Variable(tf.constant(0.0,shape =[hidden1]), name = "bias")
        activation = tf.nn.relu(
        tf.matmul(input_data, weights1) + bias, name = "relu_act")
        tf.summary.histogram("first_activation", activation)

    with tf.name_scope("second_layer"):
        weights2  = tf.Variable(
        tf.random_normal(shape =[hidden1, hidden2],stddev=0.1),
        name = "weights")
        bias2 = tf.Variable(tf.constant(0.0,shape =[hidden2]), name = "bias")
        activation2 = tf.nn.relu(
        tf.matmul(activation, weights2) + bias2, name = "relu_act")
        tf.summary.histogram("second_activation", activation2)

    with tf.name_scope("output_layer"):
        weights3 = tf.Variable(
            tf.random_normal(shape=[hidden2, 10],stddev=0.5), name = "weights")
        bias3 = tf.Variable(tf.constant(1.0, shape =[10]), name = "bias")
        output = tf.add(
        tf.matmul(activation2, weights3, name = "mul"), bias3, name = "output")
        tf.summary.histogram("output_activation", output)
    y_ = tf.placeholder(tf.float32, [batch_size, 10])

    with tf.name_scope("loss"):
        cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=output))
        tf.summary.scalar("cross_entropy", cross_entropy)
    with tf.name_scope("train"):
        train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)

    with tf.name_scope("tests"):
        correct_prediction = tf.equal(tf.argmax(output, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    summary_op = tf.summary.merge_all()

    sess = tf.InteractiveSession()
    writer = tf.summary.FileWriter("./data", sess.graph)
    tf.global_variables_initializer().run()

    # Train
    for i in range(epochs):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
         _, summary = sess.run([train_step,summary_op], feed_dict={input_data: batch_xs, y_: batch_ys})
     writer.add_summary(summary)

     if i % 10 ==0:
          test_xs, test_ys = mnist.train.next_batch(batch_size)
          test_accuracy = sess.run(accuracy, feed_dict = {input_data : test_xs, y_ : test_ys})
    writer.close()
    return test_accuracy

if __name__ =="__main__":
print(train_and_test(500, 200, 0.001, 10000, 100))

我每 10 个步骤都会使用一批随机测试数据来测试模型。 问题出在夏日作家身上。 for 循环内的 sess.run() 抛出以下错误。

    Traceback (most recent call last):

  File "<ipython-input-18-78c88c8e6471>", line 1, in <module>
    runfile('C:/Users/Suman 
Nepal/Documents/Projects/MNISTtensorflow/mnist.py', wdir='C:/Users/Suman 
Nepal/Documents/Projects/MNISTtensorflow')

  File "C:\Users\Suman Nepal\Anaconda3\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
execfile(filename, namespace)

  File "C:\Users\Suman Nepal\Anaconda3\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Suman Nepal/Documents/Projects/MNISTtensorflow/mnist.py", line 68, in <module>
    print(train_and_test(500, 200, 0.001, 100, 100))

  File "C:/Users/Suman Nepal/Documents/Projects/MNISTtensorflow/mnist.py", line 58, in train_and_test
    _, summary = sess.run([train_step,summary_op], feed_dict={input_data: batch_xs, y_: batch_ys})

  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 789, in run
    run_metadata_ptr)

  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 997, in _run
feed_dict_string, options, run_metadata)

  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1132, in _do_run
target_list, options, run_metadata)

  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1152, in _do_call
raise type(e)(node_def, op, message)

InvalidArgumentError: Shape [-1,784] has negative dimensions
 [[Node: first_layer_5/input = Placeholder[dtype=DT_FLOAT, shape=[?,784], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Caused by op 'first_layer_5/input', defined at:
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 231, in <module>
main()
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 227, in main
kernel.start()
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 477, in start
ioloop.IOLoop.instance().start()
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\tornado\ioloop.py", line 888, in start
handler_func(fd_obj, events)
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
self._handle_recv()
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
 File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
 File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 235, in dispatch_shell
handler(stream, idents, msg)
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 533, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2717, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2827, in run_ast_nodes
if self.run_code(code, result):
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-8-78c88c8e6471>", line 1, in <module>
runfile('C:/Users/Suman Nepal/Documents/Projects/MNISTtensorflow/mnist.py', wdir='C:/Users/Suman Nepal/Documents/Projects/MNISTtensorflow')
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
execfile(filename, namespace)
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
  File "C:/Users/Suman Nepal/Documents/Projects/MNISTtensorflow/mnist.py", line 86, in <module>
  File "C:/Users/Suman Nepal/Documents/Projects/MNISTtensorflow/mnist.py", line 12, in train_and_test
   input_data = tf.placeholder(tf.float32, [None, 784], name = "input")
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1530, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 1954, in _placeholder
name=name)
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
op_def=op_def)
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
  File "C:\Users\Suman Nepal\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1269, in __init__
self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Shape [-1,784] has negative dimensions
     [[Node: first_layer_5/input = Placeholder[dtype=DT_FLOAT, shape=[?,784], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

如果我删除了所有摘要编写者和摘要,则模型运行良好。 你能帮我找出这里的问题吗?我尝试操纵张量的形状但一无所获。


来自原始海报的已删除答案的一条评论:

我实际上在下面构建了一个神经网络with tf.Graph() as g。我删除了交互式会话并开始会话with tf.Session(g) as sess。它解决了问题。

图表g没有以这种方式标记为默认图,因此会话 (tf.InteractiveSession在原始代码中)将使用另一个图表来代替。

请注意,由于相同的错误消息,我偶然发现了这里。就我而言,我无意中遇到了这样的事情:

input_data = tf.placeholder(tf.float32, shape=(None, 50))
input_data = tf.tanh(input_data)
session.run(..., feed_dict={input_data: ...})

IE。我没有喂占位符。似乎其他一些张量运算可能会导致此令人困惑的错误,因为内部未定义的维度表示为 -1。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Tensorflow 汇总合并错误:形状 [-1,784] 具有负尺寸 的相关文章

  • 带有 s3 路径的张量板 logdir

    我看到tensorflow支持AWS s3文件系统 https github com tensorflow tensorflow tree master tensorflow core platform s3 https github co
  • 如何将one-hot向量转换为多标签?

    我有一项多分类任务 并且我得到了像这样的单热类型预测 0 1 1 0 1 0 1 0 1 我希望将这个单热向量转换为标签 例如 1 2 1 0 2 我已经尝试过 tf argmax 但它不起作用 那么我该如何处理呢 使用列表理解 oheLi
  • LSTM 批次与时间步

    我按照 TensorFlow RNN 教程创建了 LSTM 模型 然而 在这个过程中 我对 批次 和 时间步长 之间的差异 如果有的话 感到困惑 并且我希望得到帮助来澄清这个问题 教程代码 见下文 本质上是根据指定数量的步骤创建 批次 wi
  • 如何在 Keras 中将多个数据集与一个模型一起使用?

    我正在尝试使用 LSTM 网络通过 Keras 和 Tensorflow 进行外汇预测 我当然希望它能够在很多天的交易中进行训练 但要做到这一点 我必须给它提供具有大跳跃和无运动阶段的连续数据 当市场收盘时 这并不理想 因为它变得由于这些跳
  • 在不丢失基数信息的情况下对 TensorFlow 数据集进行窗口处理?

    tf data Dataset window返回一个新的数据集 其元素是数据集 这些嵌套数据集的元素是所需大小的窗口 如果您有一个数据集 例如 Dataset range 10 并想要一个像这样的窗口数据集 0 1 2 1 2 3 7 8
  • 无法将大小为 1665179 的数组重塑为形状 (512,512,3,3)

    该脚本用于进行检测 权重文件是 yolov4 coco 预训练模型 可以在这里找到 https drive google com file d 1cewMfusmPjYWbrnuJRuKhPMwRe b9PaT view https dri
  • 错误:tensorflow:无法匹配检查点的文件

    我正在训练一个张量流模型 在每个时期之后我都会保存模型状态并腌制一些数组 到目前为止 我的模型执行了 2 个纪元 并且保存状态的文件夹包含以下文件 checkpoint model e knihy preprocessed txt e0 c
  • 如何在google colab中降级到tensorflow-gpu版本1.12

    我正在运行一个仅与旧版本的tensorflow GPU兼容的GAN 因此我需要将google colab中的tensorflow gpu从1 15降级到1 12 我尝试使用本中建议的以下命令thread https stackoverflo
  • 张量流如何处理无法存储在一个盒子中的大变量

    我想通过训练超过十亿特征维度的数据来训练 DNN 模型 因此第一层权重矩阵的形状将为 1 000 000 000 512 这个权重矩阵太大 无法存储在一个盒子中 目前有没有什么解决方案来处理这么大的变量 例如将大的权重矩阵划分为多个框 Up
  • Tensorflow中通过字符串选择不同的模式

    我正在尝试构建一个 VAE 网络 我希望模型在不同的模式下做不同的事情 我有三种模式 训练 相同 和 不同 以及一个名为 interpolation mode 的函数 它根据模式执行不同的操作 我的代码如下所示 import tensorf
  • GradientTape 根据损失函数是否被 tf.function 修饰给出不同的梯度

    我发现计算的梯度取决于 tf function 装饰器的相互作用 如下所示 首先 我为二元分类创建一些合成数据 tf random set seed 42 np random seed 42 x tf random normal 2 1 y
  • 无法获取未知等级的 Shape 长度

    我有一个神经网络 来自tf data数据生成器和tf keras模型 如下 简化版本 因为太长 dataset A tf data Dataset反对与next x方法调用get next为了x train迭代器和next y方法调用get
  • 如何将 tf.contrib.seq2seq.Helper 用于非嵌入数据?

    我正在尝试使用 tf contrib seq2seq 模块对某些数据 仅 float32 向量 进行预测 但我使用 TensorFlow 中的 seq2seq 模块找到的所有示例都用于翻译 因此用于嵌入 我正在努力准确理解 tf contr
  • Tensorflow 中的图像叠加图像卷积

    假设我有两组图像 A 和 B 每个图像都是 11X5x5x3 其中 11 是示例数量 5x5x3 是图像尺寸 Tensorflow 中是否有一种简单的方法可以对 A i 中的每个图像应用 B i 上的卷积 即 B i 扮演过滤器角色 A i
  • 如何将张量流模型部署到azure ml工作台

    我在用Azure ML Workbench执行二元分类 到目前为止 一切正常 我有很好的准确性 我想将模型部署为用于推理的 Web 服务 我真的不知道从哪里开始 azure 提供了这个doc https learn microsoft co
  • 张量流中的复杂卷积

    我正在尝试运行一个简单的卷积 但包含复数 r np random random 1 10 10 10 i np random random 1 10 10 10 x tf complex r i conv layer tf layers c
  • Keras model.predict 函数给出输入形状错误

    我已经在 Tensorflow 中实现了通用句子编码器 现在我正在尝试预测句子的类概率 我也将字符串转换为数组 Code if model model type universal classifier basic class probs
  • 使用预训练的 word2vec 初始化 Seq2seq 嵌入

    我对使用预训练的 word2vec 初始化tensorflow seq2seq 实现感兴趣 我已经看过代码了 嵌入似乎已初始化 with tf variable scope scope or embedding attention deco
  • 错误:分配具有形状的张量时出现 OOM

    在使用 Apache JMeter 进行性能测试期间 我面临着初始模型的问题 错误 分配形状为 800 1280 3 和类型的张量时出现 OOM 通过分配器浮动在 job localhost replica 0 task 0 device
  • TensorFlow的./configure在哪里以及如何启用GPU支持?

    在我的 Ubuntu 上安装 TensorFlow 时 我想将 GPU 与 CUDA 结合使用 但我却停在了这一步官方教程 http www tensorflow org get started os setup md 这到底是哪里 con

随机推荐