L1正则化不适用于线性回归,在下面的例子中,我创建了一个数据y = 2x1+ 5x2 + 3*x3 + 5
在数据集中,x4参数是存在的,但是为了计算样本数据中的输出,我们没有使用它,所以当我们应用L1正则化时,我们应该得到w4接近于零,但是它仍然不起作用。
# In[1]:
import pandas as pd
import numpy as np
import tensorflow as tf
from matplotlib import pyplot as plt
# In[2]:
def plot_the_data(training_df, output):
plt.xlabel("feature")
plt.ylabel("label")
plt.ylim([output.min() - 1 , output.max() + 1])
print(training_df.columns)
for column in training_df.columns:
print(training_df[column].shape)
plt.xlabel(column)
plt.scatter(training_df[column], output)
plt.show()
total_data = 10000
output_label = 'output'
normalize_data = True
def createData(total_data):
my_label = np.zeros((total_data,))
my_feature = np.append(np.random.uniform(low=10, high=100, size=(total_data,)).reshape((total_data, 1)), # first col
np.random.uniform(low=300, high=500, size=(total_data,)).reshape((total_data, 1)), axis=1) # sec col
my_feature = np.append(my_feature ,
np.random.uniform(low=600, high=800, size=(total_data,)).reshape((total_data, 1)), axis=1) # third col
my_feature = np.append(my_feature ,
np.random.uniform(low=900, high=1000, size=(total_data,)).reshape((total_data, 1)), axis=1) # fourth col, unused
for i, row in enumerate(my_feature):
my_label[i] = ((2 + np.random.uniform(0.0, 0.1) ) * row[0]) + ((5 + np.random.uniform(0.0, 0.1) )* row[1]) + ((3 + np.random.uniform(0.0, 0.1) )* row[2]) + (50 + np.random.randint(low=1, high=3))
training_df = pd.DataFrame({ 'x1' : my_feature[:,0], 'x2' : my_feature[:,1], 'x3' : my_feature[:,2], 'x4' : my_feature[:,3], 'output': my_label})
return training_df
training_df = createData(total_data)
training_df = training_df.reindex(np.random.permutation(training_df.index))
training_df[output_label] = training_df[output_label] / 1000
print(training_df)
plot_the_data(training_df, training_df[output_label])
# In[3]:
def normalize_the_data(training_df):
normalized_df = pd.DataFrame()
norm_list = {}
for column in training_df.columns:
print(f"Max, Min of {column} pre normalization: {np.max(training_df[column]):0.2f}, {np.min(training_df[column]):0.2f}")
norm_l = tf.keras.layers.Normalization(axis=-1)
norm_l.adapt(training_df[column]) # learns mean, variance
Xn = norm_l(training_df[column])
normalized_df[column] = Xn.numpy()[0]
norm_list[column] = norm_l
print(f"Max, Min of {column} post normalization: {np.max(normalized_df[column]):0.2f}, {np.min(normalized_df[column]):0.2f}")
return normalized_df, norm_list
def normalize_the_test_data(training_df, norm_list):
normalized_df = pd.DataFrame()
for column in training_df.columns:
print(f"Max, Min of {column} pre normalization: {np.max(training_df[column]):0.2f}, {np.min(training_df[column]):0.2f}")
norm_l = norm_list[column]
norm_l.adapt(training_df[column]) # learns mean, variance
Xn = norm_l(training_df[column])
normalized_df[column] = Xn.numpy()[0]
print(f"Max, Min of {column} post normalization: {np.max(normalized_df[column]):0.2f}, {np.min(normalized_df[column]):0.2f}")
return normalized_df
if normalize_data == True:
print("Data Normalization enabled")
norm_l = tf.keras.layers.Normalization(axis=-1)
normalized_df, norm_l = normalize_the_data(training_df)
else:
print("Data Normalization disabled")
normalized_df = training_df
# In[4]:
normalized_df = normalized_df.loc[:, normalized_df.columns != output_label]
# In[5]:
tf.random.set_seed(1)
learning_rate=0.01
my_learning_rate = learning_rate
epochs=20
batch_size=20
validation_split = 0.2
print(normalized_df.head())
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(units=1, input_shape=(training_df.shape[1] - 1,), kernel_regularizer='l2'))
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=my_learning_rate),
loss="mean_squared_error",
metrics=[tf.keras.metrics.RootMeanSquaredError(),tf.keras.metrics.Accuracy()])
model.summary()
weights_dict = {}
weight_callback = tf.keras.callbacks.LambdaCallback(on_epoch_end=lambda epoch, logs: weights_dict.update({epoch: { "w" : model.get_weights()[0][0][0], "b" : model.get_weights()[1][0]}}))
history = model.fit(x=normalized_df,
y=training_df[output_label],
# verbose='0',
batch_size=batch_size,
epochs=epochs,
callbacks=weight_callback,
validation_split=validation_split)
# Gather the trained model's weight and bias.
trained_weight = model.get_weights()[0]
trained_bias = model.get_weights()[1]
epochs = history.epoch
hist = pd.DataFrame(history.history)
rmse = hist["root_mean_squared_error"]
testrmse = hist["val_root_mean_squared_error"]
print("Defined build_model and train_model", trained_weight, trained_bias)
# In[6]:
predictions = model.predict(normalized_df)
print(training_df.loc[:, training_df.columns == output_label])
plot_the_data( training_df.loc[:, training_df.columns == output_label], predictions.T)
# In[7]:
def plot_the_data_and_pred(training_df, output, pred):
plt.xlabel("feature")
plt.ylabel("label")
print(training_df.columns)
for column in training_df.columns:
plt.xlabel(column)
plt.scatter(training_df[column], output)
plt.scatter(training_df[column], pred)
plt.show()
plot_the_data_and_pred(training_df, training_df[output_label], predictions)
# In[8]:
def plot_the_loss_curve(epochs, rmse, testrmse):
"""Plot the loss curve, which shows loss vs. epoch."""
plt.figure()
plt.xlabel("Epoch")
plt.ylabel("Root Mean Squared Error")
plt.plot(epochs, rmse, label="Training Loss")
plt.plot(epochs, testrmse, label="Validation Loss")
# plt.plot(epochs, rmse, label="Loss")
plt.legend()
#y axis limit for the y axes
plt.ylim([rmse.min()*0.97, rmse.max() + 10])
plt.show()
plot_the_loss_curve(epochs, rmse, testrmse)
# In[9]:
output_label = 'output'
total_test_data = 1000
my_test_label = np.zeros((total_test_data,))
def createTestData(total_data):
my_label = np.zeros((total_data,))
my_feature = np.append(np.random.uniform(low=50, high=60, size=(total_data,)).reshape((total_data, 1)), # first col
np.random.uniform(low=350, high=400, size=(total_data,)).reshape((total_data, 1)), axis=1) # sec col
my_feature = np.append(my_feature ,
np.random.uniform(low=690, high=750, size=(total_data,)).reshape((total_data, 1)), axis=1) # third col
my_feature = np.append(my_feature ,
np.random.uniform(low=950, high=975, size=(total_data,)).reshape((total_data, 1)), axis=1) # fourth col, unused
for i, row in enumerate(my_feature):
my_label[i] = ((2 + np.random.uniform(0.0, 0.1) ) * row[0]) + ((5 + np.random.uniform(0.0, 0.1) )* row[1]) + ((3 + np.random.uniform(0.0, 0.1) )* row[2]) + (50 + np.random.randint(low=1, high=3))
training_df = pd.DataFrame({ 'x1' : my_feature[:,0], 'x2' : my_feature[:,1], 'x3' : my_feature[:,2], 'x4' : my_feature[:,3], 'output': my_label})
return training_df
test_df = createTestData(total_test_data)
test_df[output_label] = test_df[output_label] / 1000
print(test_df)
# plot_the_data(test_df, test_df[output_label])
if normalize_data == True:
print("Data Normalization enabled")
normalised_test_df = normalize_the_test_data(test_df, norm_l)
else:
print("Data Normalization disabled")
normalised_test_df = test_df
normalised_test_df = normalised_test_df.loc[:, normalised_test_df.columns != output_label]
print(normalised_test_df)
test_predictions = model.predict(normalised_test_df)
plot_the_data( test_df.loc[:, test_df.columns != output_label], test_df[output_label])
plot_the_data_and_pred(test_df.loc[:, test_df.columns != output_label], test_df[output_label], test_predictions)
# In[10]:
print("Defined build_model and train_model", trained_weight, trained_bias)
# In[11]:
results = model.evaluate(normalised_test_df, test_df[output_label], batch_size=batch_size)
# In[12]:
tf.random.set_seed(1)
epochs = 20
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(units=1, input_shape=(training_df.shape[1] - 1,), kernel_regularizer='l1'))
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=my_learning_rate),
loss="mean_squared_error",
metrics=[tf.keras.metrics.RootMeanSquaredError(),tf.keras.metrics.Accuracy()])
model.summary()
weights_dict = {}
weight_callback = tf.keras.callbacks.LambdaCallback(on_epoch_end=lambda epoch, logs: weights_dict.update({epoch: { "w" : model.get_weights()[0][0][0], "b" : model.get_weights()[1][0]}}))
history = model.fit(x=normalized_df,
y=training_df[output_label],
# verbose='0',
batch_size=batch_size,
epochs=epochs,
callbacks=weight_callback,
validation_split=validation_split)
# Gather the trained model's weight and bias.
trained_weight = model.get_weights()[0]
trained_bias = model.get_weights()[1]
epochs = history.epoch
hist = pd.DataFrame(history.history)
rmse = hist["root_mean_squared_error"]
testrmse = hist["val_root_mean_squared_error"]
print("Defined build_model and train_model", trained_weight, trained_bias)
# In[13]:
results = model.evaluate(normalised_test_df, test_df[output_label], batch_size=batch_size)
# In[ ]:
字符串
1条答案
按热度按时间mctunoxg1#
看起来你正在对训练数据进行归一化,但不清楚测试数据是否以同样的方式进行归一化。请确保这样做。
当应用L1正则化时,正则化项应该在模型编译过程中添加到损失函数中。尝试这样做:
字符串
如果你能分享一些玩具数据我就能复制这个了。