Tag Archives: neural network

[Solved] RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #2 ‘mat1‘

Error Message (Error Codes below):
RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #2 ‘mat1’ in call to _th_addmm

for epoch in range(num_epochs):
    # Convert numpy arrays to torch tensors
    inputs = torch.from_numpy(x_train)
    targets = torch.from_numpy(y_train)
    # Forward pass
    outputs = model(inputs)
    loss = criterion(outputs, targets)
    
    # Backward and optimize
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    print("Epoch [{}/{}] Loss: {:.4f}".format(epoch+1, num_epochs, loss.item()))

Solution:

Method 1. Add

inputs = inputs.float()
targets = targets.float()
model = model.float()

Complete code

for epoch in range(num_epochs):
    # Convert numpy arrays to torch tensors
    inputs = torch.from_numpy(x_train)
    targets = torch.from_numpy(y_train)
    inputs = inputs.float()
    targets = inputs.float()
    model = model.float()
    # Forward pass
    outputs = model(inputs)
    loss = criterion(outputs, targets)
    
    # Backward and optimize
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    print("Epoch [{}/{}] Loss: {:.4f}".format(epoch+1, num_epochs, loss.item()))

Method 2. Add

inputs = inputs.double()
targets = inputs.double()
model = model.double()

Complete code

for epoch in range(num_epochs):
    # Convert numpy arrays to torch tensors
    inputs = torch.from_numpy(x_train)
    targets = torch.from_numpy(y_train)
    inputs = inputs.double()
    targets = inputs.double()
    model = model.double()
    # Forward pass
    outputs = model(inputs)
    loss = criterion(outputs, targets)
    
    # Backward and optimize
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    print("Epoch [{}/{}] Loss: {:.4f}".format(epoch+1, num_epochs, loss.item()))

(26)RuntimeError: Error(s) in loading state_dict for YoloBody:size mismatch for yolo_head3.1.weight

Run B_ Yolov4 an error is reported when calculating the map:

RuntimeError: Error(s) in loading state_dict for YoloBody:
	size mismatch for yolo_head3.1.weight: copying a param with shape torch.Size([75, 256, 1, 1]) from checkpoint, the shape in current model is torch.Size([255, 256, 1, 1]).
	size mismatch for yolo_head3.1.bias: copying a param with shape torch.Size([75]) from checkpoint, the shape in current model is torch.Size([255]).
	size mismatch for yolo_head2.1.weight: copying a param with shape torch.Size([75, 512, 1, 1]) from checkpoint, the shape in current model is torch.Size([255, 512, 1, 1]).
	size mismatch for yolo_head2.1.bias: copying a param with shape torch.Size([75]) from checkpoint, the shape in current model is torch.Size([255]).
	size mismatch for yolo_head1.1.weight: copying a param with shape torch.Size([75, 1024, 1, 1]) from checkpoint, the shape in current model is torch.Size([255, 1024, 1, 1]).
	size mismatch for yolo_head1.1.bias: copying a param with shape torch.Size([75]) from checkpoint, the shape in current model is torch.Size([255]).

resolvent:

Check class in train.py_ Names and num_ The classes are all right, so it is determined that it is a problem in several programs called when calculating map. Finally, it is found that the number of classes in yolo.py is wrong,

"classes_path"      : 'model_data/coco_classes.txt',

Replace with:

"classes_path"      : 'model_data/voc_classes.txt',

ginseng https://github.com/bubbliiiing/yolo3-pytorch/issues/17

https://blog.csdn.net/nangg1047/article/details/116073131

[Solved] YOLOv4 Error: Layer before convolutional layer must output image.: No error

 

Recently, when learning yolo4 and running your own data set with yolo4, I found that the
training set layer before revolutionary layer must output image.: no error.

 

1. Solution

Check the customized cfg file. The size of the input image is set as follows

if both height and width are set to a multiple of 32, this problem will not occur. I set it here as 416, 416

2. Follow up questions

Pay attention to setting size to the size of the picture in your dataset, otherwise, you may not be able to open the picture. The error is as follows

Can’t load image xxxxxxxxxxxxxxxxxx

[Solved] Pytorch Error: AttributeError: ‘Tensor‘ object has no attribute ‘backword‘

Pytorch error attribute error: ‘tensor’ object has no attribute ‘backword’

According to the error description, there is no backword attribute.

error code

loss.backword() # Reverse Propagation

correct

loss.backward() # Reverse Propagation

I mistyped a a letter. There is no error prompt on the Jupiter notebook editor, which is difficult to find 😂

[Solved] pytorch CrossEntropyLoss Error: RuntimeError: 1D target tensor expected, multi-target not supported

resolvent

crossentropyloss (predicted value, label) the required input dimensions are:

    1. when there is batch, the predicted value dimension is 2 and the size is

[batch]_ When size, n]

    1. , the dimension of label is 1, and the size is

[batch]_ Size]

    1. when there is no batch, the dimension of the predicted value is 2, the size is

[M, n]

    1. , the dimension of the label is 1, and the size is

[M]

Problem analysis

One case can illustrate:

import torch
import torch.nn as nn
import numpy as np

a = torch.tensor(np.random.random((30, 5)))
b = torch.tensor(np.random.randint(0, 4, (30))).long()
loss = nn.CrossEntropyLoss()

print("a的维度:", a.size()) # torch.Size([30, 5])
print("b的维度:", b.size()) # torch.Size([30])
print(loss(a, b)) # tensor(1.6319, dtype=torch.float64)

ModuleNotFoundError: No module named ‘notebook‘

ModuleNotFoundError: No module named ‘notebook’

Problem modulenotfounderror: no module named ‘notebook’

This problem occurred when running notebook today. Now I’d like to share with you how to solve this problem

terms of settlement

    open the terminal: Win + R, enter “CMD”, then “enter”
    activate the environment when you run the code: “CONDA activate + your environment name”
    after entering your environment, enter “Python – M PIP install Jupiter”, and then “enter”
    appears at the bottom to indicate that the installation is successful
    then enter: IPython notebook
    . This page indicates that the problem has been solved

Solve pytorch multiprocess valueerror: error initializing torch.distributed using env: //rendezvou… Error

error message: ValueError: Error initializing torch.distributed using env:// rendezvous: environment variable MASTER_ADDR expected, but not set
Solution 1:
Use in the code

import os

os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '5678'

Solution 2:

If you are running the command line, you can use:

export MASTER_ADDR=localhost
export MASTER_PORT=5678

Tensor for argument #2 ‘mat1‘ is on CPU, but expected it to be on GPU (while checking arguments for

Tensor for argument #2 ‘mat1’ is on CPU, but expected it to be on GPU (while checking arguments for addmm)
Both the model and the input data need to be moved to the device

model=NonLinearRegression().to(device)#Moudle
for batch_idx,(data,target) in enumerate(train_loader):
	data,target=data.to(device),target.to(device)
	...
for data,target in val_loader: 
	data,target=data.to(device),target.to(device)
	...

[Solved] Pytorch Tensor to numpy error: RuntimeError: Can‘t call numpy() on Tensor that requires grad.报错

Solution:

Use tensor. Detach(). Numpy() when turning numpy:

a = torch.ones(5)
b = a.detach().numpy()
print(b)

Problem analysis

When the tensor conversion in calculation, because it has gradient value, it cannot be directly converted to numpy format, so it is better to call . Detach(). Numpy() no matter how

Tensorflow reported an error when using session module: attributeerror: module ‘tensorflow’ has no attribute ‘session’, which has been solved

This function can only be invoked before creating any graph, operation or tensor. It can be used at the beginning of a complex migration project from tensorflow 1. X to 2. X.

A simpler method is found. When referring to tensorflow, you can directly use:
Import tensorflow.compat.v1 as TF

[Solved] Could not find a version that satisfies the requirement onnx (from onnx-graphsurgeon0.2.6)

Could not find a version that satisfies the requirement onnx (from onnx-graphsurgeon0.2.6) (from versions: )
No matching distribution found for onnx (from onnx-graphsurgeon
0.2.6)
nvidia The official documentation gives the command sudo pip3 install onnx_graphsurgeon-0.2.6-py2.py3-none-any.whl
After many attempts it showed the above error. Finally I found out it was a permission problem. Changed the command to pip install onnx_graphsurgeon-0.2.6-py2.py3-none-any.whl and it installed

To solve the problem of increasing video memory when training network (torch)

Method 1

torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True

Principle:

Cundnn follows the following criteria:

    if the dimension or type of network input data changes little, set    torch.backends.cudnn.benchmark  =  true   It can increase the operation efficiency; If the input data of the network changes every iteration, cndnn will find the optimal configuration every time, which will improve the operation efficiency

     
    Method 2

    Tensor calculation should be written as follows:

    train_loss += loss.item()