• python常见错误类型


      Found dtype Double but expected Float

    在参与计算的时候两个参与loss计算的值要一样啊

    1. y_pred = model(train_x,batch_size)
    2. # 训练过程中,正向传播生成网络的输出,计算输出和实际值之间的损失值
    3. # 在参与计算的时候 两个loss参与值的类型要一样啊
    4. y_pred= y_pred.cpu().float()
    5. train_y=train_y.float()
    6. single_loss = loss_function(y_pred,train_y)
    7. single_loss.backward() # 调用backward()自动生成梯度
    8. optimizer.step() # 使用optimizer.step()执行优化器,把梯度传播回每个网络

    错误:ValueError: Shapes (6, 1) and (6, 20) are incompatible

    解析:

    如果y是one-hot encoding格式,使用sparse_categorical_crossentropy

    1. [1,0,0]
    2. [0,1,0]
    3. [0,0,1]

    如果y是整数,非one-hot encoding格式,使用categorical_crossentropy

    1. 1
    2. 2
    3. 3

     

    报错:errors_impl.UnknownError: 2 root error(s) found.

    tensorflow.python.framework.errors_impl.UnknownError: 2 root error(s) found.
      (0) Unknown:  Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
         [[node SegNet/block1_conv2/Relu (defined at media/ac/ubuntu train/Semantic-Segmentation-main/train.py:337) ]]
         [[confusion_matrix/assert_less_1/Assert/AssertGuard/pivot_f/_31/_77]]
      (1) Unknown:  Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
         [[node SegNet/block1_conv2/Relu (defined at media/ac/ubuntu train/Semantic-Segmentation-main/train.py:337) ]]
    0 successful operations.
    0 derived errors ignored. [Op:__inference_train_function_6169]
    原因分析:可能是显存太小

    按需分配现存

    tf 2.x与以前版本的差距是:

    tf 2.x:tensorflow.compat.v1 

    1. sess =tf.compat.v1.Session(config=config)
    2. from tensorflow.compat.v1 import ConfigProto
    3. from tensorflow.compat.v1 import InteractiveSession

     按需分配现存示例:

    1. from tensorflow.compat.v1 import ConfigProto
    2. from tensorflow.compat.v1 import InteractiveSession
    3. config = ConfigProto()
    4. config.gpu_options.allow_growth = True
    5. session = InteractiveSession(config=config)
    6. with tf.compat.v1.Session(config=config) as sess:
    7. # 输入图片为256x256,2个分类
    8. shape, classes = (224, 224, 3), 20
    9. # 调用keras的ResNet50模型
    10. model = keras.applications.resnet50.ResNet50(input_shape = shape, weights=None, classes=classes)
    11. model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
    12. # 训练模型 categorical_crossentropy sparse_categorical_crossentropy
    13. # training = model.fit(train_x, train_y, epochs=50, batch_size=10)
    14. model.fit(train_x,train_y,validation_data=(test_x, test_y), epochs=20, batch_size=6,verbose=2)
    15. # # 把训练好的模型保存到文件
    16. model.save('resnet_model_dog_n_face.h5')

    Expected object of scalar type Long but got scalar type Float for argument 

    这样可以避免出错:x=torch.Tensor(x.numpy()).float().to(device) # .float() .numpy()

                                    x=torch.Tensor(x.numpy()).to(device)

    模型不能在gpu上跑 几点原因:

    模型内部所以产生的变量都要以tensor的形式放在 gpu上 .to(device)

    第二点:模型 损失函数等要在gpu上

    1. model = LSTM().to(device)
    2. loss_function = nn.MSELoss().to(device)
    3. optimizer = torch.optim.SGD(model.parameters(), lr=0.01)#建立优化器实例
    4. print(model)

    pycharm报错:Process finished with exit code -1073741819 (0xC0000005)解决办法

     

     在模型种 tensor 不要重复的 使用 .to(device) 和torch.Tensor(arr)

    模型保存和加载出现错误:

    AttributeError: ‘Network‘ object has no attribute ‘copy‘

    解析:torch.nn.modules.module.ModuleAttributeError: ‘Network‘ object has no attribute ‘copy‘_NLOS的博客-CSDN博客

  • 相关阅读:
    字节跳动测开实习生面试,拿15K过分吗?
    HttpStatusCodeException.getResponseBodyAsString 乱码
    快应用参数传递
    关于swiper插件在vue2的使用
    代码随想录算法训练营Day32 | 贪心算法(2/6) Leetcode 122.买卖股票的最佳时机 II 55. 跳跃游戏 45.跳跃游戏II
    小程序之后台数据动态交互及WXS的使用 (5)
    【docker】Linux下查看或配置Docker的DNS服务器
    针对海量数据的存储与访问瓶颈的解决方案
    网易笔试题——mysql整理
    RabbitMQ 消息丢失案例(returnCallback/confirmCallback)
  • 原文地址:https://blog.csdn.net/weixin_55771290/article/details/126660602