【问题标题】:Can anyone explain my error in tensorflow?谁能解释我在张量流中的错误?
【发布时间】:2019-12-23 21:17:59
【问题描述】:

在尝试训练我的模型时,我在 tensorflow 中遇到输入形状错误。我已经检查了输入形状是否匹配,但我仍然收到错误,任何帮助将不胜感激。 X 的形状是 (1, 7),y 的形状是 (1, 2)。 我有这个代码:

import tensorflow as tf
import keras
import numpy as np
import json

with open("situations.json") as f:
    data = json.load(f)

X = np.array([i[0] for i in data])
y = np.array([i[1] for i in data])
print(X)
print(y)

model = keras.Sequential([
    keras.layers.InputLayer(input_shape=(7,)),
    keras.layers.Dense(7),
    keras.layers.Dense(2)
])
model.compile(optimizer="adam", loss="mean_squared_error")
model.fit(X, y, epochs=100)

文件“situations.json”中有这个,我尝试使用更多数据(但它被删除了):

[
  [[60, 60, -1, -1, -1, -5, 0], [0, 0]]
]

我收到此错误:

Traceback (most recent call last):
  File "CarSim.py", line 20, in <module>
    model.fit(X, y, epochs=100)
  File "\lib\site-packages\keras\engine\training.py", line 1239, in fit
    validation_freq=validation_freq)
  File "\lib\site-packages\keras\engine\training_arrays.py", line 196, in fit_loop
    outs = fit_function(ins_batch)
  File "\lib\site-packages\tensorflow_core\python\keras\backend.py", line 3740, in __call__
    outputs = self._graph_fn(*converted_inputs)
  File "\lib\site-packages\tensorflow_core\python\eager\function.py", line 1081, in __call__
    return self._call_impl(args, kwargs)
  File "\lib\site-packages\tensorflow_core\python\eager\function.py", line 1121, in _call_impl
    return self._call_flat(args, self.captured_inputs, cancellation_manager)
  File "\lib\site-packages\tensorflow_core\python\eager\function.py", line 1224, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager)
  File "\lib\site-packages\tensorflow_core\python\eager\function.py", line 511, in call
    ctx=ctx)
  File "\lib\site-packages\tensorflow_core\python\eager\execute.py", line 67, in quick_execute
    six.raise_from(core._status_to_exception(e.code, message), None)
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InternalError:  Blas GEMM launch failed : a.shape=(1, 7), b.shape=(7, 7), m=1, n=7, k=7
     [[node dense_1/MatMul (defined at \lib\site-packages\tensorflow_core\python\framework\ops.py:1751) ]] [Op:__inference_keras_scratch_graph_691]

Function call stack:
keras_scratch_graph

我尝试在situations.json 文件中使用更多数据,尝试不同的损失函数和神经网络架构,但总是遇到某种错误,这就是其中之一。我知道这与输入形状有关,但我无法修复它。任何帮助将不胜感激。

【问题讨论】:

    标签: python numpy tensorflow machine-learning keras


    【解决方案1】:

    在输出中,您省略了打印 X 和 y 的部分。无论如何,在我看来你的形状是错误的。您应该检查您放入 fit 方法的 X 和 y 是否具有正确的形状。这是没有 json 的代码的修改版本,它可以正常工作,并且应该有助于获得正确的形状。

    # tested with tf 1.14 and python 3.6
    import tensorflow as tf
    import numpy as np
    
    
    X = np.array([[60, 60, -1, -1, -1, -5, 0], [60, 60, -1, -1, -1, -5, 0], [60, 60, -1, -1, -1, -5, 0]])
    y = np.array([[0, 0], [0, 1], [11, 2]])
    
    model = tf.keras.Sequential([
        tf.keras.layers.InputLayer(input_shape=(7,)),
        tf.keras.layers.Dense(7),
        tf.keras.layers.Dense(2)
    ])
    model.compile(optimizer="adam", loss="mean_squared_error")
    model.fit(X, y, epochs=100)
    

    作为旁注,如果您正在对 json 数据进行培训,您可能需要查看 tfrecords。

    【讨论】:

    • 我在 python 3.7 的 tensorflow-gpu 2 中尝试了你的代码。但我仍然得到一个非常相似的错误。此外,X 形状为 (1, 7),y 形状为 (1, 2)
    • 我用 tf-cpu 2 (Python 3.6) 测试了我的代码,它也可以在那里工作。我的家用笔记本电脑上没有安装 tf-gpu 2,所以我无法在那里进行测试。但是,如果这取决于 tensorflow 版本,我会感到非常惊讶......请注意,我使用的是 tf.keras 而不是您正在使用的 keras。
    • 请注意,keras 和 tf.keras 是独立的库。
    • 我刚刚重新运行,但在 tf.keras 上再次出现错误。
    • 我现在已经让它在 tensorflow cpu 上运行,但它仍然在 gpu 上出错。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 2021-04-22
    • 2021-11-27
    • 2013-09-24
    • 2020-03-17
    • 2014-09-22
    • 2014-01-09
    相关资源
    最近更新 更多