generated from slds-lmu/lecture_template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #220 from Ziyu-Mu/enet
enet
- Loading branch information
Showing
48 changed files
with
237 additions
and
310 deletions.
There are no files selected for viewing
File renamed without changes.
106 changes: 53 additions & 53 deletions
106
...tion/rsrc/data-augmentation-images-cat.py → ...ion/attic/data-augmentation-images-cat.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,53 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Tue Dec 12 17:55:57 2023 | ||
@author: chris | ||
""" | ||
|
||
from keras.datasets import cifar10 | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
from tensorflow.keras.preprocessing.image import ImageDataGenerator | ||
|
||
# Load CIFAR-10 dataset | ||
(x_train, y_train), (_, _) = cifar10.load_data() | ||
|
||
# Selecting a random dog image | ||
# In CIFAR-10, the label for dogs is 5 | ||
dog_indices = np.where(y_train == 3)[0] # cat is 3 | ||
random_index = np.random.choice(dog_indices) | ||
dog_image = x_train[random_index] | ||
|
||
# Data augmentation techniques | ||
datagen = ImageDataGenerator( | ||
rotation_range=20, | ||
width_shift_range=0.2, | ||
height_shift_range=0.2, | ||
shear_range=0.2, | ||
zoom_range=0.2, | ||
horizontal_flip=True, | ||
fill_mode='nearest' | ||
) | ||
|
||
# Preparing the image for augmentation | ||
dog_image = dog_image.reshape((1,) + dog_image.shape) | ||
|
||
# Applying the augmentation and plotting | ||
fig, axs = plt.subplots(1, 5, figsize=(15, 3)) | ||
axs[0].imshow(dog_image[0]) | ||
axs[0].axis('off') | ||
axs[0].set_title("Original", fontsize=20) | ||
|
||
# Generate 4 augmented images | ||
i = 1 | ||
for batch in datagen.flow(dog_image, batch_size=1): | ||
axs[i].imshow(batch[0].astype('uint8')) | ||
axs[i].axis('off') | ||
axs[i].set_title(f"Augmented {i}", fontsize=20) | ||
i += 1 | ||
if i > 4: | ||
break | ||
|
||
plt.tight_layout() | ||
plt.show() | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Tue Dec 12 17:55:57 2023 | ||
@author: chris | ||
""" | ||
|
||
from keras.datasets import cifar10 | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
from tensorflow.keras.preprocessing.image import ImageDataGenerator | ||
|
||
# Load CIFAR-10 dataset | ||
(x_train, y_train), (_, _) = cifar10.load_data() | ||
|
||
# Selecting a random dog image | ||
# In CIFAR-10, the label for dogs is 5 | ||
dog_indices = np.where(y_train == 3)[0] # cat is 3 | ||
random_index = np.random.choice(dog_indices) | ||
dog_image = x_train[random_index] | ||
|
||
# Data augmentation techniques | ||
datagen = ImageDataGenerator( | ||
rotation_range=20, | ||
width_shift_range=0.2, | ||
height_shift_range=0.2, | ||
shear_range=0.2, | ||
zoom_range=0.2, | ||
horizontal_flip=True, | ||
fill_mode='nearest' | ||
) | ||
|
||
# Preparing the image for augmentation | ||
dog_image = dog_image.reshape((1,) + dog_image.shape) | ||
|
||
# Applying the augmentation and plotting | ||
fig, axs = plt.subplots(1, 5, figsize=(15, 3)) | ||
axs[0].imshow(dog_image[0]) | ||
axs[0].axis('off') | ||
axs[0].set_title("Original", fontsize=20) | ||
|
||
# Generate 4 augmented images | ||
i = 1 | ||
for batch in datagen.flow(dog_image, batch_size=1): | ||
axs[i].imshow(batch[0].astype('uint8')) | ||
axs[i].axis('off') | ||
axs[i].set_title(f"Augmented {i}", fontsize=20) | ||
i += 1 | ||
if i > 4: | ||
break | ||
|
||
plt.tight_layout() | ||
plt.show() |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+25.8 KB
(120%)
slides/regularization/figure/lasso_ridge_enet_2d.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# ------------------------------------------------------------------------------ | ||
# enetlogreg | ||
|
||
# FIG: boxplot of R-squared for elasticnet, lasso and ridge | ||
# LEFT: linear model with 5 non-Zero coefficients (sparse) | ||
# RIGHT: linear model with 500 non-Zero coefficients | ||
# ------------------------------------------------------------------------------ | ||
|
||
library(ggplot2) | ||
library(gridExtra) | ||
load("enet_exp.RData") | ||
|
||
# PLOT ------------------------------------------------------------------------- | ||
|
||
q_values <- sapply(ba$task_id, function(task) { | ||
as.numeric(sub("q:(\\d+)", "\\1", task)) | ||
}) | ||
|
||
performance_df <- as.data.frame(ba) | ||
performance_df$q <- q_values | ||
performance_df$learner_id <- as.factor(gsub("\\.tuned", "", performance_df$learner_id)) | ||
|
||
# linear model with sparse features | ||
df_5 <- performance_df[performance_df['q']==5,] | ||
|
||
p1 <- ggplot(data = df_5, aes(x = regr.rsq, y = learner_id)) + | ||
geom_boxplot() + | ||
coord_flip() + | ||
ylab("") + | ||
labs(title="sparse") + | ||
xlab("R-squared")+ | ||
xlim(0.5,1)+ | ||
theme_minimal(base_size = 10) + | ||
theme(legend.position="none", | ||
axis.title.x=element_blank()) | ||
|
||
# linear model with non-sparse features | ||
df_500 <- performance_df[performance_df['q']==500,] | ||
|
||
p2 <- ggplot(data = df_500, aes(x = regr.rsq, y = learner_id)) + | ||
geom_boxplot() + | ||
coord_flip() + | ||
ylab("") + | ||
xlab("R-squared")+ | ||
labs(title="non-sparse") + | ||
xlim(0.5,1)+ | ||
theme_minimal(base_size = 10) + | ||
theme(legend.position="none", | ||
axis.title.x=element_blank()) | ||
|
||
p <- grid.arrange(p1, p2, nrow= 1) | ||
|
||
ggsave("../figure/enet_lasso_ridge_r2.png", plot = p, width = 6, height = 2) |
Binary file not shown.
Binary file not shown.
84 changes: 42 additions & 42 deletions
84
...arization/rsrc/make_l1_l2_regupaths_mse.R → ...regularization/rsrc/l1_l2_regupaths_mse.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,42 @@ | ||
library(glmnet) | ||
library(datasets) | ||
|
||
set.seed(42) | ||
|
||
# Load mtcars | ||
data(mtcars) | ||
x <- as.matrix(mtcars[, -1]) | ||
y <- mtcars$mpg | ||
|
||
# Normalize data | ||
x <- scale(x, center = TRUE, scale = TRUE) | ||
y <- scale(y, center = TRUE, scale = FALSE) | ||
|
||
# Lasso with cross-validation | ||
cvfit_lasso <- cv.glmnet(x, y, alpha = 1) | ||
|
||
# Ridge with cross-validation | ||
cvfit_ridge <- cv.glmnet(x, y, alpha = 0) | ||
|
||
# Plot layout | ||
par(mfrow = c(2, 2), oma = c(0, 0, 0, 0), mar = c(5, 4, 4, 2) + 0.1) | ||
par(cex.main = 1.7, cex.axis = 1.5, cex.lab = 1.5, lwd=1.2) | ||
|
||
# Plot regularization path for Lasso | ||
plot(cvfit_lasso$glmnet.fit, xvar = "lambda", label = TRUE, main = "") | ||
title(main = "lasso coefficients path", line = 2.5) | ||
|
||
# Plot regularization path for Ridge | ||
plot(cvfit_ridge$glmnet.fit, xvar = "lambda", label = TRUE, main = "") | ||
title(main = "ridge coefficients path", line = 2.5) | ||
|
||
# Plot RMSE vs. Lambda for Lasso | ||
plot(cvfit_lasso, main = "") | ||
title(main = "MSE vs. lambda for lasso", line = 2.5) | ||
|
||
# Plot RMSE vs. Lambda for Ridge | ||
plot(cvfit_ridge, main = "") | ||
title(main = "MSE vs. lambda for ridge", line = 2.5) | ||
|
||
# Reset layout to default | ||
par(mfrow = c(1, 1), oma = c(0, 0, 0, 0), mar = c(5, 4, 4, 2) + 0.1) | ||
library(glmnet) | ||
library(datasets) | ||
|
||
set.seed(42) | ||
|
||
# Load mtcars | ||
data(mtcars) | ||
x <- as.matrix(mtcars[, -1]) | ||
y <- mtcars$mpg | ||
|
||
# Normalize data | ||
x <- scale(x, center = TRUE, scale = TRUE) | ||
y <- scale(y, center = TRUE, scale = FALSE) | ||
|
||
# Lasso with cross-validation | ||
cvfit_lasso <- cv.glmnet(x, y, alpha = 1) | ||
|
||
# Ridge with cross-validation | ||
cvfit_ridge <- cv.glmnet(x, y, alpha = 0) | ||
|
||
# Plot layout | ||
par(mfrow = c(2, 2), oma = c(0, 0, 0, 0), mar = c(5, 4, 4, 2) + 0.1) | ||
par(cex.main = 1.7, cex.axis = 1.5, cex.lab = 1.5, lwd=1.2) | ||
|
||
# Plot regularization path for Lasso | ||
plot(cvfit_lasso$glmnet.fit, xvar = "lambda", label = TRUE, main = "") | ||
title(main = "lasso coefficients path", line = 2.5) | ||
|
||
# Plot regularization path for Ridge | ||
plot(cvfit_ridge$glmnet.fit, xvar = "lambda", label = TRUE, main = "") | ||
title(main = "ridge coefficients path", line = 2.5) | ||
|
||
# Plot RMSE vs. Lambda for Lasso | ||
plot(cvfit_lasso, main = "") | ||
title(main = "MSE vs. lambda for lasso", line = 2.5) | ||
|
||
# Plot RMSE vs. Lambda for Ridge | ||
plot(cvfit_ridge, main = "") | ||
title(main = "MSE vs. lambda for ridge", line = 2.5) | ||
|
||
# Reset layout to default | ||
par(mfrow = c(1, 1), oma = c(0, 0, 0, 0), mar = c(5, 4, 4, 2) + 0.1) |
Oops, something went wrong.