1

Data preparation, and building the model:

dataset = datasets.load_iris()
data = dataset.data
target = dataset.target
data_tensor=torch.from_numpy(data).float()
target_tensor=torch.from_numpy(target).long()

model = nn.Sequential(
    bnn.BayesLinear(prior_mu=0, prior_sigma=0.1, in_features=4, out_features=100),
    nn.ReLU(),
    bnn.BayesLinear(prior_mu=0, prior_sigma=0.1, in_features=100, out_features=3),
)
cross_entropy_loss = nn.CrossEntropyLoss()
klloss = bnn.BKLLoss(reduction='mean', last_layer_only=False)
klweight = 0.01
optimizer = optim.Adam(model.parameters(), lr=0.01)

Run the model: Error occurred

for step in range(3000):
    models = model(data_tensor)
    cross_entropy = cross_entropy_loss(models, target)
    kl = klloss(model)
    total_cost = cross_entropy + klweight*kl

    optimizer.zero_grad()
    total_cost.backward()
    optimizer.step()
  
_, predicted = torch.max(models.data, 1)
final = target_tensor.size(0)
correct = (predicted == target_tensor).sum()
print('- Accuracy: %f %%' % (100 * float(correct) / final))
print('- CE : %2.2f, KL : %2.2f' % (cross_entropy.item(), kl.item()))
TypeError 
----> 3     cross_entropy = cross_entropy_loss(models, target)
TypeError: cross_entropy_loss(): argument 'target' (position 2) must be Tensor, not numpy.ndarray

I copied code from a web-learning page, but it shows the attached error, any suggestions? many thanks !!

xdurch0
  • 9,905
  • 4
  • 32
  • 38
Yumeng Xu
  • 179
  • 1
  • 2
  • 11

2 Answers2

2

You used the wrong variable for target.

cross_entropy_loss(models, target)

would be

cross_entropy_loss(models, target_tensor)
ayandas
  • 2,070
  • 1
  • 13
  • 26
1

Tensors are more generalized vectors. Thus every tensor can be represented as a multidimensional array or vector, but not every vector can be represented as tensors.

Hence numpy arrays can easily be replaced with tensor , but the tensor can not be replaced with numpy arrays

The crossentropyloss expects a tensor to be passed as argument but your target variable is an numpy array so it's raising the error. Pass target_tensor variable instead of target it will solve the issue.

Read more about Cross Entropyless here

Tasnuva Leeya
  • 2,515
  • 1
  • 13
  • 21
  • thank you for your explanation!!! Could you please check out my other questions about target # is out of bounds ? https://stackoverflow.com/questions/69969094/indexerror-target-11-is-out-of-bounds-cross-entropy – Yumeng Xu Nov 15 '21 at 05:35
  • 1
    If the answer is correct, Please select it, as that will help future viewers instantly find the answer. Thanks. – typedecker Nov 15 '21 at 05:47