Could you please assist me in solving the following error: 'TokenClassifierOutput' object has no attribute 'detach' The error persists even when modifying the code like output = model(input_ids, token_type_ids=None, attention_mask=input_mask,)
logits = output.loss.detach().cpu().numpy()
AttributeError Traceback (most recent call last)
<ipython-input-49-3e5217186317> in <module>
13 logits = model(input_ids, token_type_ids=None, attention_mask=input_mask,)
14
---> 15 logits = logits.detach().cpu().numpy()
16 logits = [list(p) for p in np.argmax(logits, axis=2)]
17
AttributeError: 'TokenClassifierOutput' object has no attribute 'detach'
model.eval()
y_true = []
y_pred = []
eval_loss, eval_accuracy = 0, 0
nb_eval_steps, nb_eval_examples = 0, 0
for batch in valid_dataloader:
batch = tuple(t.to(device) for t in batch)
input_ids, input_mask, label_ids = batch
with torch.no_grad():
logits = model(input_ids, token_type_ids=None, attention_mask=input_mask,)
logits = logits.detach().cpu().numpy()
logits = [list(p) for p in np.argmax(logits, axis=2)]
label_ids = label_ids.to('cpu').numpy()
input_mask = input_mask.to('cpu').numpy()
In line 13, it seems that the variable logits returned by the model() function is an instance of a python Class named TokenClassifierOutput.
You can add print(logits.__dict__) after line 13 to figure out what inside this instance.
Anyaway, if you run logits.detach() method, logits should be an instance of the pytorch.Tensor object.
Related
The following is my Sentiment Analyser:
from transformers import DistilBertTokenizer, DistilBertModel
PRE_TRAINED_MODEL_NAME = 'distilbert-base-cased'
db_model = DistilBertModel.from_pretrained(PRE_TRAINED_MODEL_NAME, return_dict = False)
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-cased', return_dict = False, return_tensors="pt")
class SentimentClassifier(nn.Module):
def __init__(self, n_classes):
super(SentimentClassifier, self).__init__()
self.db = DistilBertModel.from_pretrained(PRE_TRAINED_MODEL_NAME, return_dict = False)
self.drop = nn.Dropout(p=0.3)
self.out = nn.Linear(self.db.config.hidden_size, n_classes)
def forward(self, input_ids, attention_mask):
pooled_output = self.db(
input_ids=input_ids,
attention_mask=attention_mask
)
output = self.drop(pooled_output)
return self.out(output)
When I try to run :
F.softmax(model(input_ids, attention_mask), dim=1)
I am getting the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-191-96f6522cbd43> in <module>
----> 1 F.softmax(model(input_ids, attention_mask), dim=1)
4 frames
/usr/local/lib/python3.8/dist-packages/torch/nn/functional.py in dropout(input, p, training, inplace)
1250 if p < 0.0 or p > 1.0:
1251 raise ValueError("dropout probability has to be between 0 and 1, " "but got {}".format(p))
-> 1252 return _VF.dropout_(input, p, training) if inplace else _VF.dropout(input, p, training)
1253
1254
TypeError: dropout(): argument 'input' (position 1) must be Tensor, not tuple
I applied the solution used in BERT model (ie, return_dict = False and return_tensor = 'pt') and it is still running into this error.
Any solution to this would be highly appreciated.
I'm having an issue getting Inception V3 to work as the feature extractor with a binary classifier in Pytorch. I update the primary and auxiliary nets in Inception to have the binary class (as done in https://pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html)
but I'm getting an error
#Parameters for Inception V3
num_classes= 2
model_ft = models.inception_v3(pretrained=True)
# set_parameter_requires_grad(model_ft, feature_extract)
#handle auxilliary net
num_ftrs = model_ft.AuxLogits.fc.in_features
model_ft.AuxLogits.fc = nn.Linear(num_ftrs, num_classes)
#handle primary net
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs,num_classes)
# input_size = 299
#simulate data input
x = torch.rand([64, 3, 299, 299])
#create model with inception backbone
backbone = model_ft
num_filters = backbone.fc.in_features
layers = list(backbone.children())[:-1]
feature_extractor = nn.Sequential(*layers)
# use the pretrained model to classify damage 2 classes
num_target_classes = 2
classifier = nn.Linear(num_filters, num_target_classes)
feature_extractor.eval()
with torch.no_grad():
representations = feature_extractor(x).flatten(1)
x = classifier(representations)
But Im getting the error
RuntimeError Traceback (most recent call last)
<ipython-input-54-c2be64b8a99e> in <module>()
11 feature_extractor.eval()
12 with torch.no_grad():
---> 13 representations = feature_extractor(x)
14 x = classifier(representations)
9 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
442 _pair(0), self.dilation, self.groups)
443 return F.conv2d(input, weight, bias, self.stride,
--> 444 self.padding, self.dilation, self.groups)
445
446 def forward(self, input: Tensor) -> Tensor:
RuntimeError: Expected 3D (unbatched) or 4D (batched) input to conv2d, but got input of size: [64, 2]
before I updated the class to 2 (when it was 1000) I was getting the same error but with [64, 1000]. This method of creating a backbone and adding a classifier worked for Resnet but not here. I think it's because of the auxiliary net structure but not sure how to update it to deal with the dual output? Thanks
Inheriting feature_extracture by children function at line layers = list(backbone.children())[:-1] will bring the module from backbone to feature_extracture only, not the operation in forward function.
Let's take a look at the code below:
class Example(torch.nn.Module):
def __init__(self):
super().__init__()
self.avg = torch.nn.AdaptiveAvgPool2d((1,1))
self.linear = torch.nn.Linear(10, 1)
def forward(self, x):
out = self.avg(x)
out = out.squeeze()
out = self.linear(out)
return out
x = torch.randn(5, 10, 12, 12)
model = Example()
y = model(x) # work well
new_model = torch.nn.Sequential(*list(model.children()))
y = new_model(x) # error
Module model and new_model have the same blocks but not the same way of working. In new_module, the output from the pooling layer is not squeezed yet, so the shape of linear input is violate its assumption which causes the error.
In your case, the last two comments are redundant and that's why it returns the error, you did create a new fc in the InceptionV3 module at line model_ft.fc = nn.Linear(num_ftrs,num_classes). Therefore, replace the last one as the code below should work fine:
with torch.no_grad():
x = model_ft(x)
Im studying objective detection code with efficientdet-pytorch at the first time.
I used pretrained Weight model as reference:
https://www.kaggle.com/shonenkov/inference-efficientdet
https://github.com/toandaominh1997/EfficientDet.Pytorch
https://github.com/toandaominh1997/EfficientDet.Pytorch
I tried to check efficientdet model's output...
from effdet import get_efficientdet_config, EfficientDet, DetBenchTrain
from effdet.efficientdet import HeadNet
#load sample efficientdet code
config = get_efficientdet_config('tf_efficientdet_d0')
config.image_size = [512,512]
config.norm_kwargs=dict(eps=.001, momentum=.01)
net = EfficientDet(config, pretrained_backbone=False)
checkpoint = torch.load('efficientdet_d0-d92fd44f.pth')
net.load_state_dict(checkpoint)
#net.reset_head(num_classes=1)
#net.class_net = HeadNet(config, num_outputs=config.num_classes)
net=DetBenchTrain(net, config)
print("Loaded pretrained weights")
#>>>Loaded pretrained weights"
#img.shape:[3,512,512]
net.eval()
with torch.no_grad():
detected=net(img.unsqueeze(0))
#error of target is below.
TypeError: forward() missing 1 required positional argument: 'target'
And refering to https://www.kaggle.com/shonenkov/inference-efficientdet
I tried below code.
def make_predictions(images, score_threshold=0.22):
predictions = []
with torch.no_grad():
det = net(images, torch.tensor([1]*images.shape[0]).float())
print(det.shape)
for i in range(images.shape[0]):
boxes = det[i].detach().cpu().numpy()[:,:4]
scores = det[i].detach().cpu().numpy()[:,4]
indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
predictions.append({
'boxes': boxes[indexes],
'scores': scores[indexes],
})
return [predictions]
output=make_predictions(img.unsqueeze(0))
#error is below...
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-412-e81ace805280> in <module>
----> 1 output=make_predictions(img.unsqueeze(0))
<ipython-input-407-a6aab7dca874> in make_predictions(images, score_threshold)
3 predictions = []
4 with torch.no_grad():
----> 5 det = net(images, torch.tensor([1]*images.shape[0]).float())
6 print(det.shape)
7 for i in range(images.shape[0]):
/opt/anaconda3/envs/yohenv/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
/opt/anaconda3/envs/yohenv/lib/python3.7/site-packages/effdet/bench.py in forward(self, x, target)
117 else:
118 cls_targets, box_targets, num_positives = self.anchor_labeler.batch_label_anchors(
--> 119 target['bbox'], target['cls'])
120
121 loss, class_loss, box_loss = self.loss_fn(class_out, box_out, cls_targets, box_targets, num_positives)
IndexError: too many indices for tensor of dimension 1
When inference with TestData ,How do I set parameter of target?
Sorry for the inconvenience,Can you give me advice?
I am working on an RL problem and I created a class to initialize the model and other parameters. The code is as follows:
class Agent:
def __init__(self, state_size, is_eval=False, model_name=""):
self.state_size = state_size
self.action_size = 20 # measurement, CNOT, bit-flip
self.memory = deque(maxlen=1000)
self.inventory = []
self.model_name = model_name
self.is_eval = is_eval
self.done = False
self.gamma = 0.95
self.epsilon = 1.0
self.epsilon_min = 0.01
self.epsilon_decay = 0.995
def model(self):
model = Sequential()
model.add(Dense(units=16, input_dim=self.state_size, activation="relu"))
model.add(Dense(units=32, activation="relu"))
model.add(Dense(units=8, activation="relu"))
model.add(Dense(self.action_size, activation="softmax"))
model.compile(loss="categorical_crossentropy", optimizer=Adam(lr=0.003))
return model
def act(self, state):
options = self.model.predict(state)
return np.argmax(options[0]), options
I want to run it for only one iteration, hence I create an object and I pass a vector of length 16 like this:
agent = Agent(density.flatten().shape)
state = density.flatten()
action, probs = agent.act(state)
However, I get the following error:
AttributeError Traceback (most recent call last) <ipython-input-14-4f0ff0c40f49> in <module>
----> 1 action, probs = agent.act(state)
<ipython-input-10-562aaf040521> in act(self, state)
39 # return random.randrange(self.action_size)
40 # model = self.model()
---> 41 options = self.model.predict(state)
42 return np.argmax(options[0]), options
43
AttributeError: 'function' object has no attribute 'predict'
What's the issue? I checked some other people's codes as well, like this and I think mine is also very similar.
Let me know.
EDIT:
I changed the argument in Dense from input_dim to input_shape and self.model.predict(state) to self.model().predict(state).
Now when I run the NN for one input data of shape (16,1), I get the following error:
ValueError: Error when checking input: expected dense_1_input to have
3 dimensions, but got array with shape (16, 1)
And when I run it with shape (1,16), I get the following error:
ValueError: Error when checking input: expected dense_1_input to have
3 dimensions, but got array with shape (1, 16)
What should I do in this case?
in last code block,
def act(self, state):
options = self.model.predict(state)
return np.argmax(options[0]), options
self.model is a function which is returning a model, it should be self.model().predict(state)
I used np.reshape. So in this case, I did
density_test = np.reshape(density.flatten(), (1,1,16))
and the network gave the output.
I need to use Bagging method for LSTM, training on Time-Series data. I have defined the model base and use the KerasRegressor to link to scikit-learn. But have AttributeError: 'KerasRegressor' object has no attribute 'loss'. How can I fix it?
Update: I have used the method of Manoj Mohan (at the first comment) and successful at the fit step. However, the problem comes as TypeError when I modify the class of Manoj Mohan to
class MyKerasRegressor(KerasRegressor):
def fit(self, x, y, **kwargs):
x = np.expand_dims(x, -2)
super().fit(x, y, **kwargs)
def predict(self, x, **kwargs):
x = np.expand_dims(x, -2)
super().predict(x, **kwargs)
It has solved the dimension problem of predict() which the same as the .fit().
The problem is:
TypeError Traceback (most recent call last)
<ipython-input-84-68d76cb73e8b> in <module>
----> 1 pred_bag = bagging_model.predict(x_test)
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
Full script:
def model_base_LSTM():
model_cii = Sequential()
# Make layers
model_cii.add(CuDNNLSTM(50, return_sequences=True,input_shape=((1, 20))))
model_cii.add(Dropout(0.4))
model_cii.add(CuDNNLSTM(50, return_sequences=True))
model_cii.add(Dropout(0.4))
model_cii.add(CuDNNLSTM(50, return_sequences=True))
model_cii.add(Dropout(0.4))
model_cii.add(CuDNNLSTM(50, return_sequences=True))
model_cii.add(Dropout(0.4))
model_cii.add(Flatten())
# Output layer
model_cii.add(Dense(1))
# Compile
model_cii.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics=['accuracy'])
return model_cii
model = MyKerasRegressor(build_fn = model_base_LSTM, epochs=100, batch_size =70)
bagging_model = BaggingRegressor(base_estimator=model, n_estimators=10)
train_model = bagging_model.fit(x_train, y_train)
bagging_model.predict(x_test)
Output:
TypeError Traceback (most recent call last)
<ipython-input-84-68d76cb73e8b> in <module>
----> 1 pred_bag = bagging_model.predict(x_test)
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
There is an error in the model_base_LSTM() method. Replace
return model
with
return model_cii
Fix for "Error when checking input", an extra dimension could be added like this. This also takes care of scikit-learn(2 dimensions) vs Keras LSTM(3 dimensions) problem. Create a subclass of KerasRegressor to handle the dimension mismatch.
class MyKerasRegressor(KerasRegressor):
def fit(self, x, y, **kwargs):
x = np.expand_dims(x, -2)
return super().fit(x, y, **kwargs)
def predict(self, x, **kwargs):
x = np.expand_dims(x, -2)
return super().predict(x, **kwargs)
model = MyKerasRegressor(build_fn = model_base_LSTM, epochs=100, batch_size =70)