【问题标题】:Error "Function call stack: train_function" occurred in implementation of convLSTM2D()执行 convLSTM2D() 时出现错误“函数调用堆栈:train_function”
【发布时间】:2021-08-03 04:16:19
【问题描述】:

ConvLSTM2D的实现出现错误。

我第一次实现了时间序列数据分析。 所以排列数据格式的过程可能是错误的。 我不知道原因和解决方法,所以请帮助我...!

如果你运行这个程序,请在同一层次中准备5个图像“0.png”、“1.png”...“4.png”。 (链接here

from scipy.sparse import dok
import tensorflow as tf
import numpy as np

from matplotlib import pyplot as plt
from keras.models import Model
from keras.preprocessing.image import img_to_array, load_img
from keras.layers import Input, Dense, Reshape ,ConvLSTM2D,BatchNormalization,Activation
from keras.optimizers import Adam
from sklearn.model_selection import train_test_split

imgSize = 200
dataNum = 5

labels = list(range(dataNum, 0, -1))
imgs = []

for i in range(5):
    img = img_to_array(load_img(str(i)+'.png', target_size=(imgSize,imgSize)))
    imgs.append(img)


#-----------------------------------------------------------------------------------
# Arrange in a format that can be learned in Time-series
# original data format: List of the same number of labels(:labels) and images(:imgs)

# imgs:[image1,image2,image3,image4,image5...]
# labels:[100,110,150,140,160...]

n_seq = 2
n_sample = dataNum - n_seq
x = np.zeros((n_sample, n_seq, imgSize, imgSize, 3))
for i in range(n_sample):
    x[i] = imgs[i:i + n_seq]
del labels[0:n_seq]

#-----------------------------------------------------------------------------------
# Data-Split(val,test)
img_train, img_val, Label_train, Label_val = train_test_split(x,labels, test_size=0.6, shuffle=False)

#-----------------------------------------------------------------------------------
# Model and Training

inputL = Input(shape=(n_seq, imgSize, imgSize, 3))
x0 = ConvLSTM2D(filters=16, kernel_size=(3, 3), padding="same", return_sequences=True, data_format="channels_last")(inputL)
x0 = BatchNormalization(momentum=0.6)(x0)
x0 = ConvLSTM2D(filters=16, kernel_size=(3, 3), padding="same", return_sequences=True, data_format="channels_last")(x0)
x0 = BatchNormalization(momentum=0.8)(x0)
x0 = ConvLSTM2D(filters=3, kernel_size=(3, 3), padding="same", return_sequences=False, data_format="channels_last")(x0)
out = Activation('tanh')(x0)
outputL = Dense(1, name="a")(out)

model = Model(inputs=inputL, outputs=outputL)
model.compile(Adam(learning_rate=0.01),loss= {'a': 'mae'},metrics= {'a': 'mae'})

history = model.fit([np.array(img_train)], [np.array(Label_train)],
                    epochs=50, batch_size=16,
                    validation_data=([np.array(img_val)],[np.array(Label_val)]))

错误信息↓

Traceback (most recent call last):
  File "c:/Users/UserA/StudyAI/LSTM.py", line 189, in <module>
    history = model.fit([np.array(img_train)], [np.array(Label_train)],
  File "C:\Users\UserA\anaconda3\lib\site-packages\keras\engine\training.py", line 1158, in fit
    tmp_logs = self.train_function(iterator)
  File "C:\Users\UserA\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py", line 889, in __call__
    result = self._call(*args, **kwds)
  File "C:\Users\UserA\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py", line 950, in _call
    return self._stateless_fn(*args, **kwds)
  File "C:\Users\UserA\anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 3023, in __call__
    return graph_function._call_flat(
  File "C:\Users\UserA\anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 1960, in _call_flat
    return self._build_call_outputs(self._inference_function.call(
  File "C:\Users\UserA\anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 591, in call
    outputs = execute.execute(
  File "C:\Users\UserA\anaconda3\lib\site-packages\tensorflow\python\eager\execute.py", line 59, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError:  required broadcastable shapes at loc(unknown)
         [[node mean_absolute_error_1/sub (defined at C:\Users\UserA\anaconda3\lib\site-packages\keras\losses.py:1301) ]] [Op:__inference_train_function_10812]

Errors may have originated from an input operation.
Input Source operations connected to node mean_absolute_error_1/sub:
 ExpandDims_1 (defined at C:\Users\UserA\anaconda3\lib\site-packages\keras\engine\data_adapter.py:1414)
 model/b/BiasAdd (defined at C:\Users\UserA\anaconda3\lib\site-packages\keras\layers\core.py:1233)

Function call stack:
train_function

【问题讨论】:

  • 你能给出数据集的前几行吗?
  • 感谢您的评论!!我将图像和标签保存在各自的列表中(我在源代码中添加了新行)。一份训练数据由一张图片和一个数字组成。
  • 不,我指的是实际数据,所以我可以尝试从我这边运行整个脚本
  • 我修改了代码,使其可以执行。很抱歉给您添麻烦,请准备5张图片(0.png、1.png、2.png、3.png、4.png)。
  • 你的图片尺寸是多少?

标签: python tensorflow keras conv-neural-network lstm


【解决方案1】:

您的模型输出为(200,200,1),因为您将超过 2 维的张量传递给密集层,因此您将在输出中获得所有其他维度,而不仅仅是 1 (Dense(1))。

您可以通过在最后一层之前添加 Flatten 层来解决此问题:

inputL = Input(shape=(n_seq, imgSize, imgSize, 3))
x0 = ConvLSTM2D(filters=16, kernel_size=(3, 3), padding="same", return_sequences=True, data_format="channels_last")(inputL)
x0 = BatchNormalization(momentum=0.6)(x0)
x0 = ConvLSTM2D(filters=16, kernel_size=(3, 3), padding="same", return_sequences=True, data_format="channels_last")(x0)
x0 = BatchNormalization(momentum=0.8)(x0)
x0 = ConvLSTM2D(filters=3, kernel_size=(3, 3), padding="same", return_sequences=False, data_format="channels_last")(x0)
x0 = Activation('tanh')(x0)        #Alter this
out = keras.layers.Flatten()(x0)   #Add this
outputL = Dense(1, name="a")(out)

P.S:使用 model.summary() 调查模型的输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-01
    • 2021-04-07
    • 2021-03-26
    • 2020-11-28
    • 1970-01-01
    • 2019-11-25
    • 2010-11-14
    • 2019-11-30
    相关资源
    最近更新 更多