keras.layers.Conv2D 与tf.layers.Conv2D 的兼容性: AttributeError: ‘tuple‘ object has no attribute ‘layer‘

结论:

  • keras.layers.Conv2D 与 tf.layers.Conv2D有相同的参数设置模式
  • keras.layers.Conv2D 可以兼容处理 tf.layers.Conv2D得到的tensor
  • tf.layers.Conv2D得到的tensor不能兼容处理keras.layers.Conv2D 得到的tensor
  • 所以,在代码框架中同时使用 keras.layers.Conv2D 与 tf.layers.Conv2D 要注意。经过keras.layers处理过的数据应该都不能被tf.layers处理了

解决方法:

  • 用 tensorflow.keras.layers 代替 keras.layers
from keras.layers import Conv2D as conv2d_keras
from tensorflow.compat.v1.layers import Conv2D as conv2d_tf
import tensorflow.compat.v1 as tf
import numpy as np

data = tf.convert_to_tensor(np.random.random((4,256,256,3)).astype(np.float32))

conv1 = conv2d_tf(10,(3,3),strides=1,padding="valid",activation='relu')(data)
conv2 = conv2d_keras(10,(3,3),strides=1,padding="valid",activation='relu')(conv1)# 正常
#conv3 = conv2d_tf(10,(3,3),strides=1,padding="valid",activation='relu')(conv2)# 报错 AttributeError: 'tuple' object has no attribute 'layer'

 

你可能感兴趣的:(keras.layers.Conv2D 与tf.layers.Conv2D 的兼容性: AttributeError: ‘tuple‘ object has no attribute ‘layer‘)