TypeError: argmax(): argument 'input' (position 1) must be Tensor, not str - bert-language-model

My code was working fine and when I tried to run it today without changing anything I got the following error:
TypeError: argmax(): argument 'input' (position 1) must be Tensor, not str
Would appreciate if help could be provided.
below is the code snippet where I am getting an error. I am using BERT mdoel
start_scores, end_scores = model(torch.tensor([input_ids]), # The tokens representing our input text.
token_type_ids=torch.tensor(
[segment_ids])) # The segment IDs to differentiate question from answer_text
# ======== Reconstruct Answer ========
# Find the tokens with the highest `start` and `end` scores.
answer_start = torch.argmax(start_scores)
answer_end = torch.argmax(end_scores)

In your line of code
start_scores, end_scores = model(torch.tensor([input_ids]),token_type_ids=torch.tensor([segment_ids]))
you have to make it:
start_scores, end_scores = model(torch.tensor([input_ids]),token_type_ids=torch.tensor([segment_ids]), return_dict=False).
It worked for me. I had the same problem.

Related

BertModel transformers outputs string instead of tensor

I'm following this tutorial that codes a sentiment analysis classifier using BERT with the huggingface library and I'm having a very odd behavior. When trying the BERT model with a sample text I get a string instead of the hidden state. This is the code I'm using:
import transformers
from transformers import BertModel, BertTokenizer
print(transformers.__version__)
PRE_TRAINED_MODEL_NAME = 'bert-base-cased'
PATH_OF_CACHE = "/home/mwon/data-mwon/paperChega/src_classificador/data/hugingface"
tokenizer = BertTokenizer.from_pretrained(PRE_TRAINED_MODEL_NAME,cache_dir = PATH_OF_CACHE)
sample_txt = 'When was I last outside? I am stuck at home for 2 weeks.'
encoding_sample = tokenizer.encode_plus(
sample_txt,
max_length=32,
add_special_tokens=True, # Add '[CLS]' and '[SEP]'
return_token_type_ids=False,
padding=True,
truncation = True,
return_attention_mask=True,
return_tensors='pt', # Return PyTorch tensors
)
bert_model = BertModel.from_pretrained(PRE_TRAINED_MODEL_NAME,cache_dir = PATH_OF_CACHE)
last_hidden_state, pooled_output = bert_model(
encoding_sample['input_ids'],
encoding_sample['attention_mask']
)
print([last_hidden_state,pooled_output])
that outputs:
4.0.0
['last_hidden_state', 'pooler_output']
While the answer from Aakash provides a solution to the problem, it does not explain the issue. Since one of the 3.X releases of the transformers library, the models do not return tuples anymore but specific output objects:
o = bert_model(
encoding_sample['input_ids'],
encoding_sample['attention_mask']
)
print(type(o))
print(o.keys())
Output:
transformers.modeling_outputs.BaseModelOutputWithPoolingAndCrossAttentions
odict_keys(['last_hidden_state', 'pooler_output'])
You can return to the previous behavior by adding return_dict=False to get a tuple:
o = bert_model(
encoding_sample['input_ids'],
encoding_sample['attention_mask'],
return_dict=False
)
print(type(o))
Output:
<class 'tuple'>
I do not recommend that, because it is now unambiguous to select a specific part of the output without turning to the documentation as shown in the example below:
o = bert_model(encoding_sample['input_ids'], encoding_sample['attention_mask'], return_dict=False, output_attentions=True, output_hidden_states=True)
print('I am a tuple with {} elements. You do not know what each element presents without checking the documentation'.format(len(o)))
o = bert_model(encoding_sample['input_ids'], encoding_sample['attention_mask'], output_attentions=True, output_hidden_states=True)
print('I am a cool object and you can acces my elements with o.last_hidden_state, o["last_hidden_state"] or even o[0]. My keys are; {} '.format(o.keys()))
Output:
I am a tuple with 4 elements. You do not know what each element presents without checking the documentation
I am a cool object and you can acces my elements with o.last_hidden_state, o["last_hidden_state"] or even o[0]. My keys are; odict_keys(['last_hidden_state', 'pooler_output', 'hidden_states', 'attentions'])
I faced the same issue while learning how to implement Bert. I noticed that using
last_hidden_state, pooled_output = bert_model(encoding_sample['input_ids'], encoding_sample['attention_mask'])
is the issue. Use:
outputs = bert_model(encoding_sample['input_ids'], encoding_sample['attention_mask'])
and extract the last_hidden state using
output[0]
You can refer to the documentation here which tells you what is returned by the BertModel

sunburst.R total frequency count is incorrect

I am plotting a sunburst donut and I cannot figure out why the total is incorrect.
library(sunburstR)
reports <- data.frame(
sequence = c("SVP-VP-Dir-end","SVP-VP-Dir-end","SVP-VP-Dir-end","SVP-VP-Dir-end","SVP-No VP-Dir-end","SVP-No VP-Dir-end","SVP-No VP-Dir-end"),
freq = as.numeric(c("167","60","51","32","5","1","1")))
sunburst(reports, count = TRUE)
It is supposed to be 100% 317 of 317 . Anyone know how to fix this? There is not much documentation on this great package.
Also, I would like it to have a default value in the center of donut.
If there is another way to create an interactive donut using R, please let me know.
Thanks you in advance.
It looks like the default function generating the message in the center of the donut rounds the total value to the nearest ten.
But you can customize this function using the explanation argument of sunburst. Oddly, the customized function (in javascript) must be provided as a string.
Try the following function:
custom.message = "function (d) {
root = d;
while (root.parent) {
root = root.parent
}
p = (100*d.value/root.value).toPrecision(3);
msg = p+' %<br/>'+d.value+' of '+root.value;
return msg;
}"
Now:
sunburst(reports, explanation = custom.message )
will generate the donut displaying exact total values. The count argument is no longer needed, as it is used by the default explanation function.
The value returned by custom.message is html code. As you can see, I've just inserted a line break (<br/>). You can modify the msg return value to further customize the look and feel.

Invalid number of breaks?

I'm trying to make a confidence interval for practice and I keep getting an error referring to:
an 'invalid number of breaks' at hist.default(boot.dist).
I'm pretty sure the problem is somewhere here.
Any advice or help would be very much appreciated at this point.
b=1000
boot.dist = rep(0,b)
for (i in 1:b) {
boot.sample = sample(ACS$Income, replace = TRUE)
boot.dist[i] = mean(boot.sample)
}
hist(boot.dist)
The problem is that ACS$Income is array of NA.
Example, this code will reproduce error exactly like yours:
boot.dist[1:1000]<-NA
hist(boot.dist )
Error in hist.default(boot.dist) : invalid number of 'breaks'

using arulesSequences package : Error in makebin(data, file) : 'sid' invalid

I am using arulesSequences package in R. The documentation is too little for the type of data that read_baskets function receives. I guess data should be in text (.txt) format. Column names are: "sequenceID", "eventID", "SIZE" and "items". My data has about 200,000 rows and looks like following in z.txt file:
1,1364,3,{12,17,19}
1,1130,4,{14,17,21,23}
1,1173,3,{19,23,9}
1,98,5,{14,15,2,21,5}
2,1878,4,{1,10,14,3}
2,1878,13,{1,12,14,15,16,17,18,19,2,21,24,25,5}
2,1878,1,{2}
I tried to use:
x <- read_baskets("z.txt", sep = ",",info =c("sequenceID","eventID","SIZE"))
s <- cspade(x,parameter = list(support = 0.001),control = list(verbose =
TRUE),tmpdir = tempdir())
but I get this error :
Error in makebin(data, file) : 'sid' invalid
The combination of sequenceID and eventID must be unique.
Otherwise you'll get one of these errors:
Error in makebin(data, file) : 'sid' invalid
Error in makebin(data, file) : 'eid' invalid
This implies further that the items in your .txt file (per sequenceID, eventID combination) must be in the same row and (possibly) be separated with the same separator as the rest of the .txt file. Therefore, the item column should be the last column.
Hope this helps!
Ok I found the problem, and I'm posting it in case that some one has the same problem. The problem is both SequenceID and eventID (first and second columns must be ordered blockwise. package mentions this point, but I only ordered the first column.

Undefined columns selected

I've looked at some answers that were already posted here and was not quite able to find one that would help with my particular situation. I'm new to R so bear with me.
rm(list = ls())
dat=read.csv('C:\\Users\\Casandra\\Downloads\\roaches.csv',as.is=T)
ndata=nrow(dat)
param=read.csv('C:\\Users\\Casandra\\Downloads\\roaches+posterior+after+burnin.csv',as.is=T)
param.med=apply(param,2,mean)
yhat=param.med[1]+param.med[2]*dat$x
png('pred distribution.png')
plot(dat$x,dat$y,xlim=c(0,1),ylim=c(0,1.5),xlab='Covariate x',col='grey',ylab='')
lines(dat$x,yhat,col='red')
nsim=nrow(param)
yhat1=yhat2=matrix(NA,ndata,3)
for (i in 1:ndata){
media=param[,1]+param[,2]*dat$x[i]
yhat1[i,]=quantile(media,c(0.025,0.5,0.975))
tmp=rnorm(nsim,mean=media,sd=sqrt(param[,3]))
yhat2[i,]=quantile(tmp,c(0.025,0.5,0.975))
}
lines(dat$x,yhat1[,1],col='orange',lty=2)
lines(dat$x,yhat1[,3],col='orange',lty=2)
lines(dat$x,yhat2[,1],col='grey',lty=2)
lines(dat$x,yhat2[,3],col='grey',lty=2)
dev.off()
Above is my code and this is the error I am receiving
Error in[.data.frame(param, , 3) : undefined columns selected
The dat data set has 2 columns and the param data set has two as well.
Can anyone point me in the right direction for fixing my code?
Data sets are below:
dat: https://drive.google.com/file/d/0B9MIgQ2O0SHnakRzZ2p2bDhIZ2c/edit?usp=sharing
param: https://drive.google.com/file/d/0B9MIgQ2O0SHnTTVMU0E2TTRDR2M/edit?usp=sharing

Resources