• 深度学习常见网络及报错大汇总:vgg训练 tensorflow版本匹配 模型保存与加载 loss特别大


    VGG原理与实现
    contrib替换方案

    xavier_initializer
    替换成
    tf.keras.initializers.glorot_normal()
    
    • 1
    • 2
    • 3
    slim = tf.contrib.slim
    替换成
    pip install --upgrade tf_slim
    import tf_slim
    slim = tf_slim
    
    • 1
    • 2
    • 3
    • 4
    • 5
    AttributeError: module 'tensorflow' has no attribute 'placeholder'
    替换成
    tf.compat.v1.disable_eager_execution()
    
    tf.compat.v1.placeholder
    
    • 1
    • 2
    • 3
    • 4
    • 5
    AttributeError: module 'tensorflow._api.v2.train' has no attribute 'exponential_decay'
    原先的
    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize
    修改成
    optimizer = tf.compat.v1.train.GradientDescentOptimizer(learning_rate).minimize
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    AttributeError: module 'tensorboard.summary._tf.summary' has no attribute 'merge_all'
    修改为
    tf.compat.v1.summary.merge_all()
    
    • 1
    • 2
    • 3

    imagenet_mini的数据集可以找我的网盘索要。

     module 'tensorflow' has no attribute 'Session'
     修改为
     tf.compat.v1.Session()
    
    • 1
    • 2
    • 3
    module 'tensorflow' has no attribute 'global_variables_initializer'
    替换成
    tf.compat.v1.global_variables_initializer().
    
    • 1
    • 2
    • 3

    出现报错

    ValueError: Cannot feed value of shape (128, 100) for Tensor labels:0, which has shape (None, 1000)
    解决如下图
    
    • 1
    • 2

    在这里插入图片描述

    ValueError: cannot reshape array of size 19267584 into shape (12800)
    ​数据大小对接出错,需要因式分解
    
    • 1
    • 2
    module 'keras.optimizers' has no attribute 'RMSprop'
    修改:
    from tensorflow import optimizers
    opt = optimizers.RMSprop(lr=0.0001, decay=1e-6)
    
    • 1
    • 2
    • 3
    • 4
    RuntimeError: tf.placeholder() is not compatible with eager execution.
    解决
    tf.compat.v1.disable_eager_execution()
    
    
    • 1
    • 2
    • 3
    • 4
    IOPub data rate exceeded.
    The Jupyter server will temporarily stop sending output
    to the client in order to avoid crashing it.
    To change this limit, set the config variable
    `--ServerApp.iopub_data_rate_limit`.
    
    Current values:
    ServerApp.iopub_data_rate_limit=1000000.0 (bytes/sec)
    ServerApp.rate_limit_window=3.0 (secs)
    解决
    
    jupyter notebook --generate-config
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    Found input variables with inconsistent numbers of samples: [448, 6400]
    解决:
    肯定出了什么岔子,样本数和标签数不一样
    
    • 1
    • 2
    • 3
    UnimplementedError: Graph execution error:
    
    Detected at node 'mean_squared_error/Cast' defined at (most recent call last):
    
    2 root error(s) found.
      (0) UNIMPLEMENTED:  Cast string to float is not supported
    	 [[{{node mean_squared_error/Cast}}]]
      (1) CANCELLED:  Function was cancelled before it was started
    0 successful operations.
    0 derived errors ignored. [Op:__inference_train_function_690]
    解决:
    抓住关键词float即可发现
    需要一个类型转换:
    Y_train = Y_train.astype(float)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述
    初始loss可能会很大,可能是激活函数造成的。

    net.load_weights("alexnet_weights.h5")
    AttributeError: 'str' object has no attribute 'decode'
    解决
    pip install h5py
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    强制使用cpu

    import os
    os.environ["CUDA_VISIBLE_DEVICES"] = "-1"  # 这一行注释掉就是使用gpu,不注释就是使用cpu
    
    
    • 1
    • 2
    • 3
    InternalError: Failed copying input tensor from /job:localhost/replica:0/task:0/device:CPU:0 to /job:localhost/replica:0/task:0/device:GPU:0 in order to run _EagerConst: Dst tensor is not initialized.
    这个多半就是内存不够了。
    
    • 1
    • 2
    ValueError: `labels.shape` must equal `logits.shape` except for the last dimension. Received: labels.shape=(4,) and logits.shape=(1, 4)
    解决
    当 **loss = ‘sparse_categorical_crossentropy’**时,数据的标签不能进行onehot编码,才能运行;
    当数据标签进行了onehot编码后,改为 **loss = ‘categorical_crossentropy’**就能跑通了。
    
    • 1
    • 2
    • 3
    • 4

    growth memory

    import tensorflow as tf
    gpu_list = tf.config.experimental.list_physical_devices('GPU')
    if len(gpu_list) > 0 :
      for gpu in gpu_list:
        try:
          # 设置多张 GPU ,如果不需要 for 去设置多张,则使用 list 的索引设置即可
          tf.config.experimental.set_memory_growth(gpu, True)
      	except RuntimeError as e:
        	print(e)
    else:
    	print("Got no GPUs")
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    memory_limit

    import tensorflow as tf
    
    using_gpu_index = 0 # 使用的 GPU 号码
    gpu_list = tf.config.experimental.list_physical_devices('GPU')
    if len(gpu_list) > 0:
      try:
        tf.config.experimental.set_virtual_device_configuration(
            gpu_list[using_gpu_index], 
            [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=2048)]
        )
      except RuntimeError as e:
        print(e)
    else:
    	print("Got no GPUs")
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    2022华数杯A题 B题 C题 思路汇总
    【Python游戏】Python基于pygame实现的人机大战的斗兽棋小游戏 | 附源码
    【100个 Unity实用技能】| Unity将本地图片文件显示到Image组件中 通用方法整理
    Linux系统之安装cook菜谱工具
    亚马逊云科技 Lambda 运行selenium
    Linux——文件系统inode与软硬链接
    【蓝桥杯选拔赛真题30】python计算倒数和 青少年组蓝桥杯python 选拔赛STEMA比赛真题解析
    Windows10怎么清除运行框中历史记录?电脑中怎么清除活动历史记录?
    分享34个发布商会PPT,总有一款适合您
    Zookeeper整理
  • 原文地址:https://blog.csdn.net/weixin_54227557/article/details/126408974