forked from MAGICS-LAB/DNABERT_2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a5dc41
commit 105bb1c
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Checks if the build works, | ||
# by installing the requirements | ||
# and then running the example code | ||
|
||
name: Check build | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
jobs: | ||
check_build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-python@v2 | ||
with: | ||
python-version: 3.8 | ||
|
||
- name: install required packages | ||
run: python3 -m pip install -r requirements.txt | ||
|
||
- name: run example code shown in README | ||
run: python3 .github/workflows/example_huggingface_v4_28.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from transformers.models.bert.configuration_bert import BertConfig | ||
|
||
config = BertConfig.from_pretrained("zhihan1996/DNABERT-2-117M") | ||
model = AutoModel.from_pretrained("zhihan1996/DNABERT-2-117M", trust_remote_code=True, config=config) | ||
|
||
dna = "ACGTAGCATCGGATCTATCTATCGACACTTGGTTATCGATCTACGAGCATCTCGTTAGC" | ||
inputs = tokenizer(dna, return_tensors = 'pt')["input_ids"] | ||
hidden_states = model(inputs)[0] # [1, sequence_length, 768] | ||
|
||
# embedding with mean pooling | ||
embedding_mean = torch.mean(hidden_states[0], dim=0) | ||
print(embedding_mean.shape) # expect to be 768 | ||
|
||
# embedding with max pooling | ||
embedding_max = torch.max(hidden_states[0], dim=0)[0] | ||
print(embedding_max.shape) # expect to be 768 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import torch | ||
from transformers import AutoTokenizer, AutoModel | ||
|
||
tokenizer = AutoTokenizer.from_pretrained("zhihan1996/DNABERT-2-117M", trust_remote_code=True) | ||
model = AutoModel.from_pretrained("zhihan1996/DNABERT-2-117M", trust_remote_code=True) | ||
|
||
dna = "ACGTAGCATCGGATCTATCTATCGACACTTGGTTATCGATCTACGAGCATCTCGTTAGC" | ||
inputs = tokenizer(dna, return_tensors = 'pt')["input_ids"] | ||
hidden_states = model(inputs)[0] # [1, sequence_length, 768] | ||
|
||
# embedding with mean pooling | ||
embedding_mean = torch.mean(hidden_states[0], dim=0) | ||
print(embedding_mean.shape) # expect to be 768 | ||
|
||
# embedding with max pooling | ||
embedding_max = torch.max(hidden_states[0], dim=0)[0] | ||
print(embedding_max.shape) # expect to be 768 |