能否用神经网络拟合或近似多维函数?附Tensorflow/Keras代码需求
Absolutely! Neural networks are perfect for fitting or approximating multi-dimensional functions—this is one of their core superpowers, backed by the universal approximation theorem. That theorem basically says a simple feedforward network with one hidden layer can approximate any continuous function on a bounded input space, as long as it has enough neurons.
Let’s put this into practice with Keras (built on TensorFlow) to model the unknown function f(x,y) = sin(x) + y using synthetic "measurement data"—just like you described.
Step-by-Step Code Implementation
1. First, Generate Synthetic Measurement Data
We’ll create a set of (x,y) input pairs and their corresponding true outputs (this mimics the real-world measurements you’d have, where the underlying function is unknown).
import numpy as np import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense import matplotlib.pyplot as plt # Set a random seed for reproducibility np.random.seed(42) # Generate 10,000 random input samples x = np.random.uniform(-np.pi, np.pi, 10000) # x ranges from -π to π y = np.random.uniform(-5, 5, 10000) # y ranges from -5 to 5 X = np.column_stack((x, y)) # Combine into a 2D input array # Calculate the true outputs (this is the "unknown" function we're trying to model) y_true = np.sin(x) + y
2. Build the Neural Network
Since our target function is fairly simple, a small feedforward network will work great. We’ll use two hidden layers with ReLU activation—ReLU is great for most regression tasks because it introduces non-linearity without vanishing gradients.
model = Sequential([ Dense(16, activation='relu', input_shape=(2,)), # First hidden layer: 16 neurons Dense(8, activation='relu'), # Second hidden layer: 8 neurons Dense(1) # Output layer: single value (our prediction) ]) # Compile the model: use Adam optimizer (works well for most cases) and MSE loss (ideal for regression) model.compile(optimizer='adam', loss='mean_squared_error')
3. Train the Model
We’ll split our data into training and validation sets (80/20 split) to make sure our model generalizes well to unseen data.
# Split the data split_point = int(0.8 * len(X)) X_train, X_val = X[:split_point], X[split_point:] y_train, y_val = y_true[:split_point], y_true[split_point:] # Train the model training_history = model.fit( X_train, y_train, epochs=50, batch_size=32, validation_data=(X_val, y_val), verbose=1 # Shows training progress )
4. Check the Results
Let’s visualize how the model’s loss decreased over training, then compare its predictions to the true values.
# Plot training vs validation loss plt.plot(training_history.history['loss'], label='Training Loss') plt.plot(training_history.history['val_loss'], label='Validation Loss') plt.xlabel('Epoch') plt.ylabel('Mean Squared Error') plt.title('Model Loss Over Time') plt.legend() plt.show() # Generate new test data to evaluate the model x_test = np.random.uniform(-np.pi, np.pi, 100) y_test = np.random.uniform(-5, 5, 100) X_test = np.column_stack((x_test, y_test)) y_test_true = np.sin(x_test) + y_test y_test_pred = model.predict(X_test).flatten() # Plot predictions vs true values plt.scatter(y_test_true, y_test_pred, alpha=0.6) plt.xlabel('True Function Outputs') plt.ylabel('Model Predictions') plt.title('Predictions vs True Values') # Add a diagonal line (perfect prediction) plt.plot([y_test_true.min(), y_test_true.max()], [y_test_true.min(), y_test_true.max()], 'r--') plt.show()
Quick Tips for Better Performance
- If you’re working with a more complex multidimensional function, try increasing the number of neurons/layers, or experiment with activation functions like
tanh. - Keep an eye on the validation loss—if it starts going up while training loss goes down, your model is overfitting. You can fix this by adding dropout layers, reducing model size, or stopping training early.
- For real-world data, make sure to normalize your inputs (scale them to a range like 0-1 or -1 to 1) to help the optimizer converge faster.
内容的提问来源于stack exchange,提问作者Michael Haar

