Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dgl
Commits
1bbc885b
Unverified
Commit
1bbc885b
authored
Dec 06, 2018
by
Da Zheng
Committed by
GitHub
Dec 06, 2018
Browse files
move batcher to examples. (#269)
* move pytorch code to examples. * fix. * fix tutorial
parent
7aa494b3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
24 additions
and
15 deletions
+24
-15
examples/pytorch/tree_lstm/train.py
examples/pytorch/tree_lstm/train.py
+12
-3
python/dgl/data/tree.py
python/dgl/data/tree.py
+0
-10
tutorials/models/2_small_graph/3_tree-lstm.py
tutorials/models/2_small_graph/3_tree-lstm.py
+12
-2
No files found.
examples/pytorch/tree_lstm/train.py
View file @
1bbc885b
...
@@ -12,6 +12,15 @@ from dgl.data.tree import SST
...
@@ -12,6 +12,15 @@ from dgl.data.tree import SST
from
tree_lstm
import
TreeLSTM
from
tree_lstm
import
TreeLSTM
def
batcher
(
dev
):
def
batcher_dev
(
batch
):
batch_trees
=
dgl
.
batch
(
batch
)
return
SSTBatch
(
graph
=
batch_trees
,
mask
=
batch_trees
.
ndata
[
'mask'
].
to
(
device
),
wordid
=
batch_trees
.
ndata
[
'x'
].
to
(
device
),
label
=
batch_trees
.
ndata
[
'y'
].
to
(
device
))
return
batcher_dev
def
main
(
args
):
def
main
(
args
):
np
.
random
.
seed
(
args
.
seed
)
np
.
random
.
seed
(
args
.
seed
)
th
.
manual_seed
(
args
.
seed
)
th
.
manual_seed
(
args
.
seed
)
...
@@ -28,19 +37,19 @@ def main(args):
...
@@ -28,19 +37,19 @@ def main(args):
trainset
=
SST
()
trainset
=
SST
()
train_loader
=
DataLoader
(
dataset
=
trainset
,
train_loader
=
DataLoader
(
dataset
=
trainset
,
batch_size
=
args
.
batch_size
,
batch_size
=
args
.
batch_size
,
collate_fn
=
SST
.
batcher
(
device
),
collate_fn
=
batcher
(
device
),
shuffle
=
True
,
shuffle
=
True
,
num_workers
=
0
)
num_workers
=
0
)
devset
=
SST
(
mode
=
'dev'
)
devset
=
SST
(
mode
=
'dev'
)
dev_loader
=
DataLoader
(
dataset
=
devset
,
dev_loader
=
DataLoader
(
dataset
=
devset
,
batch_size
=
100
,
batch_size
=
100
,
collate_fn
=
SST
.
batcher
(
device
),
collate_fn
=
batcher
(
device
),
shuffle
=
False
,
shuffle
=
False
,
num_workers
=
0
)
num_workers
=
0
)
testset
=
SST
(
mode
=
'test'
)
testset
=
SST
(
mode
=
'test'
)
test_loader
=
DataLoader
(
dataset
=
testset
,
test_loader
=
DataLoader
(
dataset
=
testset
,
batch_size
=
100
,
collate_fn
=
SST
.
batcher
(
device
),
shuffle
=
False
,
num_workers
=
0
)
batch_size
=
100
,
collate_fn
=
batcher
(
device
),
shuffle
=
False
,
num_workers
=
0
)
model
=
TreeLSTM
(
trainset
.
num_vocabs
,
model
=
TreeLSTM
(
trainset
.
num_vocabs
,
args
.
x_size
,
args
.
x_size
,
...
...
python/dgl/data/tree.py
View file @
1bbc885b
...
@@ -148,13 +148,3 @@ class SST(object):
...
@@ -148,13 +148,3 @@ class SST(object):
@
property
@
property
def
num_vocabs
(
self
):
def
num_vocabs
(
self
):
return
len
(
self
.
vocab
)
return
len
(
self
.
vocab
)
@
staticmethod
def
batcher
(
device
):
def
batcher_dev
(
batch
):
batch_trees
=
dgl
.
batch
(
batch
)
return
SSTBatch
(
graph
=
batch_trees
,
mask
=
batch_trees
.
ndata
[
'mask'
].
to
(
device
),
wordid
=
batch_trees
.
ndata
[
'x'
].
to
(
device
),
label
=
batch_trees
.
ndata
[
'y'
].
to
(
device
))
return
batcher_dev
tutorials/models/2_small_graph/3_tree-lstm.py
View file @
1bbc885b
...
@@ -47,6 +47,7 @@ Tree LSTM DGL Tutorial
...
@@ -47,6 +47,7 @@ Tree LSTM DGL Tutorial
import
dgl
import
dgl
from
dgl.data.tree
import
SST
from
dgl.data.tree
import
SST
from
dgl.data
import
SSTBatch
# Each sample in the dataset is a constituency tree. The leaf nodes
# Each sample in the dataset is a constituency tree. The leaf nodes
# represent words. The word is a int value stored in the "x" field.
# represent words. The word is a int value stored in the "x" field.
...
@@ -334,10 +335,19 @@ print(model)
...
@@ -334,10 +335,19 @@ print(model)
optimizer
=
th
.
optim
.
Adagrad
(
model
.
parameters
(),
optimizer
=
th
.
optim
.
Adagrad
(
model
.
parameters
(),
lr
=
lr
,
lr
=
lr
,
weight_decay
=
weight_decay
)
weight_decay
=
weight_decay
)
def
batcher
(
dev
):
def
batcher_dev
(
batch
):
batch_trees
=
dgl
.
batch
(
batch
)
return
SSTBatch
(
graph
=
batch_trees
,
mask
=
batch_trees
.
ndata
[
'mask'
].
to
(
device
),
wordid
=
batch_trees
.
ndata
[
'x'
].
to
(
device
),
label
=
batch_trees
.
ndata
[
'y'
].
to
(
device
))
return
batcher_dev
train_loader
=
DataLoader
(
dataset
=
tiny_sst
,
train_loader
=
DataLoader
(
dataset
=
tiny_sst
,
batch_size
=
5
,
batch_size
=
5
,
collate_fn
=
SST
.
batcher
(
device
),
collate_fn
=
batcher
(
device
),
shuffle
=
False
,
shuffle
=
False
,
num_workers
=
0
)
num_workers
=
0
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment