【问题标题】:Keras flatten: ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a TensorKeras flatten:ValueError:尝试将具有不受支持的类型(<class 'NoneType'>)的值(None)转换为张量
【发布时间】:2021-07-29 20:07:02
【问题描述】:

我有标题中提到的错误,代码如下

import tensorflow as tf
import numpy as np
import pandas as pd
import seaborn as sns
from random import shuffle
import datetime
from matplotlib import pyplot

from numpy import mean
from numpy import std
from matplotlib import pyplot
from sklearn.model_selection import KFold
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Flatten
from tensorflow.keras.optimizers import SGD

from tensorflow.keras.models import load_model

first_branch= Input(shape=(28,28,1))
first_branch_st1 = Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28,1))(first_branch)
first_branch_st2 = MaxPooling2D((2, 2))(first_branch_st1)
first_branch_st3 = Flatten()(first_branch_st2)
first_branch_st4 = Dense(100, activation='relu')(first_branch_st3)

这会发送以下错误

ValueError                                Traceback (most recent call last)
/tmp/ipykernel_1973/3453405928.py in <module>
      2 first_branch_st1 = Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28,1))(first_branch)
      3 first_branch_st2 = MaxPooling2D((2, 2))(first_branch_st1)
----> 4 first_branch_st3 = Flatten()(first_branch_st2)
      5 first_branch_st4 = Dense(100, activation='relu')(first_branch_st3)
      6 

ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.

根据same error 提出的问题,当您混淆 keras 和 tf.keras 时会发生这种情况。但我认为已经相应地定义了进口,所以除非进口之间存在冲突或对进口的定义不正确,否则我认为这不是问题所在。还有其他已知的解决方案吗?

【问题讨论】:

  • 我真的看不到任何错误。这是重现问题的全部代码吗?
  • 是的,有一些数据适配代码,但是和这部分无关。这是模型定义,还没有引入数据
  • 我在 google collab 上对 Tensorflow 2.6.0 有同样的错误。如果我降级到 2.5.0 它可以工作,但仅适用于 CPU。仍然找不到解决方案,我已经只通过 tf.keras 导入,而不是 keras。

标签: python tensorflow keras deep-learning tf.keras


【解决方案1】:

只有 1 个错误,Input 未定义但仍与提到的错误无关。在下面使用:

first_branch= tf.keras.Input(shape=(28,28,1))

我的建议是请检查tf 的最新版本,例如2.4.1.

【讨论】:

  • 数小时后试图找出问题所在,将所有文件重新安装到最后一次更新,尽管它已经在最后一个版本中了。我不得不删除我一直在处理的虚拟环境文件夹并从头开始一个新文件夹。之后错误消失了。
  • 我给你的答案是一个很好的关闭线程
【解决方案2】:

当我在 google collab (2.6.0) 上更新到最新版本的 TF 时,我遇到了同样的错误。以前我使用的是 2.5,它运行时没有错误。 从改变后

from tf.python.keras.applications.efficientnet import EfficientNetB3

from tf.keras.applications.efficientnet import EfficientNetB3

解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多