tensorflow学习中遇到的bug

本文介绍了在更新到Keras2.x后如何正确设置模型输入形状,以及在遇到AttributeError关于reshape的问题时如何转换为numpy操作。推荐使用Input层明确指定输入形状,避免在非Input层使用input_shape或input_dim。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.

这个错误是因为从 Keras 2.x 版本开始,推荐使用 Input 层来明确指定模型的输入形状,而不是在添加第一个非 Input 层时通过 input_shapeinput_dim 参数来指定。

#原代码
model=tf.keras.Sequential([
    
    #unit1 第一层卷积层
    tf.keras.layers.Conv2D(16,kernel_size=(3,3),padding="same",activation=tf.nn.relu,input(shape=(28, 28, 1))),

#***********************
#修改后的代码
model=tf.keras.Sequential([
    tf.keras.layers.Input(shape=(28, 28, 1)),  # 假设输入是一个 28x28 的灰度图像  
    
    #unit1 第一层卷积层
    tf.keras.layers.Conv2D(16,kernel_size=(3,3),padding="same",activation=tf.nn.relu),

#***********************

2.AttributeError: EagerTensor object has no attribute 'reshape'. If you are looking for numpy-related methods, please run the following: tf.experimental.numpy.experimental_enable_numpy_behavior()

#原代码
X_train=X_train.reshape(60000,28,28,1)
X_test=X_test.reshape(10000,28,28,1)
#修改代码
X_train=np.array(X_train).reshape(60000,28,28,1)
X_test=np.array(X_test).reshape(10000,28,28,1)

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值