Skip to content

Instantly share code, notes, and snippets.

View yashprakash13's full-sized avatar
📱
/* ... and everything in between ... */ :)

Yash Prakash yashprakash13

📱
/* ... and everything in between ... */ :)
View GitHub Profile
[tool.black]
line-length = 109
target-version = ['py37']
[tool.isort]
profile = "black"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 21.12b0
hooks:
@yashprakash13
yashprakash13 / main.py
Created March 5, 2022 03:17
## Wandb Keras tutorial
import argparse
import os
from train import train_cnn
# let's define some default hyperparameter values
PROJECT_NAME = "fashion_mnist"
BATCH_SIZE = 32
DROPOUT = 0.2
EPOCHS = 20
@yashprakash13
yashprakash13 / train.py
Last active March 5, 2022 15:09
Weights & Biases Keras Fundamentals Tutorial
"""
Defines a simple CNN model on the fashion mnist dataset.
"""
from keras.preprocessing.image import ImageDataGenerator
from keras.datasets import fashion_mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Dense, Flatten
from tensorflow.keras.optimizers import Adam
from keras.utils import np_utils
study = optuna.create_study(direction="minimize")
study.optimize(objective, n_trials=500)
print(f"Optimized RMSE: {study.best_value:.4f}")
print("Best params:")
for key, value in study.best_params.items():
print(f"\t{key}: {value}")
def objective(n_trials):
params = {
"n_estimators": n_trials.suggest_int("n_estimators", 100, 2000, step=100),
"learning_rate": n_trials.suggest_float("learning_rate", 1e-4, 0.3, log=True),
"max_depth": n_trials.suggest_int("max_depth", 3, 15),
"n_iter_no_change": 50,
}
dtrain = xgb.DMatrix(data = X_train, label = y_train)
dval = xgb.DMatrix(data = X_test, label = y_val)
from sklearn.model_selection import train_test_split
X = df.drop(columns=['median_house_value'])
y = df.median_house_value
# split the data
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=0)
X_train.shape, X_val.shape, y_train.shape, y_val.shape
# make a new regressor model
# Do grid search
from sklearn.model_selection import GridSearchCV
rf_classifier = RandomForestClassifier(random_state=0)
rf_model_pipeline = Pipeline(steps=[
('preprocessing', columns_transformer),
('rf_model', rf_classifier),
])
# MODEL Pipeline
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix, accuracy_score
import matplotlib.pyplot as plt
# random forest classifier
rf_classifier = RandomForestClassifier(n_estimators = 11, criterion='entropy', random_state=0)
rf_model_pipeline = Pipeline(steps=[
# COLUMN PIPELINE
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import MinMaxScaler
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
col_transformation_pipeline = Pipeline(steps=[
('impute', SimpleImputer(strategy='mean')),