我的数据集变得非常大,因此我无法使用典型的OLS方法来计算线性回归估计量,因此我想使用典型的优化器(Adam似乎很适合)
我知道我可以用Keras相当简单地做到这一点,请参阅下面的例子
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
# Define the model
def build_model(input_dim):
model = Sequential()
# Using a smaller standard deviation for the normal initializer
model.add(Dense(1, input_dim=input_dim, kernel_initializer=tf.keras.initializers.RandomNormal(mean=0.0, stddev=0.05), activation='linear'))
# Increased learning rate
optimizer = Adam(learning_rate=0.1)
model.compile(loss='mse', optimizer=optimizer, metrics=['mse'])
return model
# Example usage:
X = np.array([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]], dtype=float)
y = np.array([3, 5, 7, 9, 11], dtype=float)
# Build and train the model
model = build_model(input_dim=2)
model.fit(X, y, epochs=1000, verbose=0, batch_size=5) # Reduced number of epochs and batch size
# Make predictions
predictions = model.predict(X)
print("Predictions:", predictions.flatten())
# Output the model summary to check the structure
model.summary()
model.get_weights()
然而,我的问题是,即使在1000个纪元之后,它仍然没有收敛到明显的1,1权重,大约是1.15 / 0.85
Adam对于这个例子来说不是一个很好的优化者,或者我做错了什么-我记得前段时间玩过Singapore,我记得当时在lineg问题上交谈得非常快.这对我来说有点令人担忧,因为我需要在一个超过1,000,000 x 100的矩阵上运行它,并且在那里运行1000个纪元将永远需要.