forked from SuyashLakhotia/TextCategorization
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cnn_fchollet_train.py
138 lines (103 loc) · 5.57 KB
/
cnn_fchollet_train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import argparse
import os
import time
import numpy as np
import tensorflow as tf
import data
import utils
from cnn_fchollet import FCholletCNN
from train import train_and_test
model_name = "cnn_fchollet"
# Parse Arguments
# ==================================================
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dataset", type=str, default="20 Newsgroups", choices=data.AVAILABLE_DATASETS,
help="Dataset name (default: 20 Newsgroups)")
parser.add_argument("--vocab_size", type=int, default=None,
help="Vocabulary size (default: None [see data.py])")
parser.add_argument("--seq_len", type=int, default=None,
help="Sequence length for every pattern (default: None [see data.py])")
parser.add_argument("--filter_widths", type=int, nargs="+", default=[5, 5, 5],
help="Filter widths (default: [5, 5, 5])")
parser.add_argument("--num_features", type=int, nargs="+", default=[128, 128, 128],
help="No. of features per filter (default: [128, 128, 128])")
parser.add_argument("--pooling_sizes", type=int, nargs="+", default=[5, 5, 5],
help="Pooling sizes (default: [5, 5, 5])")
parser.add_argument("--fc_layers", type=int, nargs="+", default=[128],
help="Fully-connected layers (default: [128])")
parser.add_argument("--learning_rate", type=float, default=1e-3, help="Learning rate (default: 1e-3)")
parser.add_argument("--dropout", type=float, default=0.5, help="Dropout keep probability (default: 0.5)")
parser.add_argument("--l2", type=float, default=0.0, help="L2 regularization lambda (default: 0.0)")
parser.add_argument("--batch_size", type=int, default=64, help="Batch size (default: 64)")
parser.add_argument("--epochs", type=int, default=200, help="No. of epochs (default: 200)")
parser.add_argument("--notes", type=str, default=None,
help="Any notes to add to the results.csv output (default: None)")
args = parser.parse_args()
# Parameters
# ==================================================
# Pre-trained word embeddings
embedding_dim = 300 # dimensionality of embedding
embedding_file = "data/GoogleNews-vectors-negative300.bin" # word embeddings file
# Model parameters
seq_len = args.seq_len # sequence length for every pattern
filter_widths = args.filter_widths # filter widths
num_features = args.num_features # number of features per filter
pooling_sizes = args.pooling_sizes # pooling sizes
fc_layers = args.fc_layers # number of units in fully-connected layers
# Training parameters
learning_rate = args.learning_rate # learning rate
batch_size = args.batch_size # batch size
num_epochs = args.epochs # no. of training epochs
# Regularization parameters
dropout_keep_prob = args.dropout # dropout keep probability
l2_reg_lambda = args.l2 # L2 regularization lambda
# Misc. parameters
allow_soft_placement = True # allow device soft device placement i.e. fall back on available device
log_device_placement = False # log placement of operations on devices
# Data Preparation
# ==================================================
train, test = data.load_dataset(args.dataset, out="word2ind", vocab_size=args.vocab_size, maxlen=seq_len)
x_train = train.data.astype(np.int32)
x_test = test.data.astype(np.int32)
y_train = train.labels
y_test = test.labels
# Overwrite sequence length if seq_len was originally None
seq_len = x_train.shape[1]
# Construct reverse lookup vocabulary
reverse_vocab = {w: i for i, w in enumerate(train.vocab)}
# Process Google News word2vec file (in a memory-friendly way) and store relevant embeddings
print("Loading pre-trained embeddings from {}...".format(embedding_file))
embeddings = data.load_word2vec(embedding_file, reverse_vocab, embedding_dim)
# Print information about the dataset
utils.print_data_info(train, x_train, x_test, y_train, y_test)
# To print for results.csv
data_str = "{{format: 'word2ind', vocab_size: {}, seq_len: {}}}".format(len(train.vocab), seq_len)
# Training
# ==================================================
with tf.Graph().as_default():
session_conf = tf.ConfigProto(allow_soft_placement=allow_soft_placement,
log_device_placement=log_device_placement)
sess = tf.Session(config=session_conf)
with sess.as_default():
# Init model
cnn = FCholletCNN(sequence_length=seq_len,
num_classes=len(train.class_names),
vocab_size=len(train.vocab),
embedding_size=embedding_dim,
embeddings=embeddings,
filter_widths=filter_widths,
num_features=num_features,
pooling_sizes=pooling_sizes,
fc_layers=fc_layers,
l2_reg_lambda=l2_reg_lambda)
# Output directory for models and summaries
timestamp = str(int(time.time()))
out_dir = os.path.abspath(os.path.join(os.path.curdir, "runs", args.dataset, model_name, timestamp))
# Train and test model
max_accuracy = train_and_test(sess, cnn, x_train, y_train, x_test, y_test, learning_rate, batch_size,
num_epochs, dropout_keep_prob, out_dir)
# Output for results.csv
hyperparams = "{{filter_widths: {}, num_features: {}, pooling_sizes: {}, fc_layers: {}}}".format(
filter_widths, num_features, pooling_sizes, fc_layers)
utils.print_result(args.dataset, model_name, max_accuracy, data_str, timestamp, hyperparams, args,
args.notes)