text
stringlengths
1
93.6k
parser.add_argument('--ao', action='store_true', help='whether to use alternative optimization')
parser.add_argument('--cluster_method', type=str, default='kmeans', help='clustering method of kmeans or spherical_kmeans or kernel_kmeans to choose')
parser.add_argument('--cluster_iter', type=int, default=5, help='number of iterations of K-means')
parser.add_argument('--cluster_kernel', type=str, default='rbf', help='kernel to choose when using kernel K-means')
parser.add_argument('--gamma', type=float, default=None, help='bandwidth for rbf or polynomial kernel when using kernel K-means')
parser.add_argument('--sample_weight', action='store_true', help='whether to adapt sample weight when using kernel K-means')
parser.add_argument('--initial_cluster', type=int, default=1, help='target or source class centroids for initialization of K-means')
parser.add_argument('--init_cen_on_st', action='store_true', help='whether to initialize learnable cluster centers on both source and target instances')
parser.add_argument('--src_cen_first', action='store_true', help='whether to use source class centroids as initial target cluster centers at the first epoch')
parser.add_argument('--src_cls', action='store_true', help='whether to classify source instances when clustering target instances')
parser.add_argument('--src_fit', action='store_true', help='whether to use convex combination of true label vector and predicted label vector as training guide')
parser.add_argument('--src_pretr_first', action='store_true', help='whether to perform clustering over features extracted by source pre-trained model at the first epoch')
parser.add_argument('--learn_embed', action='store_true', help='whether to apply embedding clustering')
parser.add_argument('--no_second_embed', action='store_true', help='whether to not apply embedding clustering on output features of the first FC layer')
parser.add_argument('--alpha', type=float, default=1.0, help='degrees of freedom of Student\'s t-distribution')
parser.add_argument('--beta', type=float, default=1.0, help='weight of auxiliary target distribution or assigned cluster labels')
parser.add_argument('--embed_softmax', action='store_true', help='whether to use softmax to normalize soft cluster assignments for embedding clustering')
parser.add_argument('--div', type=str, default='kl', help='measure of prediction divergence between one target instance and its perturbed counterpart')
parser.add_argument('--gray_tar_agree', action='store_true', help='whether to enforce the consistency between RGB and gray images on the target domain')
parser.add_argument('--aug_tar_agree', action='store_true', help='whether to enforce the consistency between RGB and augmented images on the target domain')
parser.add_argument('--sigma', type=float, default=0.1, help='standard deviation of Gaussian for data augmentation operation of blurring')
# checkpoints
parser.add_argument('--resume', type=str, default='', help='checkpoints path to resume')
parser.add_argument('--log', type=str, default='./checkpoints/office31', help='log folder')
parser.add_argument('--stop_epoch', type=int, default=200, metavar='N', help='stop epoch for early stop (default: 200)')
# architecture
parser.add_argument('--arch', type=str, default='resnet50', help='model name')
parser.add_argument('--num_neurons', type=int, default=128, help='number of neurons of fc1')
parser.add_argument('--pretrained', action='store_true', help='whether to use pretrained model')
# i/o
parser.add_argument('--print_freq', type=int, default=10, metavar='N', help='print frequency (default: 10)')
args = parser.parse_args()
args.pretrained = True
if args.tar.find('amazon') == -1:
args.init_cen_on_st = True
elif args.src.find('webcam') != -1:
args.beta = 0.5
args.src_cls = True
args.src_cen_first = True
args.learn_embed = True
args.embed_softmax = True
args.log = args.log + '_adapt_' + args.src + '2' + args.tar + '_bs' + str(args.batch_size) + '_' + args.arch + '_lr' + str(args.lr) + '_' + args.cluster_method
return args
# <FILESEP>
import nltk
# Download necessary NLTK data
nltk.download("punkt", quiet=True)
nltk.download("stopwords", quiet=True)
nltk.download("punkt_tab")
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report
from sklearn.metrics import roc_auc_score
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
import numpy as np
import argparse
import string
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import os
import json
def preprocess_text(text):
try:
# If text is a list, concatenate all elements into a single string
if isinstance(text, list):
text = " ".join(text)
# Lowercase and remove punctuation
text = text.lower().translate(str.maketrans("", "", string.punctuation))
tokens = word_tokenize(text)
stop_words = set(stopwords.words("english"))
tokens = [word for word in tokens if word not in stop_words]
return " ".join(tokens)
except Exception as e:
print(f"Error processing text: {e}")
return ""
def vectorize_text(train, test, text_field="quiz", method="tfidf", num_ppl=5):
column_names = train.columns.tolist()
if f"clean_{text_field}" in column_names:
text_field = f"clean_{text_field}" # use clean data's field (not perturbed data)
if method == "tfidf":
vectorizer = TfidfVectorizer(max_features=5000)
train_feature = vectorizer.fit_transform(train["processed_text"])
test_feature = vectorizer.transform(test["processed_text"])
train_feature = train_feature.toarray()
test_feature = test_feature.toarray()
elif method == "bow":
vectorizer = CountVectorizer(max_features=5000)
train_feature = vectorizer.fit_transform(train["processed_text"])
test_feature = vectorizer.transform(test["processed_text"])