Skip to content

Instantly share code, notes, and snippets.

import scrapetube
import random
import pandas as pd
AllVideoList = []
videos = scrapetube.get_channel("UCJIfeSCssxSC_Dhc5s7woww") # channel ID
titleList = []
urlList = []
while True:
from bayes_opt import BayesianOptimization
from bayes_opt.logger import JSONLogger
from bayes_opt.event import Events
logger = JSONLogger(path="./tau_logs.json")
pbounds = {'tau1': (0.1, 0.2), 'tau2': (0.3, 0.4),
'tau3': (0.5, 0.6), 'tau4': (0.7, 0.8),
}
# ref: https://blog.csdn.net/MachineLearner/article/details/104587288
def plot_decision_boundary(pred_func, X, y, figure=None):
"""Plot a decision boundary"""
if figure is None: # If no figure is given, create a new one
plt.figure()
# Set min and max values and give it some padding
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
h = 0.01
# 無重複數字
# input: name_list = [0, 1, 2, 3, 8]
for idx, num in enumerate(set(name_list)):
if idx != num:
name_list[idx] = idx
# output: name_list = [0, 1, 2, 3, 4]
# 有重複數字
# input: name_list = [0, 2, 2, 1, 6, 6]
for idx, num in enumerate(set(name_list)):
# evaluate the clustering performance
from sklearn.cluster import KMeans
from sklearn.metrics.cluster import normalized_mutual_info_score
import numpy as np
def evaluation(X, Y):
classN = np.max(Y)+1
kmeans = KMeans(n_clusters=classN).fit(X)
nmi = normalized_mutual_info_score(Y, kmeans.labels_, average_method='arithmetic')
return nmi
# https://stackoverflow.com/questions/63761717/load-image-dataset
# https://stackoverflow.com/questions/60655280/how-to-split-an-image-dataset-in-x-train-y-train-x-test-y-test-by-tensorflow
import tensorflow as tf
import pandas as pd
train_df = pd.read_csv('train.csv')
train_df['class'] = train_df['class'].apply(str)
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(horizontal_flip=True, vertical_flip=True,)
import numpy as np
from sklearn import metrics
from sklearn.metrics import roc_auc_score
y = np.array([1, 1, 2, 2])
#scores可以是模型預測結果(Label)
#scores也可以是模型預測的confidence(softmax probability)
scores = np.array([1, 1, 2, 2])
scores = np.array([0.1, 0.4, 0.35, 0.8])
area_under_curve = roc_auc_score(y, scores)
def eval(loader, gt_labels_t, output_file="output.txt"):
G.eval() # 特徵提取器
F1.eval() # 分類器
size = 0
correct = 0
y_pred=[]
y_true=[]
pred_prob = None
pred_result = None
import torch.nn.functional as F
import torch
import numpy as np
from torch.autograd import Function
import torch.nn as nn
from pdb import set_trace as breakpoint
import sys
import math
from torch.nn.parameter import Parameter
from torch.nn import init
# LabelSmoothing.py
# https://www.aiuai.cn/aifarm1333.html 示例 3
# From: Github - NVIDIA/DeepLearningExamples/PyTorch/Classification
# smoothing.py
import torch
import torch.nn as nn
# 一般版本LabelSmoothing
class LabelSmoothing(nn.Module):