Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pinyin2hanzi
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
邵子睿(21软)
pinyin2hanzi
Commits
25cce464
Commit
25cce464
authored
Jan 17, 2022
by
szr712
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加汉字logits拼接函数
parent
d8b00dd6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
0 deletions
+11
-0
translate_file2.py
translate_file2.py
+11
-0
No files found.
translate_file2.py
View file @
25cce464
...
...
@@ -29,6 +29,16 @@ def get_yunmus(file_path):
return
yunmus
def
get_hanzi_logits
(
logits
,
SRC
,
TRG
,
opt
):
preds
=
torch
.
argmax
(
logits
,
dim
=-
1
)
result
=
torch
.
randn
(
1
,
logits
.
shape
[
2
])
.
cuda
()
for
i
,
tok
in
enumerate
(
preds
[
0
][:]):
if
'
\u4e00
'
<=
TRG
.
vocab
.
itos
[
tok
]
<=
'
\u9fa5
'
:
# 判断是否是中文
result
=
torch
.
cat
((
result
,
logits
[
0
][
i
:
i
+
1
]),
dim
=
0
)
result
=
result
.
unsqueeze
(
dim
=
0
)
return
result
def
get_result
(
src
,
model
,
SRC
,
TRG
,
opt
):
src_mask
=
(
src
!=
SRC
.
vocab
.
stoi
[
'<pad>'
])
.
unsqueeze
(
-
2
)
output
=
model
(
src
,
src_mask
)
...
...
@@ -80,6 +90,7 @@ def get_result(src, model, SRC, TRG, opt):
return
''
.
join
(
result
)
.
replace
(
"_"
,
""
)
.
replace
(
" "
,
""
)
else
:
# output=get_hanzi_logits(output, SRC, TRG, opt)
preds
=
torch
.
argmax
(
output
,
dim
=-
1
)
return
''
.
join
([
TRG
.
vocab
.
itos
[
tok
]
for
tok
in
preds
[
0
][:]
if
tok
.
item
()
!=
0
])
.
replace
(
"_"
,
""
)
.
replace
(
" "
,
""
)
# return ' '.join([TRG.vocab.itos[tok] for tok in preds[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