首页 > 解决方案 > Keras 不适用于 3 个或更多维度。无效参数错误

问题描述

我想创建一个模型,该模型采用形状 (64, 4) 的矩阵来预测形状 (4) 的数组。但由于某种原因它不起作用。例如,这里有一个代码:

import numpy as np
from tensorflow.keras import models, layers, optimizers

x = np.random.uniform(size=600*64*4).reshape(600, 64, 4)
y = np.random.uniform(size=600*4).reshape(600, 4)

model = models.Sequential([
    layers.Dense(16, activation='relu', input_shape=[64, 4]),
    layers.Dense(16, activation='relu'),
    layers.Dense(4)
])
model.compile(loss='mean_absolute_error',
              optimizer=optimizers.SGD(lr=1e-3, momentum=0.9),
              metrics=["mae"])

model.fit(x, y, epochs=5)

这个脚本以InvalidArgumentError: Incompatible shapes: [32,64,4] vs. [32,4]
但下面的代码结束:

import numpy as np
from tensorflow.keras import models, layers, optimizers

x = np.random.uniform(size=600*4).reshape(600, 4)
y = np.random.uniform(size=600*4).reshape(600, 4)

model = models.Sequential([
    layers.Dense(16, activation='relu', input_shape=[4]),
    layers.Dense(16, activation='relu'),
    layers.Dense(4)
])
model.compile(loss='mean_absolute_error',
              optimizer=optimizers.SGD(lr=1e-3, momentum=0.9),
              metrics=["mae"])
model.fit(x, y, epochs=5)

...工作得很好。对我来说,这种行为似乎存在逻辑错误。或者也许我不明白什么。

请帮忙。

标签: pythontensorflowkerasneural-network

解决方案


以下作品,

from tensorflow.keras.layers import *
from tensorflow.keras.models import Model, Sequential
import tensorflow as tf
import numpy as np

x = np.random.uniform(size=600*64*4).reshape(600, 64, 4)
y = np.random.uniform(size=600*4).reshape(600, 4)

ip = Input(shape=(64,4))
d1 = Dense(16, activation='relu')(ip)
f = Flatten()(d1)
d2 = Dense(16, activation='relu')(f)
d3 = Dense(4)(d2)

model = Model(ip, d3)

model.compile(loss='mse', metrics='mae', optimizer='adam')
model.summary()

model.fit(x,y,epochs=1, batch_size = 64)

您以错误的方式使用 Dense,FC 层除了单维数据之外,大多数情况下您还需要在某些层中展平,以便您的最后输出与y.

Model: "model_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         [(None, 64, 4)]           0         
_________________________________________________________________
dense_6 (Dense)              (None, 64, 16)            80        
_________________________________________________________________
flatten (Flatten)            (None, 1024)              0         
_________________________________________________________________
dense_7 (Dense)              (None, 16)                16400     
_________________________________________________________________
dense_8 (Dense)              (None, 4)                 68        
=================================================================
Total params: 16,548
Trainable params: 16,548
Non-trainable params: 0
_________________________________________________________________
10/10 [==============================] - 0s 3ms/step - loss: 0.1689 - mae: 0.3340

<tensorflow.python.keras.callbacks.History at 0x7f838cd4deb8>

检查 Flatten 层,确保模型的输出是二维的(batch_size、num_class 或 output_nodes)。但是如果没有展平,您就可以从模型中获得 3-d 输出,因此您也必须使 y 3-d。


推荐阅读