假设您有这个数据集:
1.05 1.10 1.15 1.20 1.25
0.2 0.000 0.000 0.000 0.000 0.000
0.3 0.350 0.350 0.350 0.350 0.350
0.4 0.615 0.619 0.623 0.626 0.628
0.5 0.805 0.816 0.826 0.834 0.839
有没有什么方法可以使用神经网络来插值,例如 0.389 和 1.168,而不仅仅是使用简单插值或双线性插值?
tf.keras.Sequential
:
import os
import random
import numpy as np
import tensorflow as tf
# Set all random seeds for reproducibility on same machine at least.
RANDOM_SEED = 1
os.environ['PYTHONHASHSEED'] = str(RANDOM_SEED)
random.seed(RANDOM_SEED)
np.random.seed(RANDOM_SEED)
tf.random.set_seed(RANDOM_SEED)
# Data
x1 = np.array([1.05, 1.10, 1.15, 1.20, 1.25])
x2 = np.array([0.2, 0.3, 0.4, 0.5])
y = np.array([[0.000, 0.000, 0.000, 0.000, 0.000],
[0.350, 0.350, 0.350, 0.350, 0.350],
[0.615, 0.619, 0.623, 0.626, 0.628],
[0.805, 0.816, 0.826, 0.834, 0.839]])
X1_mesh, X2_mesh = np.meshgrid(x1, x2)
X = np.column_stack((X1_mesh.ravel(), X2_mesh.ravel()))
Y = y.ravel()
# Model
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(64, input_dim=2, activation='relu'))
model.add(tf.keras.layers.Dense(64, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='linear'))
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(X, Y, epochs=1000, verbose=0)
# Predict
new_point = np.array([[1.168, 0.389]])
predicted_value = model.predict(new_point)
print(predicted_value)
示例输出:
1/1 [==============================] - 0s 55ms/step
[[0.5972232]]