我正在尝试使用局部连接的神经网络来估计数学函数的有限元,使用 Keras 顺序模型。当我始终使用密集层时,我的代码可以完美且非常准确地工作。
但是当我切换到使用 LocallyConnected1D 层时,它会带来如图所示的错误。然而,我希望它适用于我使用的任何类型的图层,甚至是 dropout 图层。
所以根据屏幕截图,我认为问题出在尺寸上,但我无法理解。关于错误的简单含义以及可能如何解决问题的任何建议?谢谢。
代码是:
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping
from keras.layers import Dense, LocallyConnected1D
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import os
import sys
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
# %%
def LinearSampler(x_i_minus_1, x_i_plus_1, N):
gamma=1/2;
N0=int(N*gamma); Nrest=N-N0;
p1 = x_i_minus_1 / (x_i_minus_1 + 1 - x_i_plus_1)
p2 = (1 - x_i_minus_1) / (x_i_minus_1 + 1 - x_i_plus_1)
x1 = np.linspace(0, x_i_minus_1, int(p1 * Nrest))
x2 = np.linspace(x_i_minus_1, x_i_plus_1, int(N0))
x3 = np.linspace(x_i_plus_1, 1, int(p2 * Nrest))
x = np.concatenate((x1, x2, x3))
return x
# %% Defining the FEM tent functions
def tent_function(x, x_i_minus_1, x_i_plus_1):
"""
Compute the tent function for a given point x and interval [x_i_minus_1, x_i, x_i_plus_1].
k = number of finite elements, k = 1, 2, ... m < Inf
i = index of finite element function required; 1 = 1, 2, ..., k
"""
x_i = (x_i_minus_1 + x_i_plus_1)/2
if x <= x_i_minus_1 or x >= x_i_plus_1:
return 0
elif x_i_minus_1 <= x < x_i:
return (x - x_i_minus_1) / (x_i - x_i_minus_1)
else:
return (x_i_plus_1 - x) / (x_i_plus_1 - x_i)
# %% Basics
k = int(input('Enter the total number of finite elements: '))
i = int(input('Enter the index of the finite element to be fit: '))
if i > k:
print("The required finite element is outside the range of the available ones. That is, 'i' MUST always be less than or equal to 'k'")
sys.exit()
elif i == 0:
x_i_minus_1 = 0
else:
x_i_minus_1 = (i - 1)/(k + 1)
x_i_plus_1 = x_i_minus_1 + (2 / (k + 1))
# %% Generate training data set
train_x = LinearSampler(x_i_minus_1, x_i_plus_1, 50000)
train_y = np.array([tent_function(xi, x_i_minus_1, x_i_plus_1) for xi in train_x])
# %% Define the model architecture
model = Sequential()
model.add(LocallyConnected1D(64, 3, input_shape=(10, 1), activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='linear'))
# Compile the model
model.compile(loss='mse', optimizer='Adam', metrics=['accuracy'])
# Train the model on some data
early_stop = EarlyStopping(monitor='loss', patience=10*i) ##Early stop model in case it isn't performing
history = model.fit(train_x, train_y, epochs=50, verbose=1, callbacks=[early_stop])
错误是;
WARNING:tensorflow:Model was constructed with shape (None, 10, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 10, 1), dtype=tf.float32, name='locally_connected1d_2_input'), name='locally_connected1d_2_input', description="created by layer 'locally_connected1d_2_input'"), but it was called on an input with incompatible shape (None,).
Traceback (most recent call last):
File C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
exec(code, globals, locals)
File e:\desktop\...\file.py:111
history = model.fit(train_x, train_y, epochs=50, verbose=1, callbacks=[early_stop])
File C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\utils\traceback_utils.py:70 in error_handler
raise e.with_traceback(filtered_tb) from None
File ~\AppData\Local\Temp\__autograph_generated_filekrq21sck.py:15 in tf__train_function
retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
ValueError: in user code:
File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\engine\training.py", line 1160, in train_function *
return step_function(self, iterator)
File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\engine\training.py", line 1146, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\engine\training.py", line 1135, in run_step **
outputs = model.train_step(data)
File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\engine\training.py", line 993, in train_step
y_pred = self(x, training=True)
File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\keras\engine\input_spec.py", line 232, in assert_input_compatibility
raise ValueError(
ValueError: Exception encountered when calling layer "sequential_2" " f"(type Sequential).
Input 0 of layer "locally_connected1d_2" is incompatible with the layer: expected ndim=3, found ndim=1. Full shape received: (None,)
Call arguments received by layer "sequential_2" " f"(type Sequential):
• inputs=tf.Tensor(shape=(None,), dtype=float32)
• training=True
• mask=None