Keras 模型输入的形状(尺寸)和类型错误

Error in shape (dimention) and type of Keras model input

我非常想设置这个简单的 Keras 模型的输入形状:( X 和 Y 都是 numpy.narray 但我不知道它有什么问题!我尝试了不同的 X 形状,但错误就在那里!代码中提供了数据集的信息(维度、样本数量等)。 X_train 的 .pkl 文件是从预训练模型的隐藏状态中获取的。

import pandas as pd
from sklearn.preprocessing import LabelEncoder
from keras.utils import np_utils
from keras import Input, Model
from keras.layers import Dense
import numpy as np

############################## X_Train ############################

X_Train_3embed1 = pd.read_pickle("XX_Train_3embeding.pkl")


X_Train_3embed = np.array(X_Train_3embed1)

print("X-Train")
print(X_Train_3embed.shape)   # (230, 1, 128)
print(type(X_Train_3embed))  # <class 'numpy.ndarray'>
print(X_Train_3embed[0].shape) # (1, 128)
print(type(X_Train_3embed[0])) # <class 'numpy.ndarray'>


############################## Y_Train ############################

Y_Train_labels_list = pd.read_pickle("lis_Y_all_Train.pkl")

print(type(Y_Train_labels_list))  #<class 'numpy.ndarray'>
print(type(Y_Train_labels_list[0])) #<class 'str'>

encoder = LabelEncoder()
encoder.fit(Y_Train_labels_list)
encoded_Y = encoder.transform(Y_Train_labels_list)
Y_my_Train = np_utils.to_categorical(encoded_Y)


print("Y-Train")
print(Y_my_Train.shape) #(230, 83)
print(type(Y_my_Train)) # <class 'numpy.ndarray'>
print(Y_my_Train[0].shape) # (83,)
print(type(Y_my_Train[0])) # <class 'numpy.ndarray'>

##################################  Model  ##################################

first_input = Input(shape=(1, 128))

first_dense = Dense(128)(first_input)

output_layer = Dense(83, activation='softmax')(first_dense)

model = Model(inputs=first_input, outputs=output_layer)

model.summary()


model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])


history = model.fit((X_Train_3embed, Y_my_Train), epochs=2, batch_size=32)

结果如下:

Model: "model_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 1, 128)            0         
_________________________________________________________________
dense_1 (Dense)              (None, 1, 128)            16512     
_________________________________________________________________
dense_2 (Dense)              (None, 1, 83)             10707     
=================================================================
Total params: 27,219
Trainable params: 27,219
Non-trainable params: 0
_________________________________________________________________
Traceback (most recent call last):
  File "/home/vahideh/PycharmProjects/3KArgen-master/MyTransferClassifier2.py", line 63, in <module>
    history = model.fit((X_Train_3embed, Y_my_Train), epochs=2, batch_size=32)
  File "/home/vahideh/PycharmProjects/MyVirtualEnvs/MyKargo/lib/python3.6/site-packages/keras/engine/training.py", line 1154, in fit
    batch_size=batch_size)
  File "/home/vahideh/PycharmProjects/MyVirtualEnvs/MyKargo/lib/python3.6/site-packages/keras/engine/training.py", line 579, in _standardize_user_data
    exception_prefix='input')
  File "/home/vahideh/PycharmProjects/MyVirtualEnvs/MyKargo/lib/python3.6/site-packages/keras/engine/training_utils.py", line 99, in standardize_input_data
    data = [standardize_single_array(x) for x in data]
  File "/home/vahideh/PycharmProjects/MyVirtualEnvs/MyKargo/lib/python3.6/site-packages/keras/engine/training_utils.py", line 99, in <listcomp>
    data = [standardize_single_array(x) for x in data]
  File "/home/vahideh/PycharmProjects/MyVirtualEnvs/MyKargo/lib/python3.6/site-packages/keras/engine/training_utils.py", line 34, in standardize_single_array
    elif x.ndim == 1:
AttributeError: 'tuple' object has no attribute 'ndim'

如何将这些数据集提供给模型?或更改模型的输入形状?

您的模型输出的形状为 (None, 1, 83),即每个样本输出为 1 x 83,但每个样本的基本事实是一个标量。有两种方法可以解决这个问题

  1. 展平输出并继续使用您的数据
  2. 删除数据中不必要的维度,即将每个样本从 1X128 扁平化为 128 并更改模型架构以处理一维数据,这将导致输出为一维。

固定码:

方法一

from keras import Input, Model
from keras.layers import Dense, Flatten
import numpy as np

# Dummy data
X_Train_3embed = np.random.randn(230, 1, 128)
Y_my_Train = np.random.randn(230, 83)

#model
first_input = Input(shape=(1, 128))
first_dense = Dense(128)(first_input)
output_layer = Dense(83, activation='softmax')(first_dense)
outputs = Flatten()(output_layer)

model = Model(inputs=first_input, outputs=outputs)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
model.summary()
model.fit(X_Train_3embed, Y_my_Train, epochs=2, batch_size=32)

输出:

Layer (type)                 Output Shape              Param #   
=================================================================
input_10 (InputLayer)        [(None, 1, 128)]          0         
_________________________________________________________________
dense_18 (Dense)             (None, 1, 128)            16512     
_________________________________________________________________
dense_19 (Dense)             (None, 1, 83)             10707     
_________________________________________________________________
flatten_6 (Flatten)          (None, 83)                0         
=================================================================
Total params: 27,219
Trainable params: 27,219
Non-trainable params: 0
_________________________________________________________________
Epoch 1/2
8/8 [==============================] - 1s 3ms/step - loss: 6.2275 - acc: 0.0162
Epoch 2/2
8/8 [==============================] - 0s 2ms/step - loss: 0.2639 - acc: 0.0150

方法二

# Dummy data
X_Train_3embed = np.random.randn(230, 1, 128)
Y_my_Train = np.random.randn(230, 83)

#model
first_input = Input(shape=(128))
first_dense = Dense(128)(first_input)
outputs = Dense(83, activation='softmax')(first_dense)

model = Model(inputs=first_input, outputs=outputs)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
model.summary()
model.fit(X_Train_3embed.reshape(-1,128), Y_my_Train, epochs=2, batch_size=32)

输出:

Layer (type)                 Output Shape              Param #   
=================================================================
input_13 (InputLayer)        [(None, 128)]             0         
_________________________________________________________________
dense_24 (Dense)             (None, 128)               16512     
_________________________________________________________________
dense_25 (Dense)             (None, 83)                10707     
=================================================================
Total params: 27,219
Trainable params: 27,219
Non-trainable params: 0
_________________________________________________________________
Epoch 1/2
8/8 [==============================] - 0s 2ms/step - loss: -1.1705 - acc: 0.0100
Epoch 2/2
8/8 [==============================] - 0s 2ms/step - loss: -6.3587 - acc: 0.0015

基本上 MarcoCerliani 要求您删除元组

您的代码:model.fit((X_Train_3embed, Y_my_Train), epochs=2, batch_size=32)

更改代码:model.fit(X_Train_3embed, Y_my_Train, epochs=2, batch_size=32)