张量流模型是用输入张量的形状构造的,但它是在形状不兼容的输入上调用的(神经网络)



基本上,我试图构建一个使用嵌入单词作为输入的模型,但我一直收到这个警告。我多次尝试根据模型的训练数据更改输入的形状,但这对模型构建没有任何影响。我正在尝试为我的输入预测温度值,范围从0-100

在训练中,损失分数有时会改变,有时则不会,。我真的不确定这里发生了什么。

数据的原始形状:

x_train shape: (17405, 3840)
y_train shape: (17405,)
x_valid shape: (4352, 3840)
y_valid shape: (4352,)

构建模型

# Initializing the ANN
model = Sequential()
# Adding the input layer and the first hidden layer
model.add(Dense(units=1, activation='relu', input_shape = x_train.shape))
# Adding the second hidden layer
model.add(Dense(units=1, activation='relu'))
# Adding the output layer
model.add(Dense(units=1, activation='linear'))

编译和培训

#Global Variables for Model
optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001) #methods used to change the attributes of the nn
# tf.keras.optimizers.SGD(learning_rate=0.001)
batch_size = 100 # Defines the number of samples that will be propagated through the network.
loss = 'mean_squared_error' #Cmpute the quantity that a model should seek to minimize during training
EPOCHS = 20 # How many times to go through the training set.
# Compiling the Model
model.compile(optimizer = optimizer, loss = loss, metrics=['mae'])
# Training/Fitting the Model on the Training set
model.fit(x_train, y_train, batch_size = batch_size, 
epochs = EPOCHS, verbose = 1)

#Model Summary
model.summary()

#Model Evaluation
score = model.evaluate(x_valid, y_valid, verbose=1)

如前所述,模型运行,但我得到以下错误:

WARNING:tensorflow:Model was constructed with shape (None, 17405, 3840) for input Tensor("dense_73_input:0", shape=(None, 17405, 3840), dtype=float32), but it was called on an input with incompatible shape (None, 3840).

您的数据形状实际上是(None, 3840),因为形状中的17405表示数据中元素的数量,您不必将其传递给模型。

所以你应该使用这个:

model = Sequential()
model.add(Dense(units=1, activation='relu', input_shape = x_train.shape[1]))
model.add(Dense(units=1, activation='relu'))
model.add(Dense(units=1, activation='linear'))

相关内容

最新更新