Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_38786831
tcn
提交
bba34b46
T
tcn
项目概览
weixin_38786831
/
tcn
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
tcn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
bba34b46
编写于
3月 08, 2019
作者:
S
Shaojie Bai
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
upgrade repo to pytorch 1.0
上级
8845f88f
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
58 addition
and
55 deletion
+58
-55
TCN/adding_problem/add_test.py
TCN/adding_problem/add_test.py
+10
-8
TCN/char_cnn/char_cnn_test.py
TCN/char_cnn/char_cnn_test.py
+17
-23
TCN/copy_memory/copymem_test.py
TCN/copy_memory/copymem_test.py
+7
-5
TCN/lambada_language/lambada_test.py
TCN/lambada_language/lambada_test.py
+4
-4
TCN/mnist_pixel/data/mnist/processed/README.md
TCN/mnist_pixel/data/mnist/processed/README.md
+1
-0
TCN/mnist_pixel/pmnist_test.py
TCN/mnist_pixel/pmnist_test.py
+5
-3
TCN/poly_music/music_test.py
TCN/poly_music/music_test.py
+9
-7
TCN/word_cnn/word_cnn_test.py
TCN/word_cnn/word_cnn_test.py
+5
-5
未找到文件。
TCN/adding_problem/add_test.py
浏览文件 @
bba34b46
...
...
@@ -2,6 +2,8 @@ import torch
import
argparse
import
torch.optim
as
optim
import
torch.nn.functional
as
F
import
sys
sys
.
path
.
append
(
"../../"
)
from
TCN.adding_problem.model
import
TCN
from
TCN.adding_problem.utils
import
data_generator
...
...
@@ -73,8 +75,8 @@ def train(epoch):
model
.
train
()
batch_idx
=
1
total_loss
=
0
for
i
in
range
(
0
,
X_train
.
size
(
)[
0
]
,
batch_size
):
if
i
+
batch_size
>
X_train
.
size
(
)[
0
]
:
for
i
in
range
(
0
,
X_train
.
size
(
0
)
,
batch_size
):
if
i
+
batch_size
>
X_train
.
size
(
0
)
:
x
,
y
=
X_train
[
i
:],
Y_train
[
i
:]
else
:
x
,
y
=
X_train
[
i
:(
i
+
batch_size
)],
Y_train
[
i
:(
i
+
batch_size
)]
...
...
@@ -83,16 +85,16 @@ def train(epoch):
loss
=
F
.
mse_loss
(
output
,
y
)
loss
.
backward
()
if
args
.
clip
>
0
:
torch
.
nn
.
utils
.
clip_grad_norm
(
model
.
parameters
(),
args
.
clip
)
torch
.
nn
.
utils
.
clip_grad_norm
_
(
model
.
parameters
(),
args
.
clip
)
optimizer
.
step
()
batch_idx
+=
1
total_loss
+=
loss
.
data
[
0
]
total_loss
+=
loss
.
item
()
if
batch_idx
%
args
.
log_interval
==
0
:
cur_loss
=
total_loss
/
args
.
log_interval
processed
=
min
(
i
+
batch_size
,
X_train
.
size
(
)[
0
]
)
processed
=
min
(
i
+
batch_size
,
X_train
.
size
(
0
)
)
print
(
'Train Epoch: {:2d} [{:6d}/{:6d} ({:.0f}%)]
\t
Learning rate: {:.4f}
\t
Loss: {:.6f}'
.
format
(
epoch
,
processed
,
X_train
.
size
(
)[
0
],
100.
*
processed
/
X_train
.
size
()[
0
]
,
lr
,
cur_loss
))
epoch
,
processed
,
X_train
.
size
(
0
),
100.
*
processed
/
X_train
.
size
(
0
)
,
lr
,
cur_loss
))
total_loss
=
0
...
...
@@ -100,8 +102,8 @@ def evaluate():
model
.
eval
()
output
=
model
(
X_test
)
test_loss
=
F
.
mse_loss
(
output
,
Y_test
)
print
(
'
\n
Test set: Average loss: {:.6f}
\n
'
.
format
(
test_loss
.
data
[
0
]
))
return
test_loss
.
data
[
0
]
print
(
'
\n
Test set: Average loss: {:.6f}
\n
'
.
format
(
test_loss
.
item
()
))
return
test_loss
.
item
()
for
ep
in
range
(
1
,
epochs
+
1
):
...
...
TCN/char_cnn/char_cnn_test.py
浏览文件 @
bba34b46
...
...
@@ -88,21 +88,22 @@ def evaluate(source):
total_loss
=
0
count
=
0
source_len
=
source
.
size
(
1
)
for
batch
,
i
in
enumerate
(
range
(
0
,
source_len
-
1
,
args
.
validseqlen
)):
if
i
+
args
.
seq_len
-
args
.
validseqlen
>=
source_len
:
continue
inp
,
target
=
get_batch
(
source
,
i
,
args
)
output
=
model
(
inp
)
eff_history
=
args
.
seq_len
-
args
.
validseqlen
final_output
=
output
[:,
eff_history
:].
contiguous
().
view
(
-
1
,
n_characters
)
final_target
=
target
[:,
eff_history
:].
contiguous
().
view
(
-
1
)
loss
=
criterion
(
final_output
,
final_target
)
with
torch
.
no_grad
():
for
batch
,
i
in
enumerate
(
range
(
0
,
source_len
-
1
,
args
.
validseqlen
)):
if
i
+
args
.
seq_len
-
args
.
validseqlen
>=
source_len
:
continue
inp
,
target
=
get_batch
(
source
,
i
,
args
)
output
=
model
(
inp
)
eff_history
=
args
.
seq_len
-
args
.
validseqlen
final_output
=
output
[:,
eff_history
:].
contiguous
().
view
(
-
1
,
n_characters
)
final_target
=
target
[:,
eff_history
:].
contiguous
().
view
(
-
1
)
loss
=
criterion
(
final_output
,
final_target
)
total_loss
+=
loss
.
data
*
final_output
.
size
(
0
)
count
+=
final_output
.
size
(
0
)
total_loss
+=
loss
.
data
*
final_output
.
size
(
0
)
count
+=
final_output
.
size
(
0
)
val_loss
=
total_loss
[
0
]
/
count
*
1.0
return
val_loss
val_loss
=
total_loss
.
item
()
/
count
*
1.0
return
val_loss
def
train
(
epoch
):
...
...
@@ -125,12 +126,12 @@ def train(epoch):
loss
.
backward
()
if
args
.
clip
>
0
:
torch
.
nn
.
utils
.
clip_grad_norm
(
model
.
parameters
(),
args
.
clip
)
torch
.
nn
.
utils
.
clip_grad_norm
_
(
model
.
parameters
(),
args
.
clip
)
optimizer
.
step
()
total_loss
+=
loss
.
data
total_loss
+=
loss
.
item
()
if
batch_idx
%
args
.
log_interval
==
0
and
batch_idx
>
0
:
cur_loss
=
total_loss
[
0
]
/
args
.
log_interval
cur_loss
=
total_loss
/
args
.
log_interval
losses
.
append
(
cur_loss
)
elapsed
=
time
.
time
()
-
start_time
print
(
'| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.5f} | ms/batch {:5.2f} | '
...
...
@@ -140,13 +141,6 @@ def train(epoch):
total_loss
=
0
start_time
=
time
.
time
()
# if batch % (200 * args.log_interval) == 0 and batch > 0:
# vloss = evaluate(val_data)
# print('-' * 89)
# print('| In epoch {:3d} | valid loss {:5.3f} | '
# 'valid bpc {:8.3f}'.format(epoch, vloss, vloss / math.log(2)))
# model.train()
return
sum
(
losses
)
*
1.0
/
len
(
losses
)
...
...
TCN/copy_memory/copymem_test.py
浏览文件 @
bba34b46
...
...
@@ -4,6 +4,8 @@ import torch.nn as nn
import
torch.optim
as
optim
from
torch.autograd
import
Variable
import
numpy
as
np
import
sys
sys
.
path
.
append
(
"../../"
)
from
TCN.copy_memory.utils
import
data_generator
from
TCN.copy_memory.model
import
TCN
import
time
...
...
@@ -89,8 +91,8 @@ def evaluate():
correct
=
pred
.
eq
(
test_y
.
data
.
view_as
(
pred
)).
cpu
().
sum
()
counter
=
out
.
view
(
-
1
,
n_classes
).
size
(
0
)
print
(
'
\n
Test set: Average loss: {:.8f} | Accuracy: {:.4f}
\n
'
.
format
(
loss
.
data
[
0
]
,
100.
*
correct
/
counter
))
return
loss
.
data
[
0
]
loss
.
item
()
,
100.
*
correct
/
counter
))
return
loss
.
item
()
def
train
(
ep
):
...
...
@@ -114,10 +116,10 @@ def train(ep):
correct
+=
pred
.
eq
(
y
.
data
.
view_as
(
pred
)).
cpu
().
sum
()
counter
+=
out
.
view
(
-
1
,
n_classes
).
size
(
0
)
if
args
.
clip
>
0
:
torch
.
nn
.
utils
.
clip_grad_norm
(
model
.
parameters
(),
args
.
clip
)
torch
.
nn
.
utils
.
clip_grad_norm
_
(
model
.
parameters
(),
args
.
clip
)
loss
.
backward
()
optimizer
.
step
()
total_loss
+=
loss
total_loss
+=
loss
.
item
()
if
batch_idx
>
0
and
batch_idx
%
args
.
log_interval
==
0
:
avg_loss
=
total_loss
/
args
.
log_interval
...
...
@@ -125,7 +127,7 @@ def train(ep):
print
(
'| Epoch {:3d} | {:5d}/{:5d} batches | lr {:2.5f} | ms/batch {:5.2f} | '
'loss {:5.8f} | accuracy {:5.4f}'
.
format
(
ep
,
batch_idx
,
n_train
//
batch_size
+
1
,
args
.
lr
,
elapsed
*
1000
/
args
.
log_interval
,
avg_loss
.
data
[
0
]
,
100.
*
correct
/
counter
))
avg_loss
,
100.
*
correct
/
counter
))
start_time
=
time
.
time
()
total_loss
=
0
correct
=
0
...
...
TCN/lambada_language/lambada_test.py
浏览文件 @
bba34b46
...
...
@@ -99,7 +99,7 @@ def evaluate(data_source):
loss
=
criterion
(
final_output
,
final_target
)
total_loss
+=
loss
.
data
processed_data_size
+=
1
return
total_loss
[
0
]
/
processed_data_size
return
total_loss
.
item
()
/
processed_data_size
def
train
():
...
...
@@ -121,12 +121,12 @@ def train():
loss
=
criterion
(
final_output
,
final_target
)
loss
.
backward
()
if
args
.
clip
>
0
:
torch
.
nn
.
utils
.
clip_grad_norm
(
model
.
parameters
(),
args
.
clip
)
torch
.
nn
.
utils
.
clip_grad_norm
_
(
model
.
parameters
(),
args
.
clip
)
optimizer
.
step
()
total_loss
+=
loss
.
data
total_loss
+=
loss
.
item
()
if
batch_idx
%
args
.
log_interval
==
0
and
batch_idx
>
0
:
cur_loss
=
total_loss
[
0
]
/
args
.
log_interval
cur_loss
=
total_loss
/
args
.
log_interval
elapsed
=
time
.
time
()
-
start_time
print
(
'| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.5f} | ms/batch {:5.5f} | '
'loss {:5.2f} | ppl {:8.2f}'
.
format
(
...
...
TCN/mnist_pixel/data/mnist/processed/README.md
0 → 100644
浏览文件 @
bba34b46
Your
`training.pt`
and
`test.pt`
will be saved here.
\ No newline at end of file
TCN/mnist_pixel/pmnist_test.py
浏览文件 @
bba34b46
...
...
@@ -2,6 +2,8 @@ import torch
from
torch.autograd
import
Variable
import
torch.optim
as
optim
import
torch.nn.functional
as
F
import
sys
sys
.
path
.
append
(
"../../"
)
from
TCN.mnist_pixel.utils
import
data_generator
from
TCN.mnist_pixel.model
import
TCN
import
numpy
as
np
...
...
@@ -80,14 +82,14 @@ def train(ep):
loss
=
F
.
nll_loss
(
output
,
target
)
loss
.
backward
()
if
args
.
clip
>
0
:
torch
.
nn
.
utils
.
clip_grad_norm
(
model
.
parameters
(),
args
.
clip
)
torch
.
nn
.
utils
.
clip_grad_norm
_
(
model
.
parameters
(),
args
.
clip
)
optimizer
.
step
()
train_loss
+=
loss
steps
+=
seq_length
if
batch_idx
>
0
and
batch_idx
%
args
.
log_interval
==
0
:
print
(
'Train Epoch: {} [{}/{} ({:.0f}%)]
\t
Loss: {:.6f}
\t
Steps: {}'
.
format
(
ep
,
batch_idx
*
batch_size
,
len
(
train_loader
.
dataset
),
100.
*
batch_idx
/
len
(
train_loader
),
train_loss
.
data
[
0
]
/
args
.
log_interval
,
steps
))
100.
*
batch_idx
/
len
(
train_loader
),
train_loss
.
item
()
/
args
.
log_interval
,
steps
))
train_loss
=
0
...
...
@@ -103,7 +105,7 @@ def test():
data
=
data
[:,
:,
permute
]
data
,
target
=
Variable
(
data
,
volatile
=
True
),
Variable
(
target
)
output
=
model
(
data
)
test_loss
+=
F
.
nll_loss
(
output
,
target
,
size_average
=
False
).
data
[
0
]
test_loss
+=
F
.
nll_loss
(
output
,
target
,
size_average
=
False
).
item
()
pred
=
output
.
data
.
max
(
1
,
keepdim
=
True
)[
1
]
correct
+=
pred
.
eq
(
target
.
data
.
view_as
(
pred
)).
cpu
().
sum
()
...
...
TCN/poly_music/music_test.py
浏览文件 @
bba34b46
...
...
@@ -3,6 +3,8 @@ import torch
import
torch.nn
as
nn
from
torch.autograd
import
Variable
import
torch.optim
as
optim
import
sys
sys
.
path
.
append
(
"../../"
)
from
TCN.poly_music.model
import
TCN
from
TCN.poly_music.utils
import
data_generator
import
numpy
as
np
...
...
@@ -61,7 +63,7 @@ lr = args.lr
optimizer
=
getattr
(
optim
,
args
.
optim
)(
model
.
parameters
(),
lr
=
lr
)
def
evaluate
(
X_data
):
def
evaluate
(
X_data
,
name
=
'Eval'
):
model
.
eval
()
eval_idx_list
=
np
.
arange
(
len
(
X_data
),
dtype
=
"int32"
)
total_loss
=
0.0
...
...
@@ -74,10 +76,10 @@ def evaluate(X_data):
output
=
model
(
x
.
unsqueeze
(
0
)).
squeeze
(
0
)
loss
=
-
torch
.
trace
(
torch
.
matmul
(
y
,
torch
.
log
(
output
).
float
().
t
())
+
torch
.
matmul
((
1
-
y
),
torch
.
log
(
1
-
output
).
float
().
t
()))
total_loss
+=
loss
.
data
[
0
]
total_loss
+=
loss
.
item
()
count
+=
output
.
size
(
0
)
eval_loss
=
total_loss
/
count
print
(
"Validation/Test
loss: {:.5f}"
.
format
(
eval_loss
))
print
(
name
+
"
loss: {:.5f}"
.
format
(
eval_loss
))
return
eval_loss
...
...
@@ -97,11 +99,11 @@ def train(ep):
output
=
model
(
x
.
unsqueeze
(
0
)).
squeeze
(
0
)
loss
=
-
torch
.
trace
(
torch
.
matmul
(
y
,
torch
.
log
(
output
).
float
().
t
())
+
torch
.
matmul
((
1
-
y
),
torch
.
log
(
1
-
output
).
float
().
t
()))
total_loss
+=
loss
.
data
[
0
]
total_loss
+=
loss
.
item
()
count
+=
output
.
size
(
0
)
if
args
.
clip
>
0
:
torch
.
nn
.
utils
.
clip_grad_norm
(
model
.
parameters
(),
args
.
clip
)
torch
.
nn
.
utils
.
clip_grad_norm
_
(
model
.
parameters
(),
args
.
clip
)
loss
.
backward
()
optimizer
.
step
()
if
idx
>
0
and
idx
%
args
.
log_interval
==
0
:
...
...
@@ -117,8 +119,8 @@ if __name__ == "__main__":
model_name
=
"poly_music_{0}.pt"
.
format
(
args
.
data
)
for
ep
in
range
(
1
,
args
.
epochs
+
1
):
train
(
ep
)
vloss
=
evaluate
(
X_valid
)
tloss
=
evaluate
(
X_test
)
vloss
=
evaluate
(
X_valid
,
name
=
'Validation'
)
tloss
=
evaluate
(
X_test
,
name
=
'Test'
)
if
vloss
<
best_vloss
:
with
open
(
model_name
,
"wb"
)
as
f
:
torch
.
save
(
model
,
f
)
...
...
TCN/word_cnn/word_cnn_test.py
浏览文件 @
bba34b46
...
...
@@ -106,9 +106,9 @@ def evaluate(data_source):
loss
=
criterion
(
final_output
,
final_target
)
# Note that we don't add TAR loss here
total_loss
+=
(
data
.
size
(
1
)
-
eff_history
)
*
loss
.
data
total_loss
+=
(
data
.
size
(
1
)
-
eff_history
)
*
loss
.
item
()
processed_data_size
+=
data
.
size
(
1
)
-
eff_history
return
total_loss
[
0
]
/
processed_data_size
return
total_loss
/
processed_data_size
def
train
():
...
...
@@ -134,13 +134,13 @@ def train():
loss
.
backward
()
if
args
.
clip
>
0
:
torch
.
nn
.
utils
.
clip_grad_norm
(
model
.
parameters
(),
args
.
clip
)
torch
.
nn
.
utils
.
clip_grad_norm
_
(
model
.
parameters
(),
args
.
clip
)
optimizer
.
step
()
total_loss
+=
loss
.
data
total_loss
+=
loss
.
item
()
if
batch_idx
%
args
.
log_interval
==
0
and
batch_idx
>
0
:
cur_loss
=
total_loss
[
0
]
/
args
.
log_interval
cur_loss
=
total_loss
/
args
.
log_interval
elapsed
=
time
.
time
()
-
start_time
print
(
'| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.5f} | ms/batch {:5.5f} | '
'loss {:5.2f} | ppl {:8.2f}'
.
format
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录