【发布时间】: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